C
C#2y ago
?

❔ Windows Forms different controls but same event doing different code?

private void OnSecondsChange(object sender, EventArgs e)
{
textBoxUpTime.Text += $"{sender} fired out"; // somehow distinguish the sender and behave differently later in the code
}
private void OnSecondsChange(object sender, EventArgs e)
{
textBoxUpTime.Text += $"{sender} fired out"; // somehow distinguish the sender and behave differently later in the code
}
I was wondering if there is a way I can link one method to multiple controls events (such as OnClick) and depending on which element fired the event do something different? For example there are 4 buttons and each has the same OnClick event method linked but I want the code for each button behave slightly different
27 Replies
ero
ero2y ago
link a different method to the different buttons? having a method do different things depending on what fired the event just doesn't make any sense
?
?OP2y ago
Yeah, but let's say I have 10 buttons and for each the code is almost the same, just a different value (different seconds amount)
ero
ero2y ago
mh, i see sounds more like a job for like a NumericUpDown and a single button maybe you can do if (sender == _button1)?
?
?OP2y ago
Thanks! I was thinking about that but I wasn't sure why I can't access properties of the sender. It's because it's of object type and not the control type, I correcred it:
if(sender.GetType().Equals(typeof(ToolStripMenuItem)))
pingingTimeout = Convert.ToUInt16(((ToolStripMenuItem) sender).Tag);
if(sender.GetType().Equals(typeof(ToolStripMenuItem)))
pingingTimeout = Convert.ToUInt16(((ToolStripMenuItem) sender).Tag);
I put seconds value for each initiator into Tag property, and here in the code I access it. Thanks! catlurk
ero
ero2y ago
i don't understand why you're doing this? did if (sender == _button1) not work?
?
?OP2y ago
What is _button1 ? An instance of Button class?
ero
ero2y ago
yeah just any of your buttons and if you truly need to stick to this, use
if (sender is ToolStripMenuItem tsmi)
{
pingingTimeout = (ushort)(tsmi.Tag);
}
if (sender is ToolStripMenuItem tsmi)
{
pingingTimeout = (ushort)(tsmi.Tag);
}
?
?OP2y ago
Actually... that also works HmmNoted However with my approach I don't need to check for each instance, just one if() statement if it belongs to that class
ero
ero2y ago
hm using Tag is very bad practice
?
?OP2y ago
Yeah but if I have them 5 I'd put 5 if() statements Because? I just need anything holding a value
ero
ero2y ago
what? no you don't? the code i sent is equivalent to yours
?
?OP2y ago
What is tsmi? An additional instance (copy) of passed object?
ero
ero2y ago
a copy, cast to the type, if it is that so only if sender is not null, and it is an instance of a ToolStripMenuItem, it creates the tsmi local variable and then you can use that
?
?OP2y ago
Cool, that also works. And your code looks better, thanks! What about the Tag property? Why I shouldn't use it and what I should instead?
ero
ero2y ago
i could go back to this. it sounds like you should use this instead or maybe a combobox with the different values but it's always better to be explicit in your code the control's Tag property isn't really the place to store anything in (i don't know why it exists at all) so if you have a lot of if statements, so be it. if you don't like that, give each control its own handler
?
?OP2y ago
NumericUpDown - I rather want give the user a choice between a couple of options rather than entering some (either sane or insane) value ComboBox - I can't put a text after it, it's an element of MenuStrip
ero
ero2y ago
what? the combobox part? what do you mean by "put a text after it"
?
?OP2y ago
Look, this looks just better rather than a box to enter a value. If I'd do that then I'd put "seconds" after it to make a sense if users is entering seconds, minutes or hours
ero
ero2y ago
alright well, this isn't really my fault you said buttons a button is fundamentally different from what you're showing
?
?OP2y ago
As an example, it didn't matter what is triggering event method
ero
ero2y ago
it does matter, evidently
?
?OP2y ago
Yeah because I'm making MenuStrip elements
ero
ero2y ago
obviously that can't be replaced with a numupdown or a cbox and if i had known that, i wouldn't have suggested it
?
?OP2y ago
It can, but it looks like on the previous screenshot, less comfortable Alright. Anyway, thanks for your help 😀
ero
ero2y ago
that's bad ux anyway never seen a combobox in a context menu
?
?OP2y ago
Me neither, I don't wanna do it
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server