In my case i have 8 checkbox, each checkbox has click event, if checkbox is clicked, that will call same method like this.
public void _setting(CheckBox check)
{
...
}
And this is the code how click event call the method
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(check1);
}
How to simplify the click event to call method? Or i have to call like in check1_click until check8?
private void check2_Click(object sender, RoutedEventArgs e)
{
_setting(check2);
}
private void check3_Click(object sender, RoutedEventArgs e)
{
_setting(check3);
}
...
private void check8_Click(object sender, RoutedEventArgs e)
{
_setting(check8);
}
or can it be simplified?
I think so. It seems that check1..check8 are the controls being clicked. In that case, you could assign all events to a single event handler and use the sender:
private void check_Click(object sender, RoutedEventArgs e)
{
_setting((CheckBox)sender);
}
If sender is not the control you expect, possibly Source or e.OriginalSource is.
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(sender as CheckBox);
}
This way if you call _setting and sender is not a checkbox, it will be null, not Exception.
Related
Hi could some kind person help. I have two user controls. One with a textbox, the other with a Combobox. The Main window will perform calculation routine as soon as combos and textboxes are modified.
The Textbox version works, the ComboBox doesn't. The only difference I can see is
Textbox uses TextChangedEventArgs
whereas
Combobox uses System.EventArgs
Any ideas?
Thanks
// UserControl - with TextBox
public event RoutedEventHandler ucTextChanged;
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (ucTextChanged != null)
{
ucTextChanged(this, new RoutedEventArgs());
}
}
private void txtValue_TextChanged(object sender, TextChangedEventArgs e)
{
OnTextChanged(sender, e);
}
// UserControl - ComboBox
public event RoutedEventHandler ucComboChanged;
private void OnComboChanged(object sender, RoutedEventArgs e)
{
if (ucComboChanged != null)
{
ucComboChanged(this, new RoutedEventArgs());
}
}
private void ucCombo_DropDownClosed(object sender, System.EventArgs e)
{
OnComboChanged(sender, e);
}
Try looking at the event SelectionChanged of the ComboBox ( https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox.onselectionchanged )
XAML:
<ComboBox SelectionChanged="ucCombo_SelectionChanged"></ComboBox>
C#:
private void ucCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// your code here...
OnComboChanged(sender, e);
}
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
I'm working on a WinForms app. My ComboBox has DropDownClosed event, but I need to fire this event from a Button. How can I do this?
Like this:
private void button1_Click(object sender, EventArgs e)
{
comboBox1_DropDownClosed(sender, e);
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
Please take a look , I believe this is what you are talking about
private void abc_Click(object sender, RoutedEventArgs args)
{
}
private void xyz_Click(object sender, RoutedEventArgs args)
{
abc_Click(sender, args);
}
How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method?
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
// call SubGraphButton-Click().
}
You can easily do it by the following piece of code (assuming that name of your button is btnButton):
btnButton.PerformClick();
You can call the button_click event by simply passing the arguments to it:
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
you can call the button_click event by passing..
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
Also without passing..
private void SubGraphButton_Click(object sender, EventArgs args)
{
}
private void Some_Method() //this method is called
{
SubGraphButton_Click(new object(), new EventArgs());
}
You can perform different approaches to work around this. The best approach is, if your both buttons are suppose to do the same job, you can define a third function to do the job. for example :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void myJob()
{
// Your code here
}
but if you are still persisting on doing it in your way, the best action is :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click.PerformClick();
}
In WPF, you can easily do it in this way:
this.button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Usually the better way is to trigger an event (click) instead of calling the method directly.
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click Succes");
}
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
PictureBox1_Click(sender, e); //or try this one "this.PictureBox1_Click(sender, AcceptButton);"
}
}
You can simply call it:
SubGraphButton_Click(sender, args);
Now, if your SubGraphButton_Click does something with the args, you might be in trouble, but usually you don't do anything with them.
For me this worked in WPF
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
RoutedEventArgs routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent, Button_OK);
Button_OK.RaiseEvent(routedEventArgs);
}
}
Use InvokeOnClick event. it works even if the button is invisible/disabled
A simple way to call it from anywhere is just use "null" and "RoutedEventArgs.Empty", like this:
SubGraphButton_Click(null, RoutedEventArgs.Empty);
For WPF:
YourButtonName.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));
Add it to the instance of the Click delegate:
ChildNode.Click += SubGraphButton_Click
which is inkeeping with the pattern .NET events follow (Observer).
For people wondering, this also works for button click.
For example:
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Test")
}
private void txb_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
btn_Click(sender, e);
}
When pressing Enter in the textfield(txb) in this case it will click the button which will active the MessageBox.
we have 2 form in this project.
in main form change
private void button1_Click(object sender, EventArgs e)
{
// work
}
to
public void button1_Click(object sender, EventArgs e)
{
// work
}
and in other form, when we need above function
private void button2_Click(object sender, EventArgs e)
{
main_page() obj = new main_page();
obj.button2_Click(sender, e);
}
I am having a problem in calling multiple buttons at the same time because each buttons works a different process there are more than 78 folders.
I want to call all the buttons at the same time in a single button called button4. Now it's calling button1 only and not working for button2.
Is there any way to call these buttons at the same time?
My code is:
private void button4_Click_1(object sender, EventArgs e)
{
button1.PerformClick();
button2.PerformClick();
}
Thanks in Advance.
You should in general not perform UI-style clicks on other buttons in order to invoke their behaviour.
Just call the respective event handling methods of the buttons you would like to "click".
example code:
private void button4_Click_1(object sender, EventArgs e)
{
button1_Click_1(null, EventArgs.Empty);
button2_Click_1(null, EventArgs.Empty);
// and so on
}
You should refactor the other events to call well-named methods.
Say button1 does some initialization; it should look like this:
private void button1_Click(object sender, EventArgs e)
{
Initialize();
}
Say button2 finalizes that intialization; it should look like this:
private void button2_Click(object sender, EventArgs e)
{
FinalizeInitialization();
}
Then if button4 does all of this; it should look like this:
private void button4_Click(object sender, EventArgs e)
{
Initialize();
FinalizeInitialization();
WhateverElseButton4ShouldDo();
}
Under most circumstances, you shouldn't call PerformClick() at all. Instead, you should call the same methods your event handlers call. So, if clicking button 3 should behave as click clicking button 1 and then button 2, you should have code like this:
private void button1_Click(object sender, EventArgs e)
{
SomeAction();
}
private void button2_Click(object sender, EventArgs e)
{
AnotherAction();
}
private void button3_Click(object sender, EventArgs e)
{
SomeAction();
AnotherAction();
}
(As a side note, your buttons should have descriptive names, not button1 and the like.)
We can't say what those button click handlers do. So it's hard to say what's wrong. But try moving the code away from button click handlers. Create some class that contains code that should run after button click. Then call this class' methods from button click handlers. It will be easier to debug and test that code.
public class ButtonActions
{
public void DoSomething() {...}
public void DoSomething2() {...}
public void DoSomething3() {...}
public void DoAll()
{
DoSomething();
DoSomething2();
DoSomething3();
}
}
// here instead of clicking all buttons call method that does it all
protected void button_Click(object sender, EventArgs e)
{
var buttonActions = new ButtonActions();
buttonActions.DoAll();
}