开发者

Problem controlling input on treeViewNode LabelEdit

I am trying to control user input when he/she wants to ed开发者_StackOverflow社区it a treeNode. I don't want him to be able to write numbers (or even better not write a number at given index of the text but thats a bonus)

What I did was make a boolean on the mainWindow that determines if the user is editing the treeNode or not like this:

void Tree_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) {

       isEditing = false;
}



void Tree_BeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) {
      isEditing = true;
}

private void Tree_KeyDown(object sender, KeyEventArgs e) {

      control input. (e.Handled = true when keyDown is a number.)

}

This is what I tried but the event keydown is only called when I am NOT editing a treenode. So it falls out of purpose. (I use keydown already to handle arrow key events but I want to do both.)

Tried with keypress aswell to no avail.


The TreeView does not have any mechanism to control the LabelEdit part. Even if you can handle the KeyDown part of the Label to prevent numbers, you would still have to inspect the contents on the AfterLabelEdit because the user can "paste" numbers into the field as well. It's best to just control it during the AfterLabelEdit event:

private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
  foreach (char c in e.Label)
  {
    if (char.IsNumber(c))
      e.CancelEdit = true;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