2010-01-28 91 views

回答

39

在Windows窗体和WPF:

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

什么如果你想选择多个单词,但不是中间的单词? – f1wade 2013-10-15 11:24:49

+1

@ f1wade Windows窗体TextBox不支持。 – 2013-10-15 17:07:02

+0

UDP:在Windows Phone 8的WPF中支持tetxbox..SelectAll()(未在其他项目类型中测试过) – Epsil0neR 2014-01-16 21:34:23

9

在asp.net:

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

如果你想这样做对你的整个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(); 
    } 
+1

用于标签到文本框中。点击其中一项效果不佳。如果您点击文本,它会很快突出显示,然后在显示之前不会突出显示,就像您期望光标位于所单击的位置一样。 – denver 2015-02-17 21:27:36

2

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

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" /> 
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 
{ 

} 
3

我想在一个最简单的方法是使用TextBox.SelectAll喜欢Enter事件:

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

上的事件 “输入”(例如:按Tab键)或“首次点击”所有文字将被选中。 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; 
     } 
    } 
} 
0

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

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

您可以使用此,简练。:d

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

这是很容易与内置的方法来实现SelectAll

简单地凑可以这样写:

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

,一切都在文本框将会选择:)

-1
textbox.Focus(); 
textbox.SelectionStart = 0; 
textbox.SelectionLength = textbox.Text.Length; 
+3

尽管此代码可能会回答这个问题,但提供有关如何解决问题和/或为何解决问题的其他上下文会提高答案的长期价值。请阅读此https://stackoverflow.com/help/how-to-answer – 2017-06-18 08:42:29

0
textBoxX1.Focus(); 
this.ActiveControl = textBoxX1; 
textBoxX1.SelectAll();