How to write just one code block for this two events ?
I would like to execute same code when user minimize, resize or close form.
Write a method for the functionality and call the method from the event handlers.
this.Closing += (sender, e) => this.DoWork();
this.Resize += (sender, e) => this.DoWork();
private void DoWork()
{
// Your code here
}
you can create a function for your common code and call it from whereever you want.
if you want to call it when the form is closed and resized write it as below:
private void Form1_Resize(object sender, EventArgs e)
{
myfunction();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
myfunction();
}
private void myfunction()
{
//function code here
}
Related
how can I assign the Form closing event to the menustrip item click method ?
this.Closing += new CancelEventHandler(this.Form1_Closing);
private void Form1_Closing(object sender, CancelEventArgs e)
{
//
}
private void izlazToolStripMenuItem_Click(object sender, EventArgs e)
{
//
}
thanks
You probably want to close the the Form in click event, so:
private void izlazToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
This will trigger Closing event.
Another way you could hook up the Close method to the Click event on your menu item is through the use of a lambda expression. The following code in the form's constructor demonstrates this:
public Form1()
{
InitializeComponent();
this.izlazToolStripMenuItem.Click += (s, a) => this.Close();
}
More information on lambda expressions can be found here: http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx
When using the windows form app, you can create an event for example a picturebox.Click event. Inside this method all code will be ran when the butten is clicked.
Now inside another event, I can call the method Button1.click, but what is it used for? Can it be used for statements like this?
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(Button1.click == true) // If the button is clicked AND the mouse moves over the picturebox
{
//dance
}
}
No, you'll have to store a state. Where and how you do this can differ, but consider this:
var yourObject = new ObjectYouWishToControl();
...
private void Button1_Click(object sender, MouseEventArgs e)
{
yourObject.Button1WasClicked = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (yourObject.Button1WasClicked)
{
// do your thing
}
}
Simply put, how can I subscribe to and handle the following event?
System.Windows.Forms.Application.Exit(new System.ComponentModel.CancelEventArgs(true));
because apparently this is not going to give me my CancelEventArgs:
Application.ApplicationExit += new EventHandler(ApplicationExitHandler);
private void ApplicationExitHandler(Object sender, EventArgs e)
{
...
}
Try to put Event inside the Form1.Designer.cs
private void InitializeComponent()
{
//.......... UI iniatilization
System.Windows.Forms.Application.ApplicationExit +=new System.EventHandler(ApplicationExitHandler);
}
In Form1.cs
private void ApplicationExitHandler(Object sender, EventArgs 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();
}
I want to call btnDisconnect_Click within btnExit_Click.
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
}
Usually in cases like these I make my click handlers only call another function and pass in appropriate arguments:
private void btnDisconnect_Click(object sender, EventArgs e)
{
DoDisconnect();
}
private void DoDisconnect()
{
...
}
Then I can call that same function from wherever:
private void btnExit_Click(object sender, EventArgs e)
{
DoDisconnect();
}
This way your "disconnect" logic is gummed up by taking dummy arguments that don't actually affect the disconnect behavior in any way.
It also means you can start factoring out view logic from forms.
That depends on if you are using the arguments passed to the event handlers
You could yust call it using nulls
Something like
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
btnDisconnect_Click(null,null);
}
They're just methods. Just call it. You'll need to provide whatever event arguments btnDisconnect_Click is expecting (which is probably nothing). So the simplest thing is:
private void btnExit_Click(object sender, EventArgs e)
{
btnDisconnect_Click(this, EventArgs.Empty);
}
This will pass the current form/window/whatever it is as the sender, and an EventArgs object with no data.
You can call it just as you have it listed. The this below isn't necessary but it puts context on the code:
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
this.btnDisconnect_Click(null, null);
// If you need to have sender as something you can always put
// this in directly
this.btnDisconnect_Click(this.btnDisconnect, new System.EventArgs());
}
I'm going to make an assumption here and say that what you're trying to do is call a Disconnect (perhaps a network resource) for both the disconnect and exit buttons. Instead of calling one event handler method from the other you may want to refactor the disconnect event handler's code into a separate method. Then call that method from both handlers. For example:
private void Disconnect()
{
//Disconnect here
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
//do some other stuff here
Disconnect();
}
private void btnExit_Click(object sender, EventArgs e)
{
//do some other exit stuff here
Disconnect();
}
This makes your code much cleaner and saves you from having to call one event handler from another. This begins to separate your view logic from the rest of your program's logic, which is much more desirable and much easier to maintain in the long run. For instance you may want a separate controller for handling the network resource, instead of embedding it into the view's logic.
In the simplest case you can just call the btnDiconnect_Click directly as follows:
private void btnDisconnct_Click(Object sender, EventArgs e)
{
//Does Something
}
private void btnExit_Click(Object sender, EventArgs e)
{
//Call btnDisconnect_Click()
btnDisconnect_Click(sender, e);
}
You could just call the method passing in valid parameters.
btnDisconnect_Click(btnDisconnect,new EventArgs());
However you might want to consider refactoring out the code in btnDisconnect into a new method and calling that instead:
private void doSomething()
{
//....
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
doSomething();
}
private void btnExit_Click(object sender, EventArgs e)
{
doSomething();
}
{// this is probably your constructor
.
public delegate void MyCustomHandler(object sender, EventArgs e);
.
MyCustomHandler myCustomHandler = new MyCustomHandler(); //you can do more in your delegates constructor, members etc
myCustomHandler += btnExit_Click;
myCustomHandler += btnDisconnect_Click;
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
// do Something
}
private void btnExit_Click(object sender, EventArgs e)
{
// do Something
}
//And wherever you need to invoke these, you do
myCustomHandler(object sender, EventArgs e);