Set up single event handler for multiple buttons in .NET? - c#

An application I'm writting in silverlight/c# consists of 13 permanent buttons that when clicked perform a simple navigation to another page.
The problem is my code behind has 13 different event handlers(alot of code) for a nearly identical purpose.
Is there a way to detect which button was pressed so that a single event handler gets fired, and a simple if statement within could determine which page to go to?

yes: you can use the same method for all buttons, and use the parameter "sender" as "sender.Name" to get the name of the pressed button.

In the designer code of your program, tack on the same event handler for all 13 buttons (look for the code that has += and put the same event handler for all of them).
Notice that the event handler has an object (s) parameter. You can use this parameter as follows:
if(s.Name = "Button1") {//button 1 stuff}
else if (s.Name = "Button2") {button 2 stuff}
etc..
EDIT: should have been s.Name = "Button1, 2, 3, etc.."

Test the sender parameter of the button click event handler - you'll be able to test which button was the sender.

Use a Dictionary using 'sender' as key. The 'value' could be the page to navigate to.

If you have lots of code in your event handler you should break that out to a separate method anyway and send the button specific parameters to that method.
But you can still have one event handler if you look at the sender argument.

Related

C# how to unsubscribe all event handlers of a control (RadioButton)

I have a GUI that contains 10 radio buttons and a textbox, into which the user has to make his input. The RadioButton.CheckedChanged event gets subscribed on runtime depending on the user input (textbox). So, in my case there over 50 possible methods that can subscribe to the CheckedChanged event. Ok, I can unsubscribe every single method one by one (see excerpt from my code), but isn't there are any less time consuming and more efficent way to do this? Something like for instance:
unsubscribe all methods from the CheckedChanged event at once. Or: determine the method that currently subscribes to this event und unsubscribe this method.
I was already searching for an existing solution and found this: How to remove all event handlers from an event but in my case this solution does not works. As I mentioned, in my case there radio buttons, not normal buttons.
Excerpt from my code:
public List<RadioButton> RbList
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
RbList = new List<RadioButton>
{
radioButton1, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7,
radioButton8, radioButton9, radioButton10
};
}
private void Unsubscribe()
{
for (int i = 0; i < RbList.Count; i++)
{
RbList.ElementAt(i).CheckedChanged -= Method1;
RbList.ElementAt(i).CheckedChanged -= Method2;
...
}
}
Additional information regarding to the asked questions:
The last subscribed method does not get unsubscribed. With the radio buttons specific PDFs gets called. In the background is still the last subscribed method as long as another method is not get subscribed. So, if the user change the input in the textbox and the input does not matched anymore so that any other method can't be subscribed, the last method ist still there and the with the method associated PDFs can be accessed and that's exactly what I want to stop. I wouldn't have the problem, if the PDFs could by only accessed by clicking on the radio button. Then I could simply make all the radio buttons unvisible/unaccessible. But this is not the case, there is another option to the user to call the PDFs, which I dont mentioned - additionally there is a second textbox and if the input in this textbox is equal to the RadioButton.text the associated radio button gets also checked and the associated PDF to this radio button gets called and this is where the problem starts.

what is the difference between Control.OnClick and Control.InvokeOnClick

i'm just wondering what the difference is between Control.OnClick and Control.InvokeOnClick methods.
the second thing i wonder ise, when it is asp.net they talk about 3 componenets: Click event, OnClick method, and button1_click eventhandler. but when it is desktop appcliatons they talk about only 2 componenets, Click event and its eventhandler. why is the onclik(invokeonclick) not mentioned at all?
OnClick allows you to add a handler for when the Click event occurs on the control.
InvokeOnClick will "Raise the Click event for the specified control".
So if you have assigned a handler to OnClick and then call InvokeOnClick on that control then your handler will be called.
Web is different. This answer may help you for invoking click on a 'web' button.

C#.NET mnemonics unexpected behaviour

I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.

Can we Fire the button CLick event on Windowload In WPf C#

i want to FIre the button Click event When My Window is Loaded..
How Can i Achieve it in Wpf?
Create a single function with the shared behavior in your window, then call that function from both your loaded handler and your click handler.
As per this blog post in WinForms this was really easy by just calling PerformClick(), but in WPF you can do it with Automation, however as a commenter mentioned it's really easy if you have access to the button to just use RaiseEvent.
someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
But as previously answered, if you only have a single handler that needs to be notified, then simply call that handler method directly.
You could use Automation to do it aswell - I've seen this suggested some places as the more flexible/robust method to use, but it seems a bit heavy weight to me compared to just calling the method you already have directly.
in you page_loaded event handler method, make a call to the click event like this:
_buttonName_click(sender, new RoutedEventArgs())
Trgger this event on Button whic u wanted to click
button.performclick();

I have to click twice to fire an event

I'm developing a windows form application in C# and (SOMETIME) every button in the form need to be clicked twice to fire an event.
Can anybody suggest a solution?
Best Regards,
BaDoOoReY
Its a win form application.
I think you have handled button_doubleClicked event instead of Button_Clicked event.
Please make sure that you handle Button_Clicked event.
If this is not the case then can you please share your code ??
You can create a puplic variable with 0 value.
When the user click the button for the first time you give the varriable value 1.
When the user click the button for the second time you give the varriable value 2.
and you make if statement to check if the varriable is equal 2, this mean that the button clicked twice, if true you can fire your event.
Also you can you use button double_cilck event.
I think roundtrip is not happening some times because of any ajax control or javascript.If you use any of these two please check this may stops events to fire server side some times.

Categories