當控件獲得焦點時,如何自動突出顯示文本框控件中的文本。在文本框控件中自動突出顯示文本
回答
在asp.net:
textBox.Attributes.Add("onfocus","this.select();");
如果您需要爲大量的文本框(在Silverlight或WPF)的做到這一點,那麼你可以使用博客文章中使用的技術:http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html。它使用附加屬性和路由事件。
如果你想這樣做對你的整個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();
}
用於標籤到文本框中。點擊其中一項效果不佳。如果您點擊文本,它會很快突出顯示,然後在顯示之前不會突出顯示,就像您期望光標位於所單擊的位置一樣。 – denver 2015-02-17 21:27:36
這是我一直在使用的代碼。它需要將附加屬性添加到您希望自動選擇的每個文本框。鑑於我不希望我的應用程序中的每個文本框都這樣做,這對我來說是最好的解決方案。
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" />
如果你的目的是要獲得在文本框中的文本高亮顯示鼠標點擊,你可以把它簡單的通過增加:
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
{
}
我想在一個最簡單的方法是使用TextBox.SelectAll
喜歡Enter事件:
private void TextBox_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
上的事件 「輸入」(例如:按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;
}
}
}
我不知道爲什麼沒有人提到這一點,但你也可以做到這一點,它爲我工作
textbox.Select(0, textbox.Text.Length)
您可以使用此,簡練。:d
TextBox1.Focus();
TextBox1.Select(0, TextBox1.Text.Length);
這是很容易與內置的方法來實現SelectAll
簡單地湊可以這樣寫:
txtTextBox.Focus();
txtTextBox.SelectAll();
,一切都在文本框將會選擇:)
textbox.Focus();
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
儘管此代碼可能會回答這個問題,但提供有關如何解決問題和/或爲何解決問題的其他上下文會提高答案的長期價值。請閱讀此https://stackoverflow.com/help/how-to-answer – 2017-06-18 08:42:29
textBoxX1.Focus();
this.ActiveControl = textBoxX1;
textBoxX1.SelectAll();
- 1. 如何在UIAlertView文本域內自動突出顯示文本
- 2. 突出顯示文本框的文本,而不使用RichTextBox控件
- 3. 無法突出顯示IE9文本框中的文本
- 4. CSS文本突出顯示
- 5. 突出顯示文本
- 6. 突出顯示文本,住
- 7. EditText突出顯示文本
- 8. 在RichTextBlock中突出顯示文本
- 9. 動畫文本突出顯示
- 10. 突出顯示文本框內的文本
- 11. 如何突出顯示tkinter文本部件中的文本
- 12. 突出顯示文本 - 本機Javascript
- 13. Android - 突出顯示文本和副本
- 14. 阻止能夠在C#窗體中突出顯示文本框中的文本
- 15. 自定義文本框:突出顯示和選擇
- 16. .net自定義消息框文本突出顯示
- 17. 突出顯示iPhone中的文本
- 18. Word中突出顯示的文本
- 19. 突出顯示iframe中的文本
- 20. HTML中的jQuery文本突出顯示
- 21. 如何在輸入文本框中選擇(突出顯示)文本
- 22. 如何自動顯示文本框中的文本開頭
- 23. 如何突出顯示文本框中的文本而在另一個文本框中鍵入?
- 24. 如何自動選擇/突出顯示文本/輸入框中的文本在榆木
- 25. 禁用在輸入框中突出顯示文本的能力
- 26. 在富文本框中突出顯示VB.NET語法
- 27. C#WPF - 突出顯示文本顏色問題的組合框突出顯示
- 28. 顯示彈出文本框
- 29. Javascript/AJAX自動顯示文本文件中的文本
- 30. 突出顯示文本區域中的文本
http://msdn.microsoft.com/en-u s/library/hk09zy8f.aspx – 2013-02-12 23:22:52