我有一个包含一个 TextBox
的表单,在C#中,我通过以下方式将其设置为字符串:
textBox.Text = str;
我有一个包含一个 TextBox
的表单,在C#中,我通过以下方式将其设置为字符串:
textBox.Text = str;
文本框具有 TabIndex
值为0且 TabStop
属性设置为true。这意味着当表单显示时,该控件将获得焦点。
您可以将另一个控件设为0的 TabIndex
值(如果有的话),并给文本框分配不同的 TabIndex(>0),或者将 TabStop
属性设置为false来停止此操作。
TabStop
属性从True
更改为False
,然后一切问题都得到解决了。 - ewu11protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) && (Control.MouseButtons == MouseButtons.None))
{
base.SelectAll();
}
}
}
问题出在那个if语句上,它导致了我们不喜欢的行为。更糟糕的是,为了雪上加霜,每当重新分配文本时,Text
属性的setter都会盲目重置selectionSet
变量:
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.selectionSet = false;
}
}
Text
,然后再次切换到文本框中,那么所有文本都将再次被选择。Load()
和每当Text
更改时调用DeselectAll()
来触发selectionSet
的设置:protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox2.SelectionStart = this.textBox2.Text.Length;
this.textBox2.DeselectAll();
}
DeselectAll()
仅将SelectionLength
设置为零。实际上,是SelectionStart
翻转了TextBox
的selectionSet
变量。在上述情况下,调用DeselectAll()
是不必要的,因为我们将开始位置设置为文本的末尾。但如果我们将其设置为任何其他位置,比如文本的开头,那么调用它是一个好主意。)
更持久的方法是通过继承创建具有所需行为的自定义TextBox:
public class NonSelectingTextBox : TextBox
{
// Base class has a selectionSet property, but its private.
// We need to shadow with our own variable. If true, this means
// "don't mess with the selection, the user did it."
private bool selectionSet;
protected override void OnGotFocus(EventArgs e)
{
bool needToDeselect = false;
// We don't want to avoid calling the base implementation
// completely. We mirror the logic that we are trying to avoid;
// if the base implementation will select all of the text, we
// set a boolean.
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) &&
(Control.MouseButtons == MouseButtons.None))
{
needToDeselect = true;
}
}
// Call the base implementation
base.OnGotFocus(e);
// Did we notice that the text was selected automatically? Let's
// de-select it and put the caret at the end.
if (needToDeselect)
{
this.SelectionStart = this.Text.Length;
this.DeselectAll();
}
}
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
// Update our copy of the variable since the
// base implementation will have flipped its back.
this.selectionSet = false;
}
}
}
base.OnGotFocus()
,但这样我们就会失去基本Control
类中有用的功能。你可能也会想完全不处理selectionSet
的麻烦,而只是在每次OnGotFocus()中取消选择文本,但这样如果用户从字段中切换出去再切回来,我们就会丢失用户的高亮。
丑陋?当然。但它就是它。
SelectionStart
设置为0
即可解决问题!yourtextbox.Text = "asdf";
yourtextbox.SelectionStart = 0;
如果您想取消文本字段的高亮显示,可以在VS 2013中尝试使用以下初始化:
myTextBox.GotFocus += new System.EventHandler(this.myTextBox_GotFocus);
public void myTextBox_GotFocus(object sender, EventArgs e)
{
myTextBox.SelectionLength=0;
}
我没有在C#上测试过这个,但是我在使用C++ WIN32对话框时遇到了同样的问题。似乎你可以通过从OnInitDialog()
或WM_INITDIALOG
返回FALSE
来改变行为。希望这能帮到你。
public void SetNotes(string notes)
{
notesTextBox.Text = notes;
notesTextBox.Select();
notesTextBox.SelectionLength = 0;
notesTextBox.SelectionStart = notes.Length;//place cursor at end of text
}