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);
}
Related
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);
}
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.
I have this little piece of code inside a project:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click();
}
private void button2_Click(object sender, EventArgs e)
{
x = 0;
}
Now, I want to call the button2_Click method, but I cannot because I have to pass arguments.
The problem is, I don't know what arguments to pass when calling the method.
What should I pass?
use like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click(this.button2,EventArgs.Empty);
}
private void button2_Click(object sender, EventArgs e)
{
x = 0;
}
just treat it as normal parameter. but it's not recommand like it. the best Practice is Extract the body of button2_Click and call the Extract method.the button2_Click should alway be trigger by user Interface.for example :
private void textBox1_TextChanged(object sender, EventArgs e)
{
method();
}
private void button2_Click(object sender, EventArgs e)
{
method();
}
public void method(){
//x=0 or other
....
}
Nevermind, I just used:
button2_Click(sender, e)
and it worked.
Let's say I have the following code:
private void btnArr1(object sender, EventArgs e)
{
wr("test string1","st1");
}
private void btnArr2(object sender, EventArgs e)
{
wr("test string2","st2");
}
private void btnArr3(object sender, EventArgs e)
{
wr("test string3","st3");
}
private void wr(String str, String st){
switch(st){
case "st1":{
st1.Add(str);
break;
}
case "st2":{
st2.Add(str);
break;
}
case "st3":{
st3.Add(str);
break;
}
}
}
Is it somehow possible to minimize that code?
For example Change "st1.Add(str)" to something like "st".Add(str).
The only difference between those buttons is the list name where the information is stored. Everything else is equal.
You need to use a Dictionary<String, List<String>>.
In your constructor (or other initializationary area), add a new List<string>() for each key that you need.
You can then write
dict[someKey].Add(something);
Well if these are all in the same class, you can just use:
private void btnArr1(object sender, EventArgs e)
{
st1.Add("test string1");
}
private void btnArr2(object sender, EventArgs e)
{
st2.Add("test string2");
}
private void btnArr3(object sender, EventArgs e)
{
st3.Add("test string3");
}
If that doesn't work for you, please give details as to why it wouldn't work.
Hard to say without knowing the type of st1, st2, etc. but something like this?
private void btnArr1(object sender, EventArgs e)
{
wr("test string1",st1);
}
private void btnArr2(object sender, EventArgs e)
{
wr("test string2",st2);
}
private void btnArr3(object sender, EventArgs e)
{
wr("test string3",st3);
}
private void wr(String str, List<string> st){
st.Add(str);
}
I need to know if it's possible to call the Click of a button from another one.
private void myAction_Click(object sender, EventArgs e)
{
// int x;
// ...
}
private void Go_Click(object sender, EventArgs e)
{
// call the myAction_Click button
}
Thanks.
You want:
private void Go_Click(object sender, EventArgs e)
{
myAction_Click(sender, e);
}
But a better design is to pull the code out:
private void myAction_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Go_Click(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// Your code here
}
The button has a PerformClick method.
http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx