每当我在WinForms (.NET 3.5)中将焦点设置到文本框时,整个文本都会被选中。将MultiLine设置为true或false无关紧要。似乎与这个用户看到的完全相反:Making a WinForms TextBox behave like your browser's address bar
我尝试过这样做:
private void Editor_Load(object sender, EventArgs e)
{
//form load event
txtName.SelectedText = String.Empty; // has no effect
}是否可以设置其他属性来停止这种恼人的行为?
我刚刚注意到这是有效的:
txtName.Select(0,0);
txtScript.Select(0,0);但是我真的需要在我所有的文本框上调用select()吗?
发布于 2009-09-08 18:21:03
创建一个重写Enter事件的自定义TextBox控件。
如下所示:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace YourNamespace
{
class MyTextBox : TextBox
{
protected override void OnEnter(EventArgs e) {
this.Select(0, 0);
base.OnEnter(e);
}
}
}发布于 2009-09-08 18:17:11
如果你使用Select(0,0),你就不需要使用Focus(),所以我看不出有什么问题?它仍然以一个单独的调用结束。
https://stackoverflow.com/questions/1395442
复制相似问题