c# - FormClosing delegate event problem -
i have 2 forms named 'mainform' , 'addrslt'. idea when users click on button in mainform, addrslt form show() , user populate treeview. when user want close addrslt form, program instead hide() form (using e.cancel = true; ) later if user reopen he/she can add more things treeview.
in mainform have button showing addrslt form, , inside button's click code, there formclosing delegte detect , copy contents od treeview in addrslt form treeview in mainform.
now problem want check duplicated nodes , not add them treeview in mainform. done right, have message box tells user program had not added existing nodes! thats ok till now.. but problem each time this, messagebox appear n+1 times! mean if first time, message box appears 2 time , etc...
here code! sorry 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 exist in list!"); } countexist = 0; }; }
you're adding formclosing handler every time show it. add once, when set rest of form looks like. (personally i'd split separate method... don't think it's particularly appropriate use of lambda expression - it's large chunk of code doesn't refer variables declared within containing method, there's no real benefit.)
Comments
Post a Comment