【发布时间】:2011-01-10 05:16:39
【问题描述】:
当控件获得焦点时,如何自动突出显示文本框控件中的文本。
【问题讨论】:
当控件获得焦点时,如何自动突出显示文本框控件中的文本。
【问题讨论】:
在 Windows 窗体和 WPF 中:
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
【讨论】:
如果您想为整个 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();
}
【讨论】:
在 ASP.NET 中:
textbox.Attributes.Add("onfocus","this.select();");
【讨论】:
使用内置方法SelectAll很容易实现
你可以这么写:
txtTextBox.Focus();
txtTextBox.SelectAll();
文本框中的所有内容都将被选中:)
【讨论】:
如果您的目的是让文本框中的文本在鼠标单击时突出显示,您可以通过添加使其变得简单:
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
{
}
【讨论】:
我认为最简单的方法是在 Enter 事件中使用 TextBox.SelectAll:
private void TextBox_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
【讨论】:
这是我一直在使用的代码。它需要将附加属性添加到您希望自动选择的每个文本框。鉴于我不希望我的应用程序中的每个文本框都这样做,这对我来说是最好的解决方案。
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" />
【讨论】:
如果您需要对大量文本框(在 Silverlight 或 WPF 中)执行此操作,则可以使用博客文章中使用的技术:http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html。它使用附加属性和路由事件。
【讨论】:
你可以用这个,精辟。 :D
TextBox1.Focus();
TextBox1.Select(0, TextBox1.Text.Length);
【讨论】:
如果您只想在用户第一次单击框中时选择所有文本,然后让他们根据需要在文本中间单击,这就是我最终使用的代码。
仅处理FocusEnter 事件是行不通的,因为Click 事件随后发生,如果您在Focus 事件中SelectAll(),则会覆盖选择。
private bool isFirstTimeEntering;
private void textBox_Enter(object sender, EventArgs e)
{
isFirstTimeEntering = true;
}
private void textBox_Click(object sender, EventArgs e)
{
switch (isFirstTimeEntering)
{
case true:
isFirstTimeEntering = false;
break;
case false:
return;
}
textBox.SelectAll();
textBox.SelectionStart = 0;
textBox.SelectionLength = textBox.Text.Length;
}
【讨论】:
如果您想在“On_Enter Event”中全选,这将无助于您实现目标。 尝试使用“On_Click 事件”
private void textBox_Click(object sender, EventArgs e)
{
textBox.Focus();
textBox.SelectAll();
}
【讨论】:
在事件“Enter”(例如:按 Tab 键)或“First Click”时,所有文本都将被选中。 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)
【讨论】:
textBoxX1.Focus();
this.ActiveControl = textBoxX1;
textBoxX1.SelectAll();
【讨论】:
在 c# 窗体中。如果您使用 Enter 事件,它将不起作用。尝试使用 MouseUp 事件
bool FlagEntered;
private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if ((sender as TextBox).SelectedText == "" && !FlagEntered)
{
(sender as TextBox).SelectAll();
FlagEntered = true;
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
FlagEntered = false;
}
【讨论】:
textbox.Focus();
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
【讨论】: