A bug in TreeView AfterCheck event?
-
hi! I thing I found a bug in TreeView AfterCheck event but I´m not 100 % sure. this is my event (C#): private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { e.Node.Nodes[1].Checked = true; e.Node.Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[0].Checked = true; e.Node.Nodes[2].Nodes[1].Checked = true; e.Node.Nodes[2].Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[3].Checked = true; e.Node.Nodes[2].Nodes[4].Checked = true; e.Node.Nodes[2].Nodes[5].Checked = true; e.Node.Nodes[2].Nodes[6].Checked = true; e.Node.Nodes[2].Nodes[7].Checked = true; e.Node.Nodes[3].Checked = true; MessageBox.Show("TEST"); } what VS.NEt make: at first it go to e.Node.Nodes[1].Checked = true; then VS.NET go not to e.Node.Nodes[2].Checked = true; VS.NET go again to e.Node.Nodes[1].Checked = true; after this i become a message: An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll Additional information: Specified argument was out of the range of valid values. why that doesn´t work? but it work something like this: private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { e.Node.Nodes[1].ForeColor = Color.Red; e.Node.Nodes[2].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[0].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[1].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[2].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[3].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[4].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[5].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[6].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[7].ForeColor = Color.Red; e.Node.Nodes[3].ForeColor = Color.Red; MessageBox.Show("TEST"); } is it ahain a bug in VS.NET??? Pawel
-
hi! I thing I found a bug in TreeView AfterCheck event but I´m not 100 % sure. this is my event (C#): private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { e.Node.Nodes[1].Checked = true; e.Node.Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[0].Checked = true; e.Node.Nodes[2].Nodes[1].Checked = true; e.Node.Nodes[2].Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[3].Checked = true; e.Node.Nodes[2].Nodes[4].Checked = true; e.Node.Nodes[2].Nodes[5].Checked = true; e.Node.Nodes[2].Nodes[6].Checked = true; e.Node.Nodes[2].Nodes[7].Checked = true; e.Node.Nodes[3].Checked = true; MessageBox.Show("TEST"); } what VS.NEt make: at first it go to e.Node.Nodes[1].Checked = true; then VS.NET go not to e.Node.Nodes[2].Checked = true; VS.NET go again to e.Node.Nodes[1].Checked = true; after this i become a message: An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll Additional information: Specified argument was out of the range of valid values. why that doesn´t work? but it work something like this: private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { e.Node.Nodes[1].ForeColor = Color.Red; e.Node.Nodes[2].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[0].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[1].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[2].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[3].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[4].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[5].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[6].ForeColor = Color.Red; e.Node.Nodes[2].Nodes[7].ForeColor = Color.Red; e.Node.Nodes[3].ForeColor = Color.Red; MessageBox.Show("TEST"); } is it ahain a bug in VS.NET??? Pawel
I was working on a sample in Jesse Liberty's "Programming C#" book today and had the same problem. For some reason the code killed the application without an exception. Basically, it looked as though setting the Checked property fires the AfterCheck event. This causes a recursion that kills the program. This seems to be different that the behavior I am used to (in say VB). Also, remember that arrays start at zero. Nodes[0], Nodes[1], and Nodex[2] for a node with three child nodes. You may also want to protect your code to prevent multiple firing of the AfterCheck event.
Boy. How ya gonna keep 'em down on the farm once they seen Karl Hungus.
- The Dude :cool:
-
I was working on a sample in Jesse Liberty's "Programming C#" book today and had the same problem. For some reason the code killed the application without an exception. Basically, it looked as though setting the Checked property fires the AfterCheck event. This causes a recursion that kills the program. This seems to be different that the behavior I am used to (in say VB). Also, remember that arrays start at zero. Nodes[0], Nodes[1], and Nodex[2] for a node with three child nodes. You may also want to protect your code to prevent multiple firing of the AfterCheck event.
Boy. How ya gonna keep 'em down on the farm once they seen Karl Hungus.
- The Dude :cool:
hi! I have get a replay for me message on an other message board with the solution for this problem. and here is the replay message: Re: A bug in TreeView AfterCheck event? Posted on: April 12 2002 12:10 Pawel, By setting e.Node.Nodes[1].Checked = true; you are firing a new event (because the checked was changed) and displaySaleContractTreeView_AfterCheck will be invoked again even before you come to the second line. I believe you are coming into an infinite loop here. Anyhow, try to disable the AfterCheck event and enable it again at the end of your handler like this: private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { displaySaleContractTreeView.AfterCheck -= new System.Windows.Forms.AfterCheckEventHandler(this.displaySaleContractTreeView_AfterCheck); e.Node.Nodes[1].Checked = true; //.. //... do your stuff here // .. displaySaleContractTreeView.AfterCheck += new System.Windows.Forms.AfterCheckEventHandler(this.displaySaleContractTreeView_AfterCheck); } Hope this works, Viel Spass, Minh. ---------------------------------------------------- wow!! thx!! for every bug has enderminh a solution!! IT´s WORK!!!!!!!!!!!!!!! ;) but some events that you used doesn´t exist. this is the 100% correct code: private void displaySaleContractTreeView_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e) { displaySaleContractTreeView.AfterCheck -= new System.Windows.Forms.TreeViewEventHandler(this.displaySaleContractTreeView_AfterCheck); e.Node.Nodes[1].Checked = true; e.Node.Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[0].Checked = true; e.Node.Nodes[2].Nodes[1].Checked = true; e.Node.Nodes[2].Nodes[2].Checked = true; e.Node.Nodes[2].Nodes[3].Checked = true; e.Node.Nodes[2].Nodes[4].Checked = true; e.Node.Nodes[2].Nodes[5].Checked = true; e.Node.Nodes[2].Nodes[6].Checked = true; e.Node.Nodes[2].Nodes[7].Checked = true; e.Node.Nodes[3].Checked = true; displaySaleContractTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.displaySaleContractTreeView_AfterCheck); } thx again!! DANKE!! ;) Pawel ------------------------------------------- Pawel, No, it's not a bug. It supposed to happen. You have said that "ey, if anyone changes this checkbox, please let me know and run this method". But since you are changing the checkbox inside this own method, it will recursively call this method