Treeview Aftercheck

P

pbaugh

Hi Folks,
I've got a treeview control (trvRecipes) and am attempting to detect
the 'checking' of a nodes checkbox in order to check all the child
nodes. Ive found some sample code to do precisely this on MSDN, but
for some reason the event never seems to get raised....

private void trvRecipes_AfterCheck(object sender,
TreeViewEventArgs e)
{
// The code only executes if the user caused the checked
state to change.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.Nodes.Count > 0)
{
/* Calls the CheckAllChildNodes method, passing in
the current
Checked value of the TreeNode whose checked state
changed. */
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}

Im sure its just something obvious Im missing.... any ideas??

Many many thanks
Pete
 
C

Chris Shepherd

Hi Folks,
I've got a treeview control (trvRecipes) and am attempting to detect
the 'checking' of a nodes checkbox in order to check all the child
nodes. Ive found some sample code to do precisely this on MSDN, but
for some reason the event never seems to get raised.... [...]
Im sure its just something obvious Im missing.... any ideas??

Do you have the EventHandler defined for the AfterCheck event?

Chris.
 
P

pbaugh

Hi Folks,
I've got a treeview control (trvRecipes) and am attempting to detect
the 'checking' of a nodes checkbox in order to check all the child
nodes. Ive found some sample code to do precisely this on MSDN, but
for some reason the event never seems to get raised.... [...]
Im sure its just something obvious Im missing.... any ideas??

Do you have the EventHandler defined for the AfterCheck event?

Chris.

Hi Chris,
I do but I may well have made a complete mess of it! (Apologies for
being an VB developer on a steep learning curve!)

The code Ive got is :

public partial class frmRecipeImport : Form
{
public event TreeViewEventHandler AfterCheck;
... etc


Wrong place?? wrong code??

Thanks
Pete
 
C

Chris Shepherd

Hi Chris,
I do but I may well have made a complete mess of it! (Apologies for
being an VB developer on a steep learning curve!) [...]

Wrong place?? wrong code??

Heheh, I hear ya. It's the wrong code basically.

If you look in the properties of the TreeView in the designer, there's
the Events listing (lightning bolt in your properties window), which is
one way to get at the events. Double clicking inside the value cell in
that list will either create the event handler for you, or take you to
it in code if you already have one.

In the designer.cs file, you should see a section of code with a comment
of "trvRecipe" at the top, where it assigns a bunch of properties to the
object.
In there, you can add a line like:

trvRecipe.AfterCheck += new TreeViewEventhandler(trvRecipe_AfterCheck);

One thing to watch out for, if you have a habit of cut and pasting form
elements, cutting them will remove all the event subscription code.

Chris.
 
P

pbaugh

Heheh, I hear ya. It's the wrong code basically.

If you look in the properties of the TreeView in the designer, there's
the Events listing (lightning bolt in your properties window), which is
one way to get at the events. Double clicking inside the value cell in
that list will either create the event handler for you, or take you to
it in code if you already have one.

In the designer.cs file, you should see a section of code with a comment
of "trvRecipe" at the top, where it assigns a bunch of properties to the
object.
In there, you can add a line like:

trvRecipe.AfterCheck += new TreeViewEventhandler(trvRecipe_AfterCheck);

One thing to watch out for, if you have a habit of cut and pasting form
elements, cutting them will remove all the event subscription code.

Chris.

What a star! Yeah, I'd completely messed up the event handler. After
following your instructions, the whole thing worked perfectly first
time.

Many thanks!!
Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top