TreeView 有个 AfterEdit 事件。
解决方案 »
- MonthCalendar控件如何改变大小
- 请教有关C#中,我对一个页面POST了用户名与密码等相关数据,验证通过后,它应该返回给我另一个页面地址,这个地址如何获取?
- c# 与 sql的问题。
- 执行insert 语句,怎样插入时间呢?
- 如何将表中记录构建成需要的二维数组方式!
- 请教ComboBox的数据源如何绑定这样的数据?
- 请问下各位哥哥姐姐怎么样才能实现两个类之间的参数传递
- 高分求解TextBox"继承控件问题"
- Active Directory:怎样用ADSI查询显示扩展类对象
- 如何向xml写入数据(一条记录row)?
- 水晶报表文件,怎么把其中的数据导出?
- datagrid为什么不能显示?我觉得没做错亚,大哥指教
事件处理程序接收一个 NodeLabelEditEventArgs 类型的参数,它包含与此事件相关的数据。下列 NodeLabelEditEventArgs 属性提供特定于此事件的信息。
属性说明
CancelEdit获取或设置指示是否已取消编辑的值。
Label获取与树节点关联的新文本。
Node获取包含待编辑文本的树节点。备注
有关处理事件的详细信息,请参见使用事件。
示例
[Visual Basic, C#] 下面的示例允许用户使用 ContextMenu 编辑非根树节点。当用户右击鼠标时,即可确定该位置的 TreeNode,并将其存储在名为 mySelectedNode 的变量中。如果选择了非根树节点,则它被置于可编辑状态,允许用户编辑节点标签。用户停止编辑树节点标签后,将计算新的标签文本并予以保存。对于本示例,多个字符在标签文本中无效。如果标签字符串中有一个无效字符,或者该字符串为空,则将向用户通知错误,同时标签返回到其以前的文本。
[Visual Basic]
' Get the tree node under the mouse pointer and
' save it in the mySelectedNode variable.
Private Sub treeView1_MouseDown(sender As Object, _
e As System.Windows.Forms.MouseEventArgs)
mySelectedNode = treeView1.GetNodeAt(e.X, e.Y)
End Sub
Private Sub menuItem1_Click(sender As Object, e As System.EventArgs)
If Not (mySelectedNode Is Nothing) And _
Not (mySelectedNode.Parent Is Nothing) Then
treeView1.SelectedNode = mySelectedNode
treeView1.LabelEdit = True
If Not mySelectedNode.IsEditing Then
mySelectedNode.BeginEdit()
End If
Else
MessageBox.Show("No tree node selected or selected node is a root node." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"Editing of root nodes is not allowed.", "Invalid selection")
End If
End Sub
Private Sub treeView1_AfterLabelEdit(sender As Object, _
e As System.Windows.Forms.NodeLabelEditEventArgs)
If Not (e.Label Is Nothing) Then
If e.Label.Length > 0 Then
If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) = -1 Then
' Stop editing without canceling the label change.
e.Node.EndEdit(False)
Else
' Cancel the label edit action, inform the user, and
' place the node in edit mode again.
e.CancelEdit = True
MessageBox.Show("Invalid tree node label." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"The invalid characters are: '@','.', ',', '!'", _
"Node Label Edit")
e.Node.BeginEdit()
End If
Else
' Cancel the label edit action, inform the user, and
' place the node in edit mode again.
e.CancelEdit = True
MessageBox.Show("Invalid tree node label." & _
Microsoft.VisualBasic.ControlChars.Cr & _
"The label cannot be blank", "Node Label Edit")
e.Node.BeginEdit()
End If
Me.treeView1.LabelEdit = False
End If
End Sub
[C#]
/* Get the tree node under the mouse pointer and
save it in the mySelectedNode variable. */
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}private void menuItem1_Click(object sender, System.EventArgs e)
{
if (mySelectedNode != null && mySelectedNode.Parent != null)
{
treeView1.SelectedNode = mySelectedNode;
treeView1.LabelEdit = true;
if(!mySelectedNode.IsEditing)
{
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}private void treeView1_AfterLabelEdit(object sender,
System.Windows.Forms.NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if(e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
{
// Stop editing without canceling the label change.
e.Node.EndEdit(false);
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\n" +
"The invalid characters are: '@','.', ',', '!'",
"Node Label Edit");
e.Node.BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
this.treeView1.LabelEdit = false;
}
}