c# trigger/call events from another control - c#

In VFP9, if i have two buttons (butt1 & butt2) If i want to trigger butt2 click event from butt1 i just do this from butt1 click event butt2.click() and it will call whatever code i have in the procedure/event in butt2. How do i do that in c#?
How do I call event of a certain control from another control like
below
.
private void button2_Click(object sender, EventArgs e)
{
button1.click()
}

You can call the handler directly like this button1_Click(button1, null);
Example Usage:
Button button1;
Button button2;
public Form1()
{
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
}
void button2_Click(object sender, EventArgs e)
{
button1_Click(button1, null);
}
public void button1_Click(object sender, EventArgs e)
{
//Action when click occurs
}

Why are you trying to simulate button click, if you just want to execute some logic?
Wrap this logic into separate method, an call it from click handlers you choose:
private Foo1() {}
private Foo2() {}
private button1_Click(object sender, EventArgs e)
{
Foo1();
Foo2();
}
private button2_Click(object sender, EventArgs e)
{
Foo2();
}

just bind the button1 click event to button2 click event in form_load or designer.csbutton2.Click+=new EventHandler(button1_Click);

Related

How to programmatically invoke the Click event handler of a TextBox

I know how to programmatically invoke the event handler of a Button:
button1.PerformClick();
I would like to do the same for the Click event handler of a TextBox. The problem is that TextBox does not have a
textBox1.PerformClick();
I suggest method extraction (why should we mix UI - windows messages and Business Logic):
//TODO: put a better name here
private void onMyTextBoxClick() {
//TODO: relevant code here
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick();
}
Then you can just call onMyTextBoxClick:
...
// Same business logic as if MyTextBox is clicked
onMyTextBoxClick();
...
Edit: If you really want EventArgs aruments, just provide them:
//TODO: put a better name here
private void onMyTextBoxClick(TextBox box, EventArgs e) {
//TODO: relevant code here
}
// Default EventArgs
private void onMyTextBoxClick(TextBox box) {
onMyTextBoxClick(box, EventArgs.Empty);
}
// Both TextBox and EventArgs are default ones
private void onMyTextBoxClick() {
onMyTextBoxClick(MyTextBox, EventArgs.Empty);
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick(sender as TextBox, e);
}
Usage:
// Default EventArgs
onMyTextBoxClick(myTextBox);
// Custom EventArgs
EventArgs args = ...
onMyTextBoxClick(myTextBox, args);

Binding Form Closing event in class

Hello I'm trying to make a class that can handle the form closing event for my winForm
I have figured out how to work the event handlers, like this for a ContextMenuStrip items click event:
mnuItemShow.Click += new EventHandler(mnuItemShow_Click);
private void mnuItemShow_Click(object sender, EventArgs e)
{
}
But I can't figure out how to bind the Form Closing event..
I have tried it like this:
this.form.FormClosing += new EventHandler(closing);
private override void closing(EventArgs e)
{
}
But I get this error message:
No overload for 'closing' matches delegate 'System.EventHandler'
This works:
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Main_FormClosing);
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
}
it should be like this:
private void closing(object sender, FormClosingEventArgs e)
{
}
and you have to add handler like below
frm.FormClosing += new FormClosingEventHandler();

How to call LostFocus event of another control from the other control

Calling the LostFocus event of control from other control
Example. : I want to call LostFocus event of Button1 button from the Gotfocus event of Button2 button.
code is :-
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.gotFocus // this line giving error.
}
Use following code:
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.RaiseEvent(new RoutedEventArgs(LostFocusEvent, button1));
}
private void button1_LostFocus(object sender, RoutedEventArgs e)
{
}
If this doesn't solve your problem, post your code and state your problem and purpose clearly so that you can get better solution.
you can just call event handler method of Button1's LostFocus event in button2_GotFocus :
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(this.button1, null);
}
Try this
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(sender,e)
}

