【问题标题】:Auto highlight text in a textbox control自动突出显示文本框控件中的文本
【发布时间】:2011-01-10 05:16:39
【问题描述】:

当控件获得焦点时,如何自动突出显示文本框控件中的文本。

【问题讨论】:

标签: c# textbox


【解决方案1】:

在 Windows 窗体和 WPF 中:

textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;

【讨论】:

  • 如果你想选择多个单词,而不是中间的单词怎么办?
  • @f1wade Windows 窗体文本框不支持。
  • UDP:Windows Phone 8 的 WPF 支持 tetxbox..SelectAll()(未在其他项目类型中测试)
  • 我发现在文本框的 GotMouseCapture 事件中使用上面的代码可以像我需要的那样工作。
  • 如果组件继承自 TextBox,如 MakedTextBox,这可能不起作用。如果是您的情况,请参阅此答案Select all in MaskedTextBox
【解决方案2】:

如果您想为整个 WPF 应用程序执行此操作,您可以执行以下操作: - 在文件 App.xaml.cs 中

    protected override void OnStartup(StartupEventArgs e)
    {
        //works for tab into textbox
        EventManager.RegisterClassHandler(typeof(TextBox),
            TextBox.GotFocusEvent,
            new RoutedEventHandler(TextBox_GotFocus));
        //works for click textbox
        EventManager.RegisterClassHandler(typeof(Window),
            Window.GotMouseCaptureEvent,
            new RoutedEventHandler(Window_MouseCapture));

        base.OnStartup(e);
    }
    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        (sender as TextBox).SelectAll();
    }

    private void Window_MouseCapture(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
             textBox.SelectAll(); 
    }

【讨论】:

  • 用于标签进入文本框。没有那么好点击一个。如果您单击文本,它会快速突出显示,然后在显示之前取消突出显示,正如您所期望的那样,光标位于单击的位置。
【解决方案3】:

在 ASP.NET 中:

textbox.Attributes.Add("onfocus","this.select();");

