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
Related
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.
I have a TreeView that contains database objects that are basically folders. I want to be able to click on a "folder" in the tree and have it populate a set of controls with data about that "folder". While this all works fine with the code I've written, the issue is that using the arrow keys on the keyboard to go up and down the folder list will eventually hang the application. My assumption is that the background worker I am using to populate the controls is getting hung up.
I've searched and I can't find anything similar to my issue.
Here's my tree view afterselect code.
private void dmTree_AfterSelect(object sender, TreeViewEventArgs e)
{
object[] tagParts = e.Node.Tag as object[];
SelectedFolderNumber = tagParts[1].ToString();
if (!String.IsNullOrEmpty(SelectedFolderNumber) && SelectedFolderNumber != "0")
{
//update mini profile
if (bgwMiniProfile.IsBusy)
{
bgwMiniProfile.CancelAsync();
}
while (bgwMiniProfile.CancellationPending)
{
Application.DoEvents();
}
bgwMiniProfile.RunWorkerAsync();
while (bgwMiniProfile.IsBusy)
{
Application.DoEvents();
}
securityPanel.DisplayTrusteeList(folderTrustees);
}
}
securityPanel is a user control on the form.
Here is the DisplayTrusteeList code
public void DisplayTrusteeList(List<DocumentTrustee> documentTrustees)
{
try
{
dgvTrustees.Rows.Clear();
foreach (DocumentTrustee dt in documentTrustees)
{
dgvTrustees.Rows.Add(imagePG.Images[(int)dt.TrusteeType], dt.GetFullName(dmLogin), dt.AccessRights);
}
foreach (DataGridViewRow myRow in dgvTrustees.Rows)
{
ValidateRights(int.Parse(myRow.Cells["dmRights"].Value.ToString()), myRow);
}
dgvTrustees.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "DisplayTrusteeList");
}
}
And here is the background worker:
private void bgwMiniProfile_DoWork(object sender, DoWorkEventArgs e)
{
if (!bgwMiniProfile.CancellationPending)
{
SetText(txtDocNumber, SelectedFolderNumber);
SetText(txtDocName, Utility.GetProfileValue(adminLogin, SelectedFolderNumber, "DOCNAME"));
SetText(txtClientId, Utility.GetProfileValue(adminLogin, SelectedFolderNumber, "CLIENT_ID"));
SetText(txtClientName, Utility.SetDescription(adminLogin, "CLIENT", txtClientId.Text));
SetText(txtMatterId, Utility.GetProfileValue(adminLogin, SelectedFolderNumber, "MATTER_ID"));
SetText(txtMatterName, Utility.SetDescription(adminLogin, "CLIENT", txtClientId.Text, txtMatterId.Text));
folderTrustees = Utility.GetFolderTrustees(adminLogin, SelectedFolderNumber);
}
else
{
e.Cancel = true;
}
}
I would like to be able to cursor through the tree nodes with the arrow keys and not have the after select code fire until the user lands on a node and stays there for a few seconds. Is that possible?
Thanks and this is my first question. Sorry if the format ins't great. I've used a lot of solutions from here.
I found a better way. Instead of using AfterSelect I'm using NodeMouseClick. This mirrors Windows Explorer functionality. Now the user can cursor up and down the folder tree without any issues. The data to the right will fill in only when they click on the node. This works for me perfectly.
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;
}
}
}
I have two threads which uses the BeginInvoke method to change some Windows Form object's (Panel and Label) visibility attribute to false.The problem is that I'm not sure when the change happens. I can see that the panel is not there (so the BeginInvoke method works) but my if condition to check the visibility status always returns true the first time the form is activated.
bool notVisible = false;
private void LunchMainScreen_Activated(object sender, EventArgs e) {
String CurrentSite = "";
List<DateTime> availableDates = new List<DateTime>();
// Get available dates
Thread availableDatesThread = new Thread(delegate() {
availableDates = LunchUserPreferences.GetUserAvailableDates();
changeObjVisible(notVisible, selectAvailabilityPanel);
changeObjVisible(notVisible, whenLbl);
}
});
availableDatesThread.Start();
// Get user current site
Thread checkSiteThread = new Thread(delegate() {
CurrentSite = LunchUserPreferences.GetUserSite();
changeObjVisible(notVisible, selectSitePanel);
changeObjVisible(notVisible, whereLbl);
}
updateText(CurrentSite, CurrentSiteSetLbl);
});
checkSiteThread.Start();
while (selectSitePanel.Visible == false && selectAvailabilityPanel.Visible == false) {
// it NEVER gets here, even though the panels are NOT visible when the program loads
WhoLunchTable.Visible = false;
WhoLunchTable.SuspendLayout();
listOfAvailableGroups.Clear();
WhoLunchTable.Controls.Clear();
WhoLunchTable.RowStyles.Clear();
PopulateTable();
WhoLunchTable.Visible = true;
WhoLunchTable.ResumeLayout();
break;
}
}
private delegate void changeObjVisibleDelegate(bool visibility, object obj);
private void changeObjVisible(bool visibility, object obj) {
if (this.InvokeRequired) {
this.BeginInvoke(new changeObjVisibleDelegate(changeObjVisible), new object[] { visibility, obj });
return;
}
// downcast to the correct obj
if (obj is Panel) {
Panel panel = (Panel)obj;
panel.Visible = visibility;
}
if (obj is Label) {
Label lbl = (Label)obj;
lbl.Visible = visibility;
}
}
private delegate void updateTextDelegate(string text, Label lbl);
private void updateText(string text, Label lbl) {
if (this.InvokeRequired) {
this.BeginInvoke(new updateTextDelegate(updateText), new object[] { text, lbl });
return;
}
lbl.Text = text;
}
It does work fine when the Form is activated for the second time, for example:
The form loads for the first time and it doesn't go inside the while loop.
I minimize the form/program.
The LunchMainScreen_Activated runs again and it works as it should because it recognises that the panels are not visible.
UPDATE:
I had an idea after reading AlexF answer which solved the problem but it doesn't look like the ideal solution:
I've created a while condition that will only stop when both threads are not alive and an if condition inside it that will get this point in time and execute what I need:
while (availableDatesThread.IsAlive || checkSiteThread.IsAlive) {
// At least one thread is still alive, keeps checking it...
if (!availableDatesThread.IsAlive && !checkSiteThread.IsAlive) {
// Both threads should be dead now and the panels not visible
WhoLunchTable.Visible = false;
WhoLunchTable.SuspendLayout();
listOfAvailableGroups.Clear();
WhoLunchTable.Controls.Clear();
WhoLunchTable.RowStyles.Clear();
PopulateTable();
WhoLunchTable.Visible = true;
WhoLunchTable.ResumeLayout();
break;
}
}
Reading your code the first time the code doesn't enter in the while loop because selectSitePanel.Visible and selectAvailabilityPanel.Visible are true: this is because the availableDatesThread.Start(); and checkSiteThread.Start(); are started but not finished; those two calls are not blocking so the code continues and skips the while.
Meanwhile the two backgrund threads finishes so the second time the "Activated" event is raised the variables values are "correct" (at least for the last cycle).
Without waiting for the threads to finish you are rushing through the code before having a result for the needed value.
In other words, it's better to not use a background thread to update an interface for the use you need.
If you need you may continue to use the code the way you are using it but moving the "while" section in two separate functions: they may be called when the threads have finished their work and refresh the window in this moment and not in the "activate" event.
Background:
In my winforms form, I have a Checked ListView and a "master" checkbox called checkBoxAll.
The behaviour of the master is as follows:
If the master is checked or unchecked, all ListViewItems must change accordingly.
If the user unchecks a ListViewItem, the master must change accordingly.
If the user checks a ListViewItem, and all other ListViewItems are checked aswell, the master must change accordingly.
I have written the following code to mimic this behaviour:
private bool byProgram = false; //Flag to determine the caller of the code. True for program, false for user.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If checkBoxAll is checked, all listviewitems must be checked too and vice versa.
//Check if there are any items to (un)check.
if (myListView.Items.Count > 0)
{
byProgram = true; //Raise flag.
//(Un)check every item.
foreach (ListViewItem lvi in myListView.Items)
{
lvi.Checked = checkBoxAll.Checked;
}
byProgram = false; //Lower flag.
}
}
}
private void myListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
//Get the appropiate ListView that raised this event
var listView = sender as ListView;
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If all items are checked, set checkBoxAll checked, else: uncheck him!
bool allChecked = true; //This boolean will be used to set the value of checkBoxAll
//This event was raised by an ListViewItem so we don't have to check if any exist.
//Check all items untill one is not checked.
foreach (ListViewItem lvi in listView.Items)
{
allChecked = lvi.Checked;
if (!allChecked) break;
}
byProgram = true; //Raise flag.
//Set the checkBoxAll according to the value determined for allChecked.
checkBoxAll.Checked = allChecked;
byProgram = false; //Lower flag.
}
}
In this example, I use a flag (byProgram) to make sure an event was caused by the user or not, thereby preventing an infinite loop (one event can fire another, which can fire the first one again etc. etc.). IMHO, this is a hacky solution.
I searched around but I couldn't find a MSDN documented method to determine if an User Control Event was directly fired thanks to the user. Which strikes me as odd (again, IMHO).
I know that the FormClosingEventArgs has a field which we can use to determine if the user is closing the form or not. But as far as I know, that is the only EventArg that provides this kind of functionality...
So in summary:
Is there a way (other than my example) to determine if an event was fired directly by the user?
Please note: I don't mean the sender of an event! It won't matter if I code someCheckBox.Checked = true; or manually set someCheckBox, the sender of the event will always be someCheckBox. I want to find out if it is possible to determine whether it was through the user (click) or by the program (.Checked = true).
Aaand also: 30% of the time it took to write this question was to formulate the question and the title correctly. Still not sure if it is a 100% clear so please edit if you think you can do better :)
No, there's no practical way to determine whether the change came from GUI or was done by program (in fact, you could analyze the callstack - but that's not recommended because it's very slow and error-prone).
BTW, there's one other thing you could do instead of setting byProgram. You could remove and add the event handler prior or after, respectively, change your controls:
checkBoxAll.CheckedChanged -= checkBoxAll_CheckedChanged;
// do something
checkBoxAll.CheckedChanged += checkBoxAll_CheckedChanged;
Instead of using the changed event, you could use the clicked event to cascade the change through to the relevant controls. This would be in response to a user click, and not the value being changed programatically.
This is something I come across quite a lot and what I tend to try do is not split it between user interaction vs program interaction - I use more generic code i.e. the UI is being updated and doesn't require any events to be handled. I usually package this up through BeginUpdate/EndUpdate methods e.g.
private int updates = 0;
public bool Updating { get { return updates > 0; } }
public void BeginUpdate()
{
updates++;
}
public void EndUpdate()
{
updates--;
}
public void IndividualCheckBoxChanged(...)
{
if (!Updating)
{
// run code
}
}
public void CheckAllChanged(...)
{
BeginUpdate();
try
{
// run code
}
finally
{
EndUpdate();
}
}