当控件获得焦点时,如何自动突出显示文本框控件中的文本。在文本框控件中自动突出显示文本
回答
在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. 同时在2个文本框中突出显示文本
- 3. 在windows phone 7中突出显示文本框/文本块
- 4. 同时在两个文本框中突出显示文本
- 5. 突出显示文本框的文本,而不使用RichTextBox控件
- 6. 如何突出显示GWT文本框
- 7. 突出显示Winforms文本框中的文本
- 8. 无法突出显示IE9文本框中的文本
- 9. Wordpress - 突出显示文本框中的文本
- 10. 无法突出显示文本框中的文本
- 11. 使用jquery突出显示文本框中的文本
- 12. 突出显示文本
- 13. 突出显示文本,住
- 14. 突出显示的文本
- 15. 突出显示文本programmaticly
- 16. 突出显示文本CSS
- 17. EditText突出显示文本
- 18. CSS文本突出显示
- 19. 突出显示文本
- 20. 同时在多个EditText控件中突出显示文本
- 21. 在PySide.QTextEdit中突出显示文本
- 22. 在RichTextBlock中突出显示文本
- 23. 在Grocery Crud中突出显示文本
- 24. 在UITextView中突出显示文本
- 25. 动画文本突出显示
- 26. 如何在文本框中突出显示文字?
- 27. vb.net文本框,不会让我突出显示文本
- 28. 突出显示文本框内的文本
- 29. 用sencha框架突出显示文本并获取该文本?
- 30. 如何突出显示tkinter文本部件中的文本
http://msdn.microsoft.com/en-u s/library/hk09zy8f.aspx – 2013-02-12 23:22:52