button.PerformClick(); not working in Form Load

I'm calling a button click in the form_load like this:
public void Form1_Load(object s, EventArgs e)
{
button.PerformClick();
}
But upon loading the button does not get clicked, what am I doing wrong?
You can write whatever you want to do inside of click in another function and call that from inside the click handler or programmatically like this -
public void Form1_Load(object s, EventArgs e)
{
//button.PerformClick();
PerformClickAction();
}
void button_click(object sender,EventArgs e)
{
PerformClickAction();
}
void PerformClickAction()
{
// Write what you need to do on click
}
This works for me:
public void Form1_Load(object s, EventArgs e){
button.PerformClick();
}
Looks like you didn't register the Form1_Load as event handler for the Load event of your form. Try this:
public Form1(){
InitializeComponent();
Load += Form1_Load;//Register the event handler so that it will work for you.
}
To get the button clicked on form load, you need to fire an event after the form is loaded, try this
public Form1()
{
InitializeComponent();
//Event fired
this.Load += new System.EventHandler(this.button1_Click);
}
//Event Handler
private void button1_Click(object sender, EventArgs e)
{
//do something
}

C# .net framework2.0 how to dynamically assign methods to an event

VS2008, C#, .NET FRAMEWORK2.0
I want this: click button1, webbrowser1._DocumentCompleted() event revokes doA(); click button2, it revokes doB(); click button3, it revokes doC().
I know how to do it using JAVA and I guess C# has this mechanism too. Could anyone give me some idea or better, show me some example?
myButton.Click += myButton_Click;
protected void myButton_Click(object sender, EventArgs e) {}
To Add a handler
button.Click += buttonClickEventHandler;
To remove a handler
button.Click -= buttonClickEventHandler;
To add to these answers, you can also add an anonymous method to an event:
myButton.Click += (object sender, EventArgs e) => {
MessageBox.Show("MASSIVE ERROR!");
};
What this means is that you can effectively call a method even if it does not match the appropriate event handler signature:
myButton.Click += (object sender, EventArgs e) => {
DoA();
};
Or (without using a lamba expression):
myButton.Click += delegate(object sender, EventArgs e) {
DoA();
};
If you want to add event handlers to a control, which is what I think you are describing, you can easily do this. One common approach is to assign control event handlers in the code behind during page load:
protected void Page_Load(object sender, EventArgs e)
{
//add the event handler for the click event. You could provide other
//logic to determine dynamically if the handler should be added, etc.
btnButton1.Click += new EventHandler(btnButton1_Click);
btnButton2.Click += new EventHandler(btnButton2_Click);
btnButton3.Click += new EventHandler(btnButton3_Click);
}
protected void btnButton1_Click(object sender, EventArgs e)
{
//get the button, if you need to...
Button btnButton1 = (Button)sender;
//do some stuff...
DoA();
}
protected void btnButton2_Click(object sender, EventArgs e)
{
//do some stuff...
DoB();
}
protected void btnButton3_Click(object sender, EventArgs e)
{
//do some stuff...
DoC();
}
private void DoA() {}
private void DoB() {}
private void DoC() {}
Declaring an event
public class MyClass1
{
...
public event EventHandler<EventArgs> NotifyValidate;
protected void RaiseNotifyValidate(EventArgs e)
{
if (NotifyValidate != null)
{
NotifyValidate(this, e);
}
}
...
}
Firing that event in your code
...
RaiseNotifyValidate(new EventArgs()); //EventArgs could be more sophisticated, containing data etc..
Registering for that event in your code:
...
MyClass aObj = new MyClass();
aObj.NotifyValidate += new EventHandler(onNotifyValidate);
...
private void onNotifyValidate(object sender, EventArgs e)
{
//do what you need to
}
As Dan pointed out, with Lambda expressions you can define events like
aObj.NotifyValidate += (s,ev) =>
{
//handle your event
};

Categories

Resources