Here's the problem. I have a TreeView that is essentially a listing of
directories and files. If you right-click on a directory then a
context menu comes up and one of the options is "New Folder", when the
user clicks that it expands the current node, creates and add's a child
node, then calls BeginEdit() on the child node.
I have written a custom class that implements IComparer to sort on
rules that I have defined. The problem comes in the fact that
BeginEdit() appears to be an async call, and as far as I can tell,
there's no event that gets fired to let me know when it is done so that
I can then call Sort() on the TreeView. There is an 'AfterLabelEdit'
event on the TreeView, but it appears that the actual label on the node
isn't changed until after that event is finished firing. I've tried
manually setting the text of the node in an event handler for that
event, but it causes the TreeView to do wierd things (like the node is
still editable after I finished editing it, and if this is the second
or so folder I've created, the previous one I create is being
reassigned the same name as the one I created. If anyone can help me,
I'd appreciate it.
Code:
================================================
private void contextMenuFolder_Click(object sender, EventArgs e)
{
TreeNode newFolder = new TreeNode("", FOLDER_CLOSED,
FOLDER_OPEN);
tvwReports.SelectedNode.Nodes.Add(newFolder);
tvwReports.SelectedNode.Expand();
CatalogItem item = new CatalogItem();
item.Type = ItemTypeEnum.Folder;
newFolder.Tag = item;
newFolder.BeginEdit();
}
private void tvwReports_BeforeLabelEdit(object sender,
NodeLabelEditEventArgs e)
{
if (e.Node.Tag != null)
{
if ((e.Node.Tag as CatalogItem).Type !=
ItemTypeEnum.Folder)
e.CancelEdit = true;
else
e.CancelEdit = false;
}
else
e.CancelEdit = true;
}
private void tvwReports_AfterLabelEdit(object sender,
NodeLabelEditEventArgs e)
{
this.Cursor = Cursors.WaitCursor;
string fullPath =
e.Node.FullPath.Substring(e.Node.FullPath.IndexOf('/'));
if ((e.Node.Tag as CatalogItem).Name == null)
{
if (e.Label == null || e.Label == "")
//this is a new folder and no name was given,
delete the node
e.Node.Remove();
else
{
//get path as relative to the webservice
fullPath += e.Label;
//new folder node, need to call webservice to add
CatalogItemManagement.CatalogItemManager.AddFolder(fullPath);
e.Node.Tag =
CatalogItemManagement.CatalogItemManager.GetCatalogItem(fullPath);
}
}
else if ((e.Node.Tag as CatalogItem).Name != e.Label)
{
if (e.Label == null || e.Label == "")
//the label was left blank, set it back to what it
was before
e.CancelEdit = true;
else
{
//node already existed, but folder name has
changed, need to update
CatalogItemManagement.CatalogItemManager.MoveCatalogItem(fullPath,
fullPath.Replace(e.Node.Text, e.Label));
}
}
this.Cursor = Cursors.Default;
}
================================================
I am using Visual Studio 2005 (with .NET 2.0, of course)