treeView afterLabelEdit deadly embrace

P

.pd.

I created a Windows form and stuck a toolbar on it with one button. I
then added a treeView, docked it to the left of the form and enabled
Label Edit.

Here's the toolbar button click and after label edit event code:

private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (treeView1.Nodes.Count > 0)
{
if (treeView1.SelectedNode != null)
{
if (treeView1.SelectedNode.IsEditing)
{
MessageBox.Show("You are editing another
node");
return;
}
}
}

TreeNode nextIdea = new TreeNode("", 0, 0);
treeView1.Nodes.Add(nextIdea);
nextIdea.BeginEdit();

}

private void treeView1_AfterLabelEdit(object sender,
System.Windows.Forms.NodeLabelEditEventArgs e)
{
if (e.Label == null)
{
MessageBox.Show("can't be blank (label = null)");
e.CancelEdit = true;
e.Node.BeginEdit();
}
else
if (e.Label.Length == 0)
{
MessageBox.Show("can't be blank (length = 0)");
e.CancelEdit = true;
e.Node.BeginEdit();
}
}

Here's what happens when I click the toolbar button and then click it
again without entering anything in the new node label.

-- get message "can't be blank (label = null)"
(click ok)
-- get message "can't be blank (label = null)"
(click ok)
-- get message "can't be blank (label = null)" and 2nd node appears in
tree view with edit box around it
(click ok)
-- get message "can't be blank (label = null)" and edit box disappears
but 2nd node remains
(click ok)
-- same message and edit box reappears on 2nd node
(click ok)
-- edit box appears around 1st node as well as 2nd
(click ok)
-- edit boxes remain, message window closes

If I click anywhere on or off the form, the message window reappears. If
I close the message window, any further click produce the same behaviour.

If I click the toolbar button again, the whole thing repeats except there
are now 3 nodes in the treeView and so on.

Am I doing something wrong?

All I want to do is add nodes to a list and allow new nodes to be created
while others are still being edited.

Cheers,
..pd.
 
Joined
Jun 20, 2010
Messages
1
Reaction score
0
try this


private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (treeView1.SelectedNode != null)
{
if ((Keys)e.KeyValue == Keys.Space)
{

treeView1.SelectedNode.Toggle();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top