views:

210

answers:

4

So in my documentation it says:

"C# public event TreeViewPlusNodeCheckedEventHandler NodeChecked()

You can use this event to run cause a method to run whenever the check-box for a node is checked on the tree."

So how do I add a method to my code behind file that will run when a node is checked? The method I want to run is:

protected void TOCNodeCheckedServer(object sender, TreeViewPlusNodeEventArgs args)
{

    TreeViewPlusNode aNode = args.Node;
    if (!aNode.Checked)
        return;
    List<string> BaseLayers = new List<string>();
    _arcTOCConfig.BaseDataLayers.CopyTo(BaseLayers);

    List<MapResourceItem> mapResources = new List<MapResourceItem>();

    if (BaseLayers.Contains(aNode.Text))
    {
        foreach (BaseDataLayerElement anEl in _arcTOCConfig.BaseDataLayers)
        {
            if (!aNode.Text.Equals(anEl.Name))
            {
                if (aNode.TreeViewPlus.Nodes.FindByValue(anEl.Name).Checked)
                {
                    aNode.TreeViewPlus.Nodes.FindByValue(anEl.Name).Checked = false;
                    aNode.TreeViewPlus.Nodes.FindByValue(anEl.Name).Refresh();
                    MapResourceItem aMapResource = this.Map1.MapResourceManagerInstance.ResourceItems.Find(anEl.Name);
                    aMapResource.DisplaySettings.Visible = false;
                    this.Map1.RefreshResource(anEl.Name);
                    mapResources.Add(aMapResource);
                    this.Map1.MapResourceManagerInstance.ResourceItems.Remove(aMapResource);
                }
                else
                {
                    MapResourceItem aMapResource = this.Map1.MapResourceManagerInstance.ResourceItems.Find(anEl.Name);
                    mapResources.Add(aMapResource);
                    this.Map1.MapResourceManagerInstance.ResourceItems.Remove(aMapResource);
                }

            }
        }

        foreach (MapResourceItem aMapResource in mapResources)
        {
            int count = this.Map1.MapResourceManagerInstance.ResourceItems.Count - 1;
            this.Map1.MapResourceManagerInstance.ResourceItems.Insert(count, aMapResource);
            this.Map1.MapResourceManagerInstance.CreateResource(aMapResource);
        }
        this.Map1.InitializeFunctionalities();
        this.Map1.Refresh();
    }
}

vs 2008 c# .net 3.5

+5  A: 

You need to assign a delegate to the event and have it run the method you want. Something like :

TreeViewControl.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer)

DaveK
+2  A: 

Just add a handler to the event.

myTreeView.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer);

or (because instantiating the TreeViewPlusNodeCheckedEventHandler isn't actually necessary)

myTreeView.NodeChecked += TOCNodeCheckedServer;
Max Schmeling
+2  A: 

This is a standard case of registering a handler for an event

treeView.NodeChecked += TOCNodeCheckedServer;
Jonathan
Where do I put the line? outside of the method?
mrjrdnthms
Anywhere where you have access to the treeView variable. For a TreeView that was created at design time, the constructor or Load hander for the Form would work.
Jonathan
A: 

On your initialise method for the form add

TOCTree.NodeChecked += new TreeViewPlusNodeCheckedEventHandler (TOCNodeCheckedServer);

This will tell your app to run TOCNodeCheckedServer when the TOCNode Fires the NodeChecked Event.

There are loads of resources on the web explaining how this works. Check out http://www.csharphelp.com/archives/archive253.html as an example.

Kaius
Forgive my ignorance but when I look at my .cs file I don't see a method that initializes the form. What am I missing?
mrjrdnthms
Is there a method on the form which runs first ? A load method without seeing your code its difficult to tell but you should have a method which is the entry point for the form.
Kaius