You're creating a new B
object every time your combobox is changed. That adds a new handler every single time, without ever removing the previous handlers. You need to either unsubscribe the old handler when you go to add a new one, or only ever add one handler and simply modify the data that it relies on so that that handler acts appropriately when it fires.
Listening event outside MainPage class calling method multiple times on button click event (repetetively)
-
30-06-2023 - |
Pregunta
I have a situation where I have to manage the button click event outside MainPage
class (suppose in Class B
). Here is the code that I'm using:
namespace TEST
{
public partial class MainPage: UserControl
{
public event Action simpleEvent;
public MainPage()
{
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (comboBox1.SelectedItem as string)
{
case "UInt8":
B obj4 = new B(this, fileContent, "UInt8");
break;
case "UInt16":
B obj1 = new B(this, fileContent, "UInt16");
break;
case "UInt32":
B obj2 = new B(this, fileContent, "UInt32");
break;
case "UInt64":
B obj3 = new B(this, fileContent, "UInt64");
goto
default;
default:
MessageBox.Show("check");
break;
}
}
private void ButtonClick_EVENT(object sender, RoutedEventArgs e)
{
// I try to listen outside the class this way.
if (simpleEvent != null)
simpleEvent();
}
}
Class B
{
public B(MainPage Obj, byte[] fileContent, string type)
{
switch (type = Obj.comboBox1.SelectedItem as string)
{
case "UInt8":
{
//do something
break;
}
case "UInt16":
{
//do something
break;
}
case "UInt32":
{
//do something
break;
}
case "UInt64":
{
//do something
goto
default;
}
default:
{
break;
}
}
Obj.simpleEvent += () = > MyMethod(Obj);
}
public void MyMethod(MainPage Obj)
{
// This MessageBox repeats 2 times on second time button click
// and 3 times on 3rd button click .
// It should be called once even on second/third/fourth etc. button clicks.
MessageBox.Show("Repetition check");
}
}
}
The problem is my method is called repetitively. For example, when I click the button the first time it will be called only once (which is good). and if I click the button again the message box pop-up comes up 2 times and again pop-ups 3 times on 3rd attempt.
What I want is to pop-up this message box only once on the click (even if I keep on clicking on the button one after the other click).
EDIT: The new updated code is the actual situation i was trying to make it short so last time i didn't include to much code (because i was not suspecting that part).
What i am trying to do is :
I have to select 1 data type among 4 given data type at run time (uint64/32/16/8)(which will be data type for reading a file ("fileContent" in constructor call), but it's not related to question i asked, so leaving too much discussion on it).After selecting that data type i click the button
to display something. (so each time i first i select "data type" from combo box
and then i press button
to display some data corresponding to that data type then i again select another data type from combo box and then again i press "button" but this time it pop-ups message 2 times, and when i repeat the same 3rd time it pop-ups messagebox 3times).
Suppose first i selected "uint64
" in combo box at run time (in the first attempt) and then i click button to display messagebox it will pop-up the message box once.Now without closing the application my second selection to combo box is on uint32
(On second attempt) and again i click to "button" this time this message box is popped 2 times. I don't know why? and on third attempt it pop-ups 3 times.
(1) Why message box repeats ? Thanks for the help in advance, I really wanna know the reason for it, not able to understand yet.
Solución