2010-01-28 117 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();