Problem Editing TreeView Labels

  • Thread starter Thread starter Peter D. Dunlap
  • Start date Start date
P

Peter D. Dunlap

I'm working on a Windows Forms app that includes a TreeView control.
The TreeView is set to allow label editing. If I click twice on a
label I can edit it fine. I also have a "Rename Client" item on a
context menu, and if I pick that I can edit the label.

However, at the next level up I have a context menu item "Add New
Client", which adds a client with a default name to the database and
to the TreeView, and then is supposed to select the new item and put
it in edit mode, much like adding a new folder in Windows Explorer.
However, it refuses to go into edit mode.

A trimmed down snippet of my code (the handler for the context menu
event):


private void AddClient_Click(object sender, EventArgs e)
{
// ... code to set up entry in database (works fine) ...

TreeNode nd = new TreeNode("New Client");
nd.ImageIndex = 1;
nd.SelectedImageIndex = 1;
ndContext.Nodes.Add(nd);

tvMain.SelectedNode = nd;
if (! nd.IsEditing)
{
nd.BeginEdit(); // It never goes into edit...
}
}


ndContext is set on a click event on the TreeView as the node last
clicked. The new node is being inserted in the correct place and is
being selected, but it does not go into edit mode. If I click on it
after this code runs, THEN it goes into edit mode.

I tried replacing the "if" with a "while", and it locks up. No matter
how many times I do the BeginEdit() call, it just does not go into
edit mode.

(The code is actually edited a bit - the node I'm actually using is
derived from TreeNode and just adds an ID field which is returned by
the database routine.)

Pete Dunlap
 
I'm working on a Windows Forms app that includes a TreeView control.
The TreeView is set to allow label editing. If I click twice on a
label I can edit it fine. I also have a "Rename Client" item on a
context menu, and if I pick that I can edit the label.

However, at the next level up I have a context menu item "Add New
Client", which adds a client with a default name to the database and
to the TreeView, and then is supposed to select the new item and put
it in edit mode, much like adding a new folder in Windows Explorer.
However, it refuses to go into edit mode.

A trimmed down snippet of my code (the handler for the context menu
event):


private void AddClient_Click(object sender, EventArgs e)
{
// ... code to set up entry in database (works fine) ...

TreeNode nd = new TreeNode("New Client");
nd.ImageIndex = 1;
nd.SelectedImageIndex = 1;
ndContext.Nodes.Add(nd);

tvMain.SelectedNode = nd;
if (! nd.IsEditing)
{
nd.BeginEdit(); // It never goes into edit...
}
}


ndContext is set on a click event on the TreeView as the node last
clicked. The new node is being inserted in the correct place and is
being selected, but it does not go into edit mode. If I click on it
after this code runs, THEN it goes into edit mode.

I tried replacing the "if" with a "while", and it locks up. No matter
how many times I do the BeginEdit() call, it just does not go into
edit mode.

(The code is actually edited a bit - the node I'm actually using is
derived from TreeNode and just adds an ID field which is returned by
the database routine.)

Pete Dunlap

Never mind...

In the BeforeEdit event I'm making sure that ndContext is an editable
node. The node where the "Add Client" option comes up isn't. I set
ndContext to the new node and now it edits fine...

Pete
 
Back
Top