如何使用事件使方法运行?

时间:2023-02-09 10:12:07

So in my documentation it says:

所以在我的文档中它说:

public event TreeViewPlusNodeCheckedEventHandler NodeChecked()

公共事件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

vs 2008 c#.net 3.5

4 个解决方案

#1


5  

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

您需要为事件分配一个委托,并让它运行您想要的方法。就像是 :

TreeViewControl.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer)

TreeViewControl.NodeChecked + = new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer)

#2


2  

Just add a handler to the event.

只需为事件添加处理程序即可。

myTreeView.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer);

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

或(因为实际上不需要实例化TreeViewPlusNodeCheckedEventHandler)

myTreeView.NodeChecked += TOCNodeCheckedServer;

#3


2  

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

这是注册事件处理程序的标准情况

treeView.NodeChecked += TOCNodeCheckedServer;

#4


0  

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.

当TOCNode触发NodeChecked事件时,这将告诉您的应用运行TOCNodeCheckedServer。

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

网上有大量资源解释了它的工作原理。以http://www.csharphelp.com/archives/archive253.html为例。

#1


5  

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

您需要为事件分配一个委托,并让它运行您想要的方法。就像是 :

TreeViewControl.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer)

TreeViewControl.NodeChecked + = new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer)

#2


2  

Just add a handler to the event.

只需为事件添加处理程序即可。

myTreeView.NodeChecked += new TreeViewPlusNodeCheckedEventHandler(TOCNodeCheckedServer);

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

或(因为实际上不需要实例化TreeViewPlusNodeCheckedEventHandler)

myTreeView.NodeChecked += TOCNodeCheckedServer;

#3


2  

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

这是注册事件处理程序的标准情况

treeView.NodeChecked += TOCNodeCheckedServer;

#4


0  

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.

当TOCNode触发NodeChecked事件时,这将告诉您的应用运行TOCNodeCheckedServer。

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

网上有大量资源解释了它的工作原理。以http://www.csharphelp.com/archives/archive253.html为例。