FormClosing委托事件问题
我有两个名为“mainForm”和“addRslt”的表单。这个想法是,当用户单击 mainForm 中的按钮时,addRslt 表单将 Show(),然后用户将填充 TreeView。现在,当用户想要 CLOSE 此 addRslt 表单时,程序将改为 Hide() 表单(使用 e.Cancel = true; ),以便稍后如果用户重新打开它,他/她可以向 TreeView 添加更多内容。
在我的 mainForm 中,我有一个用于显示此 addRslt 表单的按钮,并且在该按钮的单击代码中,还有我的 FormClosing delegte,它将检测 addRslt 表单中的 TreeView 内容并将其复制到 mainForm 中的 TreeView。
现在的问题是我想检查重复的节点,并且不要将它们添加到 mainForm 中的 TreeView 中。这样做是正确的,但我还有一个消息框,告诉用户程序尚未添加现有节点!到目前为止还好..但是问题是每次我这样做时,这个消息框都会出现N+1次!我的意思是,如果我第一次这样做,这个消息框会出现两次等等......
这是我的代码!对不起,故事很长!
private void menuFileAddTestResults_Click(object sender, EventArgs e)
{
addRslt.Show();
addRslt.FormClosing += delegate
{
foreach (TreeNode node in addRslt.treeViewSelectedFiles.Nodes)
{
TreeNode newNode = new TreeNode();
newNode.Text = node.Text;
newNode.Name = node.Name;
newNode.Tag = node.Tag;
if (!treeViewTestFiles.Nodes.ContainsKey(node.Name))
{
treeViewTestFiles.Nodes.Add(newNode);
}
else
{
countExist++;
}
}
if (countExist > 0)
{
MessageBox.Show(countExist.ToString() + " Test files are already exist in the list!");
}
countExist = 0;
};
}
I have two forms named 'mainForm' and 'addRslt'. The idea is when users click on a button in mainForm, the addRslt form will Show() and then user will populate a TreeView. Now when user WANT to CLOSE this addRslt form, program will instead Hide() the form (using e.Cancel = true; ) so later if user reopen this he/she can add more things to the TreeView.
In my mainForm I have a button for showing this addRslt form, and also inside this button's click code, there is my FormClosing delegte which will detect and copy the contents od TreeView in addRslt form to a TreeView in mainForm.
Now the problem is I want to check for duplicated Nodes and do not add them to TreeView in mainForm. This is done right, but I also have a message box that tells the user that program had not added existing nodes! thats ok till now.. BUT problem is with each time I do this, this messagebox will appear N+1 times! I mean if I do it for first time, this message box appears 2 time and etc...
Here is my code! Sorry for long story!
private void menuFileAddTestResults_Click(object sender, EventArgs e)
{
addRslt.Show();
addRslt.FormClosing += delegate
{
foreach (TreeNode node in addRslt.treeViewSelectedFiles.Nodes)
{
TreeNode newNode = new TreeNode();
newNode.Text = node.Text;
newNode.Name = node.Name;
newNode.Tag = node.Tag;
if (!treeViewTestFiles.Nodes.ContainsKey(node.Name))
{
treeViewTestFiles.Nodes.Add(newNode);
}
else
{
countExist++;
}
}
if (countExist > 0)
{
MessageBox.Show(countExist.ToString() + " Test files are already exist in the list!");
}
countExist = 0;
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次显示时都会添加一个
FormClosing
处理程序。当您设置表单的其余部分时,只需添加一次即可。 (就我个人而言,我可能会将其拆分为一个单独的方法...我认为这不是 lambda 表达式的特别合适的使用 - 这是相当大的代码块,它不引用包含方法中声明的任何变量,所以没有真正的好处。)You're adding a
FormClosing
handler every time you show it. Just add it once, when you set up the rest of what the form looks like. (Personally I'd probably split this into a separate method... I don't think it's a particularly appropriate use of a lambda expression - it's a fairly large chunk of code which doesn't refer to any variables declared within the containing method, so there's no real benefit.)看起来您正在重复将内联实现添加到多播委托中。
显然这不是你的意图。您要么需要按照 Jon Skeet 的建议订阅委托的一个实例,要么每次管理订阅。
It looks like you are adding your inline implementation to the multicast delegate repeatedly.
Clearly this is not your intention. You will either need to subscribe one instance of the delegate as Jon Skeet suggests, or manage the subcriptions each time.