I've seen lots of posts asking a question similar to this, but none seem to answer the question. I have a TreeView of vendors like this:
Soda
Regular
SmallCan
SmallBottle
Diet
SmallCan
Water
Regular
EcoBottle
I created a context menu that allows the user to rename the selected node, but cannot find a way to enforce that if it makes a duplicate node name, either the change is refused or the node text is reverted to the previous value. This is the context change event and the method to handle the enforcing:
private void contextMenuRename_Click(object sender, System.EventArgs e)
{
restoreNode = treProducts.SelectedNode;
treProducts.LabelEdit = true;
if (!treProducts.SelectedNode.IsEditing)
{
treProducts.SelectedNode.BeginEdit();
}
enforceNoTreeDuplicates();
}
private void enforceNoTreeDuplicates()
{
nodeNames.Clear();
if (treProducts.SelectedNode.Level != 0)
{
foreach (TreeNode node in treProducts.SelectedNode.Parent.Nodes)
{
nodeNames.Add(node.Text);
}
}
else
{
foreach (TreeNode node in treProducts.Nodes)
{
nodeNames.Add(node.Text);
}
}
int countDuplicates = 0;
foreach (string nodeName in nodeNames)
{
if (restoreNode.Text == nodeName)
{
countDuplicates++;
}
if (countDuplicates > 1)
{
treProducts.SelectedNode = restoreNode;
}
}
}
However, the BeginEdit() doesn't seem to run if the enforceNoTreeDuplicates() method is in there. Is there a better way to handle the editing of the selected node or is there something wrong with the enforceNoTreeDuplicates() method?
Generally, you would use the AfterLabelEdit for that, which has an option to cancel the edit:
void treProducts_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
foreach (TreeNode tn in e.Node.Parent.Nodes) {
if (tn.Text == e.Label) {
e.CancelEdit = true;
}
}
}
Related
I am creating A WPF application and am currently having issues with updating the visuals for a particular instance.
I have a ViewModel.Textlines which I am trying to change one element within that. It works and behaves fine from what I gather.
I am using Remove and Insert and it doesnt work. I use breakpoints and find out it all seems to work and the element has indeed been swapped out and when I check the OC it will show the values I wanted it to have and OnPropertyChanged Has been called after but it fails to update that. It might occure because the value is not a 'new' MFD_textline as I am just assigning it via =
Below is the code I am using
private void Controller_UpdateEvent(object sender, UpdateEventArgs e)
{
if(e.SystemName == "ALE47")
{
if(e.Values.ContainsKey("PageIndex") && e.Values.ContainsKey("Value") && e.Values.ContainsKey("TextLine"))
{
int pageIndex = (int)e.Values["PageIndex"].NewValue;
int value = (int)e.Values["Value"].NewValue;
textLine = new MFD_Textline();
textLine = (MFD_Textline)e.Values["TextLine"].NewValue;
ViewModel.TextLines.RemoveAt(value);
ViewModel.TextLines.Insert(value, textLine);
}
}
}
public void TextLineChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// when the textlines obserevable collection is changed (added to or removed) force a property changed to update the view
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (TextLines.Count == 11)
{
OnPropertyChanged("textLines");
}
}
}
private ObservableCollection<MFD_Textline> textLines;
public ObservableCollection<MFD_Textline> TextLines
{
get { return textLines; }
set
{
textLines = value;
OnPropertyChanged("textlines");
}
}
Please let me know if there are any questions or if I have not quite explained it right.
This call is wrong OnPropertyChanged("textlines");
You should pass the Property's name, not the backing field's
OnPropertyChanged("TextLines");
I have 1 root node and many child nodes of that root node.
I want to get all of the visible nodes key.
The recursive code block like below;
public void PrintNodesRecursive(UltraTreeNode oParentNode)
{
foreach (UltraTreeNode oSubNode in ultraTree1.Nodes[0].Nodes)
{
MessageBox.Show(oSubNode.Key.ToString());
PrintNodesRecursive(oSubNode);
}
}
private void ultraButton3_Click(object sender, EventArgs e)
{
PrintNodesRecursive(ultraTree1.Nodes[0]);
}
However messagebox always show me '1' value. It doesn't count and endless loop happens.
How can I make it happen?
Try like this;
public void PrintNodesRecursive(UltraTreeNode oParentNode)
{
if (oParentNode.Nodes.Length == 0)
{
return;
}
foreach (UltraTreeNode oSubNode in oParentNode.Nodes)
{
if(oSubNode.Visible)
{
MessageBox.Show(oSubNode.Key.ToString());
}
PrintNodesRecursive(oSubNode);
}
}
Also, put the visible condition in the loop.
You made a simple programming error. This line:
foreach (UltraTreeNode oSubNode in ultraTree1.Nodes[0].Nodes)
should probably be
foreach (UltraTreeNode oSubNode in oParentNode.Nodes)
Otherwise, every recursion step starts again from the top.
My point is to display number of all child nodes at StatusStripLabel. My point is that I want StatusStripLabel to get updated everytime number of child nodes get's changed - I'll add or delete some of already exists. First, I placed code in Public Form, but it didn't worked as I expected. After some time I came with idea that actually works: I placed the code inside button method. But after that I realized that I'll need to place it in second place, in case of deleting node. So My question is: is there anything that can make it simplier? If my explanation isn't enough, just tell me, I'll try my best.
Code from Public Form (Because I want that counter to work from the beggining, not after I'll press the button)
childNodeCounter();
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
Method:
public void childNodeCounter()
{
NodeCounter = 0;
foreach (TreeNode RootNode in treeView1.Nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
{
NodeCounter++;
}
}
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
}
Code inside button method:
private void button1_Click(object sender, EventArgs e)
{
NodeCounter = 0;
foreach (TreeNode RootNode in treeView1.Nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
{
NodeCounter++;
}
}
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
}
Edit: Thanks to mr. Hans Passant I wrote this and it works very well:
public int childNodeCounter(TreeNodeCollection nodes)
{
int count = 0;
foreach (TreeNode RootNode in nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
count++;
}
return count;
Event handler looks like this:
toolStripStatusLabel1.Text = "Number of games in database: " + childNodeCounter(treeView1.Nodes);
Three tiny optimizations
Rather that iterate the tree yourself, just use ChildNode.Nodes.GetNodeCount
Rather than repeat the same logic in different places, have your button Click events simply call the UpdateNodeCount() method.
The text initializer in the first code fragment is redundant, and can be eliminated: the call to childNodeCounter already does the status label update.
The natural way to traverse a tree structure is by using recursion. That's always a bit hard to reason through but there are lots of resources available. Doing it iteratively is a lot uglier, you have to use a Stack<> to allow you to backtrack again out of a nested node. I'll therefore post the recursion solution:
private static int CountNodes(TreeNodeCollection nodes) {
int count = nodes.Count;
foreach (TreeNode node in nodes) count += CountNodes(node.Nodes);
return count;
}
Then your event handler becomes:
private void button1_Click(object sender, EventArgs e) {
toolStripStatusLabel1.Text = "Number of games in database: " +
CountNodes(treeView1.Nodes);
}
If you're adding and removing "game" nodes to the treeView, you must be having methods like void AddGame(string title) and void RemoveGame(string title) which add/remove (child) nodes - those whose total number you want to count. If I understood well, you want toolStripStatusLabel1.Text to be automatically updated each time the number of child nodes changes. In that case you can add field
private int nodesCount;
to your Form class and have something like this:
void AddGame(string title)
{
if(InvokeRequired)
{
Invoke(new MethodInvoker(delegate() { AddGame(title); }));
}
else
{
AddGameNodeToTreeView(title); // add new game node to desired place in TreeView
nodesCount++; // increase node counter
toolStripStatusLabel1.Text = "Number of games in database: " + nodesCount;
}
}
RemoveGame() would be implemented in the same way (or joined with AddGame() into a single method with one additional argument - bool add). Both methods could be extended if you're adding/removing multiple nodes in which case you'll be passing title array and updating nodesCount accordingly.
The advantage of this approach is that you don't have to count nodes in the tree each time before updating toolStripStatusLabel1.Text. Also, toolStripStatusLabel1.Text is updated automatically, not only when user clicks the button.
Drawback is that nodesCount is somewhat redundant information: total number of nodes of interest is 'hidden' in the treeView. You have to make sure that nodesCount is in sync with the actual number of nodes.
Okay, I have a TreeView that serves as a directory tree for Windows. I have it loading all the directories and functioning correctly, but it is pausing my GUI while it loads directories with many children. I'm trying to implement multithreading, but am new to it and am having no luck.
This is what I have for my TreeView:
private readonly object _dummyNode = null;
public MainWindow()
{
InitializeComponent();
foreach (string drive in Directory.GetLogicalDrives())
{
DriveInfo Drive_Info = new DriveInfo(drive);
if (Drive_Info.IsReady == true)
{
TreeViewItem item = new TreeViewItem();
item.Header = drive;
item.Tag = drive;
item.Items.Add(_dummyNode);
item.Expanded += folder_Expanded;
TreeViewItemProps.SetIsRootLevel(item, true);
Dir_Tree.Items.Add(item);
}
}
}
private void folder_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)sender;
if (item.Items.Count == 1 && item.Items[0] == _dummyNode)
{
item.Items.Clear();
try
{
foreach (string dir in Directory.GetDirectories(item.Tag as string))
{
DirectoryInfo tempDirInfo = new DirectoryInfo(dir);
bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);
if (!isSystem)
{
TreeViewItem subitem = new TreeViewItem();
subitem.Header = tempDirInfo.Name;
subitem.Tag = dir;
subitem.Items.Add(_dummyNode);
subitem.Expanded += folder_Expanded;
subitem.ToolTip = dir;
item.Items.Add(subitem);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Whenever I expand a directory that has a large number of subdirectories, the program appears to be frozen for a few seconds. I would like to display a loading message or animation while it's processing, but I'm not sure how to begin with multithreading. I know I have to use the TreeView's Dispatcher.BeginInvoke method, but other than that I'm kinda lost.
Any help would be greatly appreciated!!!
One of the easiest ways to start a async process is to use an anonymous delegate with BeginInvoke. As an example you could move your code in the constructor to a separate method (say RenderTreeView) and then call it asynchronously to begin a new thread as follows:
Action action = RenderTreeView;
action.BeginInvoke(null, null);
The trick to this is that any time you interact with any UI elements from the async process you need to rejoin the main UI thread, otherwise you will get an exception about cross thread access. This is relatively straight forward as well.
In Windows Forms it's:
if (InvokeRequired)
Invoke(new MethodInvoker({item.Items.Add(subitem)}));
else
item.Items.Add(subitem);
In WPF it's:
if (!Dispatcher.CheckAccess())
Dispatcher.Invoke(new Action(() => item.Items.Add(subitem)));
else
item.Items.Add(subitem);
You really need to break up the code to make it more flexible in terms of methods. At the moment everything is bundled in one method which makes it hard to work with and re-factor for async processes.
Update Here you go :)
public partial class MainWindow : Window
{
private readonly object dummyNode = null;
public MainWindow()
{
InitializeComponent();
Action<ItemCollection> action = RenderTreeView;
action.BeginInvoke(treeView1.Items, null, null);
}
private void RenderTreeView(ItemCollection root)
{
foreach (string drive in Directory.GetLogicalDrives())
{
var driveInfo = new DriveInfo(drive);
if (driveInfo.IsReady)
{
CreateAndAppendTreeViewItem(root, drive, drive, drive);
}
}
}
private void FolderExpanded(object sender, RoutedEventArgs e)
{
var item = (TreeViewItem) sender;
if (item.Items.Count == 1 && item.Items[0] == dummyNode)
{
item.Items.Clear();
var directory = item.Tag as string;
if (string.IsNullOrEmpty(directory))
{
return;
}
Action<TreeViewItem, string> action = ExpandTreeViewNode;
action.BeginInvoke(item, directory, null, null);
}
}
private void ExpandTreeViewNode(TreeViewItem item, string directory)
{
foreach (string dir in Directory.GetDirectories(directory))
{
var tempDirInfo = new DirectoryInfo(dir);
bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);
if (!isSystem)
{
CreateAndAppendTreeViewItem(item.Items, tempDirInfo.Name, dir, dir);
}
}
}
private void AddChildNodeItem(ItemCollection collection, TreeViewItem subItem)
{
if (Dispatcher.CheckAccess())
{
collection.Add(subItem);
}
else
{
Dispatcher.Invoke(new Action(() => AddChildNodeItem(collection, subItem)));
}
}
private void CreateAndAppendTreeViewItem(ItemCollection items, string header, string tag, string toolTip)
{
if (Dispatcher.CheckAccess())
{
var subitem = CreateTreeViewItem(header, tag, toolTip);
AddChildNodeItem(items, subitem);
}
else
{
Dispatcher.Invoke(new Action(() => CreateAndAppendTreeViewItem(items, header, tag, toolTip)));
}
}
private TreeViewItem CreateTreeViewItem(string header, string tag, string toolTip)
{
var treeViewItem = new TreeViewItem {Header = header, Tag = tag, ToolTip = toolTip};
treeViewItem.Items.Add(dummyNode);
treeViewItem.Expanded += FolderExpanded;
return treeViewItem;
}
}
Multithreading may not help much here because the TreeView has to be updated on it's Dispatcher thread.
TreeViews will pause when loading a large number of entries. One way to get around this is to store the contents into an object that mirrored the TreeView structure, and then programmatically load just the first level of the TreeView.
When a user clicks on a node, load the next level of child nodes and expand the node. When that node is collapsed, delete its child nodes to conserve TreeView memory. This has worked well for me. For exceedingly large structures I've used a local Sqlite database (via System.Data.Sqlite) as my backing store and even then the TreeView loaded quickly and was responsive.
You can also look at using BackgroundWorker; that's easiest way of executing an operation on a separate thread(For me :) ).
BackgroundWorker Component Overview: http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
BackgroundWorker Class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
You can use it with Command pattern as explained here -
Asynchronous WPF Commands
Tree View control's AfterCheck event checks all child nodes below it and enables the Run button if something is checked.
1346 void TreeNode_AfterCheck(object sender, TreeViewEventArgs e) {
1347 if (!e.Node.Checked) return;
1348 foreach (TreeNode sub in e.Node.Nodes) {
1349 sub.Checked = e.Node.Checked;
1350 }
1351 RunButton.Enabled = IsANodeChecked();
1352 }
1429 static bool IsANodeChecked(TreeNode node) {
1430 if (node.Checked) return true;
1431 foreach (TreeNode sub in node.Nodes) {
1432 if (IsANodeChecked(sub)) {
1433 return true;
1434 }
1435 }
1436 return false;
1437 }
Checking the root node when there are 4881 sub nodes will hang the GUI for about 7 seconds.
I only need to call IsANodeChecked (on Line 1351) once, but I don't know how to disable it until after all of the tree nodes have been processed.
And I do not want to have a timer on my form devoted to monitoring this.
Does anyone see a simple/obvious solution?
Put an event handler on your checkboxes that enables or disables the RunButton as opposed to having something that iterates over the whole thing to find out.
Add the checkbox to a list of checked checkboxes when it get's checked first so you don't disable the RunButton until the list of checked checkboxes is empty. Remove it from the list when it's unchecked, etc.
Here's kind of how I would write it out, this is just winging it so sorry if I miss something:
private int _checkedCheckboxes;
void AddCheckBox()
{
if (_checkedCheckBoxes++ == 1) RunButton.Enabled = true;
}
void RemoveCheckBox()
{
if (_checkedCheckBoxes-- == 0) RunButton.Enabled = false;
}
void TreeNode_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Checked)
{
AddCheckBox();
return;
}
RemoveCheckBox();
}
I sometimes use a Timer to handle such cases. Add a timer and set up the Tick event handler to call IsANodeChecked and enable/disable the button. Give it a short interval (~100 ms perhaps), and leave it disabled. Then, you call Stop followed by Start on the timer in your AfterCheck event handler. This will cause the timer to be restarted for each call to AfterCheck, but the Tick event handler will be invoked only when a certain time has elapsed after the Start call, which means that it will not be invoked until after the last call to AfterCheck.
100 ms is a very long time for the computer to work, but will seem immediate for the user.
You can see similar behavior in the Windows Explorer. If you use the keyboard to quickly navigate around in the folder tree, the right hand pane with the folder contents will not update unless you stay on a folder in the tree for a brief moment.
These ideas where helpful, but I used something different that worked by adding a single boolean variable:
bool _treeNodeFirst = false;
...and a Before Checked event that temporarily modifies the Back Color on the control to serve as a flag for the control that started the chain of events:
1273 void TreeNode_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
1274 if (!_treeNodeFirst) {
1275 _treeNodeFirst = true;
1276 e.Node.BackColor = Color.Silver;
1277 }
1278 }
1346 void TreeNode_AfterCheck(object sender, TreeViewEventArgs e) {
1347 if (e.Node.Checked) {
1348 foreach (TreeNode sub in e.Node.Nodes) {
1349 sub.Checked = e.Node.Checked;
1350 }
1351 }
1352 if (e.Node.BackColor == Color.Silver) {
1353 e.Node.BackColor = Color.Empty;
1354 RunButton.Enabled = IsANodeChecked();
1355 _treeNodeFirst = false;
1356 }
1357 }
1429 static bool IsANodeChecked(TreeNode node) {
1430 if (node.Checked) return true;
1431 foreach (TreeNode sub in node.Nodes) {
1432 if (IsANodeChecked(sub)) {
1433 return true;
1434 }
1435 }
1436 return false;
1437 }
This seems to be the best way (that I can see right now) to ensure that IsANodeChecked(TreeNode) is only run once when a group of nodes is selected all at once.
I do, however, really like Jimmy Hoffa's idea of using a count, though. I will probably add that to my code.
Thanks to all!
~Joe