Winforms 如何为TreeNode名称和文本属性设置MaxLength?
如何为TreeNode名称和文本属性设置MaxLength?这是一个windows窗体应用程序,用户右键单击树视图以添加节点,树节点名称的最大长度应为40个字符。目前,我在AfterlabelEdit事件中检查此项,并在字符数超过时抛出一条消息。但是requiremnet说要限制长度而不显示消息框,就像我们在文本框中所做的那样Winforms 如何为TreeNode名称和文本属性设置MaxLength?,winforms,treenode,Winforms,Treenode,如何为TreeNode名称和文本属性设置MaxLength?这是一个windows窗体应用程序,用户右键单击树视图以添加节点,树节点名称的最大长度应为40个字符。目前,我在AfterlabelEdit事件中检查此项,并在字符数超过时抛出一条消息。但是requiremnet说要限制长度而不显示消息框,就像我们在文本框中所做的那样 谢谢。您可以在树状视图上显示一个文本框,并设置其最大长度 一种方法是使用以下表单创建文本框: private TextBox _TextBox; pub
谢谢。您可以在树状视图上显示一个文本框,并设置其最大长度 一种方法是使用以下表单创建文本框:
private TextBox _TextBox;
public Form1()
{
InitializeComponent();
_TextBox = new TextBox();
_TextBox.Visible = false;
_TextBox.LostFocus += new EventHandler(_TextBox_LostFocus);
_TextBox.Validating += new CancelEventHandler(_TextBox_Validating);
this.Controls.Add(_TextBox);
}
private void _TextBox_LostFocus(object sender, EventArgs e)
{
_TextBox.Visible = false;
}
private void _TextBox_Validating(object sender, CancelEventArgs e)
{
treeView1.SelectedNode.Text = _TextBox.Text;
}
然后在树视图BeforeLabeleEdit中设置文本框的最大长度,并在当前选定的节点上显示:
private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
_TextBox.MaxLength = 10;
e.CancelEdit = true;
TreeNode selectedNode = treeView1.SelectedNode;
_TextBox.Visible = true;
_TextBox.Text = selectedNode.Text;
_TextBox.SelectAll();
_TextBox.BringToFront();
_TextBox.Left = treeView1.Left + selectedNode.Bounds.Left;
_TextBox.Top = treeView1.Top + selectedNode.Bounds.Top;
_TextBox.Focus();
}
您可能希望向文本框添加一些附加功能,以便它能够根据树状视图的宽度正确调整大小,并且能够在用户点击回车键时接受新文本,等等