❔ 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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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)?
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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?
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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);
}
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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"
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
As an example, it didn't matter what is triggering event method
ero
ero2y ago
it does matter, evidently
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
🩷🐻 𝙎𝙖𝙘𝙠𝙗𝙤𝙮 🐻🩷
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
More Posts
✅ What is better, in this case, Replace() or Substring()In Unity it seems that text input comes with a 'Zero Width Space Character' at the end, and that mes❔ How to pass anchor tag values to controller to render view based on selected valueHI, how do I pass the values assigned to an anchor tag to the controller so that the controller can ❔ How to force method call regardless of type?```cs public class TempBaseClass { } public class TempGenericClass<T> : TempBaseClass { public ❔ Double property with [Required] data annotation does not validateHow can I make it so that the req body has to have a DiscountPercentage field?❔ 'Type may be weakened' inspection in riderIn intelliJ there is an inspection *look at screenshot* for java. Is there an equivalent for it in r❔ Where to learn building APIs ?hey guys can anyone tell me where i can learn building APIs with C#❔ The foreign key property 'UserRole.RoleId1' was created in shadow stateI've expanded the default `IdentityUserRole` implementation for .NET identity like this: ```cs publiPossible to have one controller checking which button is clicked and render view based on it?Hi, I'm not sure if this is possible but say there are 3 big clickable `<a>` tag button, and instead❔ Authorization convention for requests other than getI want to require authorization for all endpoints that accept request types other than get. By lookiSharing elements of 2 arrays into another arraySo i got a char array that consists of the chars **ATG** AAA GGG **TAG** **ATG** AAA **TAG** **ATG*