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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:56:46  来源:igfitidea点击:

Auto highlight text in a textbox control

c#textbox

提问by Kevin

How do you auto highlight text in a textbox control when the control gains focus.

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

采纳答案by Reed Copsey

In Windows Forms and WPF:

在 Windows 窗体和 WPF 中:

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

回答by beon

In asp.net:

在asp.net中:

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

回答by user260578

If you need to do this for a large number of textboxes (in Silverlight or WPF), then you can use the technique used in the blog post: http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html. It uses Attached Properties and Routed Events.

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

回答by MontrealKid

If you want to do it for your whole WPF application you can do the following: - In the file App.xaml.cs

如果您想为整个 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(); 
    }

回答by Zamotic

Here's the code I've been using. It requires adding the attached property to each textbox you wish to auto select. Seeing as I don't want every textbox in my application to do this, this was the best solution to me.

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

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;
        }
    }
} 

The main change I made here was adding a loaded event to many of the examples I've seen. This allows the code to continue working after it's unloaded (ie. a tab is changed). Also I included code to make sure the text gets selected if you click on the textbox with the mouse, and not just keyboard focus it. Note: If you actually click on the text in the textbox, the cursor is inserted between the letters as it should.

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

You can use this by including the following tag in your xaml.

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

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

回答by Jan

If your intention is to get the text in the textbox highlighted on a mouse click you can make it simple by adding:

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

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

in:

在:

partial class Form1
{
    private void InitializeComponent()
    {

    }
}

where textBox1 is the name of the relevant textbox located in Form1

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

And then create the method definition:

然后创建方法定义:

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

in:

在:

public partial class Form1 : Form
{

}

回答by ShooShoSha

I think the easiest way is using TextBox.SelectAlllike in an Enter event:

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

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

回答by Alatey

On events "Enter" (for example: press Tab key) or "First Click" all text will be selected. dotNET 4.0

在事件“Enter”(例如:按 Tab 键)或“第一次单击”时,所有文本都将被选中。网络 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;
        }
    }
}

回答by believe me

I don't know why nobody mentioned that but you can also do this, it works for me

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

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

回答by Majid

You can use this, pithy. :D

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

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