代码之家  ›  专栏  ›  技术社区  ›  Kevin

自动突出显示文本框控件中的文本

  •  32
  • Kevin  · 技术社区  · 16 年前

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

    15 回复  |  直到 8 年前
        1
  •  48
  •   Reed Copsey    16 年前

    在Windows窗体和WPF中:

    textbox.SelectionStart = 0;
    textbox.SelectionLength = textbox.Text.Length;
    
        2
  •  9
  •   beon    16 年前

    ASP.NET:

    textBox.Attributes.Add("onfocus","this.select();");
    
        3
  •  9
  •   MontrealKid    15 年前

    如果要为整个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(); 
        }
    
        4
  •  5
  •   Jan    13 年前

    如果您想通过鼠标单击使文本框中的文本突出显示,可以通过添加以下内容使其简单化:

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

    在:

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

    其中textbox 1是表单1中相关文本框的名称

    然后创建方法定义:

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

    在:

    public partial class Form1 : Form
    {
    
    }
    
        5
  •  4
  •   Roxy'Pro    9 年前

    用内置方法很容易实现 SelectAll

    只需写下:

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

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

        6
  •  3
  •   ShooShoSha    11 年前

    我认为最简单的方法是使用 TextBox.SelectAll 就像在进入事件中:

    private void TextBox_Enter(object sender, EventArgs e)
    {
        ((TextBox)sender).SelectAll();
    }
    
        7
  •  2
  •   Zamotic    15 年前

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

    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
  •  1
  •   user260578    16 年前

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

        9
  •  0
  •   Alatey    10 年前

    在事件“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;
            }
        }
    }
    
        10
  •  0
  •   believe me user2423466    10 年前

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

    textbox.Select(0, textbox.Text.Length)
    
        11
  •  0
  •   Majid    10 年前

    你可以用这个,皮蒂。D

    TextBox1.Focus();    
    TextBox1.Select(0, TextBox1.Text.Length);
    
        12
  •  0
  •   Ali asghar Fendereski    8 年前
     textBoxX1.Focus();
     this.ActiveControl = textBoxX1;
     textBoxX1.SelectAll();
    
        13
  •  0
  •   Brendan Gooden    8 年前

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

    只是处理 FocusEnter 事件不起作用,因为 Click 事件随后出现,如果 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;
    }
    
        14
  •  0
  •   congchien    8 年前

    窗口形式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;
        }
    
        15
  •  -1
  •   Billy Moukas    8 年前
    textbox.Focus();
    textbox.SelectionStart = 0;
    textbox.SelectionLength = textbox.Text.Length;
    
    推荐文章