【讨论】:

    【解决方案4】:

    使用内置方法SelectAll很容易实现

    你可以这么写:

    txtTextBox.Focus();
    txtTextBox.SelectAll();
    

    文本框中的所有内容都将被选中:)

    【讨论】:

    • 除非它没有。在我的情况下,我试图在输入事件中选择所有文本,但没有选择任何内容,并且插入符号保持脉动。击中断点时触发事件。有什么想法吗?
    【解决方案5】:

    如果您的目的是让文本框中的文本在鼠标单击时突出显示,您可以通过添加使其变得简单:

    this.textBox1.Click += new System.EventHandler(textBox1_Click);
    

    在:

    partial class Form1
    {
        private void InitializeComponent()
        {
    
        }
    }
    

    其中 textBox1 是位于 Form1 中的相关文本框的名称

    然后创建方法定义:

    void textBox1_Click(object sender, System.EventArgs e)
    {
        textBox1.SelectAll();
    }
    

    在:

    public partial class Form1 : Form
    {
    
    }
    

    【讨论】:

      【解决方案6】:

      我认为最简单的方法是在 Enter 事件中使用 TextBox.SelectAll

      private void TextBox_Enter(object sender, EventArgs e)
      {
          ((TextBox)sender).SelectAll();
      }
      

      【讨论】:

      • 你试过了吗?我复制了你的例子,它没有选择任何东西。
      • 我在 5 年前做过。这也是一个 sn-p 缺少许多其他代码来将事件连接到控件。但是方法 SelectAll 是 TextBoxBase 类的一部分:docs.microsoft.com/en-us/dotnet/api/…>
      • 看起来 OnEnter 事件不能很好地与 SelectAll() 方法配合使用。文本框的 OnEnter 事件清除 SelectAll() 中的选择。
      • 很好奇。很难回忆很久以前我的想法是什么,但我想我参考了 Enter 事件 docs.microsoft.com/en-us/dotnet/api/…> 的备注,可能没有注意到 OnEnter 方法 docs.microsoft.com/en-us/dotnet/api/…>。无论如何,我的例子是缺乏的。
      【解决方案7】:

      这是我一直在使用的代码。它需要将附加属性添加到您希望自动选择的每个文本框。鉴于我不希望我的应用程序中的每个文本框都这样做,这对我来说是最好的解决方案。

      public class AutoSelectAll
      {
          public static bool GetIsEnabled(DependencyObject obj) 
          { 
              return (bool)obj.GetValue(IsEnabledProperty); 
          } 
          public static void SetIsEnabled(DependencyObject obj, bool value) 
          { 
              obj.SetValue(IsEnabledProperty, value);
          }
      
          static void ue_Loaded(object sender, RoutedEventArgs e)
          {
              var ue = sender as FrameworkElement;
              if (ue == null)
                  return;
              ue.GotFocus += ue_GotFocus;
              ue.GotMouseCapture += ue_GotMouseCapture;
          }
      
          private static void ue_Unloaded(object sender, RoutedEventArgs e)
          {
              var ue = sender as FrameworkElement;
              if (ue == null)
                  return;
              //ue.Unloaded -= ue_Unloaded;
              ue.GotFocus -= ue_GotFocus;
              ue.GotMouseCapture -= ue_GotMouseCapture;
          }
      
          static void ue_GotFocus(object sender, RoutedEventArgs e)
          {
              if (sender is TextBox)
              {
                  (sender as TextBox).SelectAll();
              }
              e.Handled = true;
          }
      
          static void ue_GotMouseCapture(object sender, MouseEventArgs e)
          {
              if (sender is TextBox)
              {
                  (sender as TextBox).SelectAll();
              }
              e.Handled = true;
          }
      
          public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool),
              typeof(AutoSelectAll), new UIPropertyMetadata(false, IsEnabledChanged));
      
          static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var ue = d as FrameworkElement;
              if (ue == null)
                  return;
              if ((bool)e.NewValue)
              {
                  ue.Unloaded += ue_Unloaded;
                  ue.Loaded += ue_Loaded;
              }
          }
      } 
      

      我在这里所做的主要更改是在我见过的许多示例中添加了加载事件。这允许代码在卸载后继续工作(即更改选项卡)。我还包含了代码以确保如果您用鼠标单击文本框,而不仅仅是键盘焦点,则选择文本。注意:如果您实际单击文本框中的文本,则光标会按原样插入到字母之间。

      您可以通过在您的 xaml 中包含以下标记来使用它。

      <TextBox  
          Text="{Binding Property}"
          Library:AutoSelectAll.IsEnabled="True" />
      

      【讨论】:

        【解决方案8】:

        如果您需要对大量文本框(在 Silverlight 或 WPF 中)执行此操作,则可以使用博客文章中使用的技术:http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html。它使用附加属性和路由事件。

        【讨论】:

          【解决方案9】:

          你可以用这个,精辟。 :D

          TextBox1.Focus();    
          TextBox1.Select(0, TextBox1.Text.Length);
          

          【讨论】:

            【解决方案10】:

            如果您只想在用户第一次单击框中时选择所有文本,然后让他们根据需要在文本中间单击,这就是我最终使用的代码。

            仅处理FocusEnter 事件是行不通的,因为Click 事件随后发生,如果您在Focus 事件中SelectAll(),则会覆盖选择。

            private bool isFirstTimeEntering;
            private void textBox_Enter(object sender, EventArgs e)
            {
                isFirstTimeEntering = true;
            }
            
            private void textBox_Click(object sender, EventArgs e)
            {
                switch (isFirstTimeEntering)
                {
                    case true:
                        isFirstTimeEntering = false;
                        break;
                    case false:
                        return;
                }
            
                textBox.SelectAll();
                textBox.SelectionStart = 0;
                textBox.SelectionLength = textBox.Text.Length;
            }
            

            【讨论】:

              【解决方案11】:

              如果您想在“On_Enter Event”中全选,这将无助于您实现目标。 尝试使用“On_Click 事件”

                  private void textBox_Click(object sender, EventArgs e)
                  {
                      textBox.Focus();
                      textBox.SelectAll();
                  }
              

              【讨论】:

                【解决方案12】:

                在事件“Enter”(例如:按 Tab 键)或“First Click”时,所有文本都将被选中。 dotNET 4.0

                public static class TbHelper
                {
                    // Method for use
                    public static void SelectAllTextOnEnter(TextBox Tb)
                    {
                        Tb.Enter += new EventHandler(Tb_Enter);
                        Tb.Click += new EventHandler(Tb_Click);
                    }
                
                    private static TextBox LastTb;
                
                    private static void Tb_Enter(object sender, EventArgs e)
                    {
                        var Tb = (TextBox)sender;
                        Tb.SelectAll();
                        LastTb = Tb;
                    }
                
                    private static void Tb_Click(object sender, EventArgs e)
                    {
                        var Tb = (TextBox)sender;
                        if (LastTb == Tb)
                        {
                            Tb.SelectAll();
                            LastTb = null;
                        }
                    }
                }
                

                【讨论】:

                  【解决方案13】:

                  我不知道为什么没有人提到,但你也可以这样做,它对我有用

                  textbox.Select(0, textbox.Text.Length)
                  

                  【讨论】:

                    【解决方案14】:
                     textBoxX1.Focus();
                     this.ActiveControl = textBoxX1;
                     textBoxX1.SelectAll();
                    

                    【讨论】:

                      【解决方案15】:

                      在 c# 窗体中。如果您使用 Enter 事件,它将不起作用。尝试使用 MouseUp 事件

                          bool FlagEntered;
                          private void textBox1_MouseUp(object sender, MouseEventArgs e)
                          {
                              if ((sender as TextBox).SelectedText == "" && !FlagEntered)
                              {
                                  (sender as TextBox).SelectAll();
                                  FlagEntered = true;
                              }
                          }
                      
                          private void textBox1_Leave(object sender, EventArgs e)
                          {
                              FlagEntered = false;
                          }
                      

                      【讨论】:

                        【解决方案16】:
                        textbox.Focus();
                        textbox.SelectionStart = 0;
                        textbox.SelectionLength = textbox.Text.Length;
                        

                        【讨论】:

                        • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读此stackoverflow.com/help/how-to-answer
                        猜你喜欢
                        • 2010-10-18
                        • 1970-01-01
                        • 1970-01-01
                        • 2020-12-24
                        • 1970-01-01
                        • 2014-02-09
                        • 2011-02-21
                        • 2013-01-23
                        • 1970-01-01
                        相关资源
                        最近更新 更多