为什么ToolStripMenuItem.Click事件处理程序显示相同的内容?

时间:2019-05-08 08:27:43

标签: c# eventhandler

我有一个树视图,其中有一个名为“ FTPServerListNode”的节点。

“ FTPServerListNode”具有2个FTPServerNode,其中1个是“ serverId1”,其他1个是“ serverId2”。

FTPServerListNode和FTPServerNode都是从TreeNode扩展的。

每个子节点都有一个ToolStripMenuItem。

每个ToolStripMenuItem都有自己的点击事件处理程序。

这些2单击事件处理程序仅显示节点名称。

但是,我发现两个事件处理程序都显示相同的内容(即“ serverId2”)。

这是我的源代码。

using System;
using System.Windows.Forms;

namespace ToolStripMenuItemTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Add(new FtpServerListNode());
        }
    }
    internal class FtpServerNode : TreeNode
    {
        internal FtpServerNode(string serverDesc, string serverId)
        {
            this.Text = serverDesc;
            this.Name = serverId;
        }
    }
    internal class FtpServerListNode : TreeNode
    {
        internal FtpServerListNode()
        {
            FtpServerNode ftpServerNode = new FtpServerNode("serverDesc1", "serverId1");

            var nN= ftpServerNode.Name;
            ToolStripMenuItem removeServerToolStrip = new ToolStripMenuItem();
            removeServerToolStrip.Text = "Remove This FTP Server";
            removeServerToolStrip.Click += (sender, e) => MessageBox.Show(nN);
            ftpServerNode.ContextMenuStrip = new ContextMenuStrip();
            ftpServerNode.ContextMenuStrip.Items.Add(removeServerToolStrip);
            this.Nodes.Add(ftpServerNode);

            ftpServerNode = new FtpServerNode("serverDesc2", "serverId2");
            nN= ftpServerNode.Name;
            removeServerToolStrip = new ToolStripMenuItem();
            removeServerToolStrip.Text = "Remove This FTP Server";
            removeServerToolStrip.Click += (sender, e) => MessageBox.Show(nN);
            ftpServerNode.ContextMenuStrip = new ContextMenuStrip();
            ftpServerNode.ContextMenuStrip.Items.Add(removeServerToolStrip);
            this.Nodes.Add(ftpServerNode);
            this.Text = "Root Node";
            this.Expand();
        }
    }
}

我附上我的项目屏幕供您参考。

enter image description here

2 个答案:

答案 0 :(得分:0)

请将事件处理程序更改为

var mName = ftpServerNode.Name;
removeServerToolStrip.Click += (sender, e) => MessageBox.Show(mName);

您遇到修改后的关闭问题: https://www.jetbrains.com/help/resharper/AccessToModifiedClosure.html

答案 1 :(得分:0)

经过几次试验,以下代码有效:

internal class FtpServerListNode : TreeNode
{
    internal FtpServerListNode()
    {
        SortedDictionary<string, FtpServerNode> ftpServerList = new SortedDictionary<string, FtpServerNode>();
        FtpServerNode ftpServerNode = new FtpServerNode("serverDesc1", "serverId1");
        ftpServerList.Add("serverId1", ftpServerNode);
        ftpServerNode = new FtpServerNode("serverDesc2", "serverId2");
        ftpServerList.Add("serverId2", ftpServerNode);

        foreach (FtpServerNode fn in ftpServerList.Values)
        {
            ToolStripMenuItem removeServerToolStrip = new ToolStripMenuItem();

            removeServerToolStrip.Text = "Remove This FTP Server " + fn.Name;
            removeServerToolStrip.Click += (sender, e) => MessageBox.Show(fn.Name);
            fn.ContextMenuStrip = new ContextMenuStrip();
            fn.ContextMenuStrip.Items.Add(removeServerToolStrip);
            this.Nodes.Add(fn);
        }
        this.Text = "Root Node";
        this.Expand();
    }
}