❔ Windows Forms different controls but same event doing different 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
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
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)
mh, i see
sounds more like a job for like a NumericUpDown and a single button
maybe you can do
if (sender == _button1)
?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:
I put seconds value for each initiator into Tag property, and here in the code I access it. Thanks! i don't understand why you're doing this?
did
if (sender == _button1)
not work?What is _button1 ? An instance of Button class?
yeah
just any of your buttons
and if you truly need to stick to this, use
Actually... that also works However with my approach I don't need to check for each instance, just one
if()
statement if it belongs to that classhm
using
Tag
is very bad practiceYeah but if I have them 5 I'd put 5
if()
statements
Because? I just need anything holding a valuewhat? no you don't?
the code i sent is equivalent to yours
What is tsmi? An additional instance (copy) of passed object?
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 thatCool, 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?
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
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
what?
the combobox part?
what do you mean by "put a text after it"
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
alright well, this isn't really my fault
you said buttons
a button is fundamentally different from what you're showing
As an example, it didn't matter what is triggering event method
it does matter, evidently
Yeah because I'm making MenuStrip elements
obviously that can't be replaced with a numupdown or a cbox
and if i had known that, i wouldn't have suggested it
It can, but it looks like on the previous screenshot, less comfortable
Alright. Anyway, thanks for your help 😀
that's bad ux anyway
never seen a combobox in a context menu
Me neither, I don't wanna do it
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.