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
Related
I have a userControl and I've a button there, I'd like to call event when I'm clicking on the button in my main form from userControl. I do this:
UserControl
public UserControlerConstructor()
{
_button.Click += new EventHandler(OnButtonClicked);
}
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public event ButtonClickedEventHandler OnUserControlButtonClicked;
private void OnButtonClicked(object sender, EventArgs e)
{
// Delegate the event to the caller
if (OnUserControlButtonClicked != null)
OnUserControlButtonClicked(this, e);
}
Form
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
It doesn't work because when I click in the form do nothing in the form code, but it does in userControl code. But I'd like to do in form code. I don't know how to call event from userControl to the form.
Well now I don't know if you're explicity want to use the delegate, no? If not, why don't you just do:
public Form1()
{
userControlInstance._button.Click += OnUCButtonClicked;
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
up to now your code does not compile. You are using the wrong event handler type. It should show the following compiler error:
EventHandler cannot be converted to ButtonClickedEventHandler
Do the following steps:
1) put the declaration of the delegate outside of the class UserControlerConstructor:
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public partial class UserControlerConstructor: UserControl
{
1) then change the type of the handler when registering the event in Form:
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new ButtonClickedEventHandler(OnUCButtonClicked);
}
This way it should work
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
}
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 have made a custom dialog window that inherits ChildWindow
public partial class InputWindow : ChildWindow
{
public InputWindow()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("clicked");
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
this.OKButton_Click(this, new RoutedEventArgs());
}
}
When I press enter in the tetxbox the event OKButton_Click gets fired ( because message box appears). However, the code (Add Folder) in the event handler below that exists in another class does not get fired! even though the message box appears! Why is this so? and How can I fix it?
InputWindow win = new InputWindow();
win.Title = "Enter New Folder Name";
win.OKButton.Click += (s, e) =>
{
if (!string.IsNullOrWhiteSpace(win.inputTextBox.Text))
{
AddNewFolder(win.inputTextBox.Text);
win.DialogResult = true;
}
};
win.Show();
You're just calling OKButton_click directly from your KeyDown event handler. That's not the same as raising the Click event on the OK button itself - it's just a method call. So it's no wonder that other event handlers for OKButton.Click aren't being called.
I don't know of any way of manually raising the Click event yourself. It sounds like really you should have one common method which is called from both the Click event handler and the KeyDown event handler.
Im making a userControl named [File_Manager] and i was wondering if i can add a button to this custom control that i can set its job later after adding this custom control to another form .. something like
File_Manager fManager = new File_Manager();
fManager.SetFreeButtonJob( MessageBox.Show("Hello") ); // something like this.
then whenever user press that button .. the messageBox shows up.
So.. Is it possible to do that?
thanks in advance.
Sure you can. Just attach the buttons click handler to the action you pass in.
fManager.SetFreeButtonJob(() => MessageBox.Show("Hello"));
private void SetFreeButtonJob(Action action)
{
button1.Click += (s,e) => action();
}
Just note that passing in the Action breaks the encapsulation of user control though. You should probably do something like SetFreeButtonJob(Jobs.SayHello); and put the knowledge of what to do inside the control.
Create a custom event for your UserControl and fire it when your Button is clicked. You can then attach an event handler to the custom event in your Form. Or you can just raise the UserControl's Click Event when your Button is clicked.
public delegate void CustomClickEventHandler(object sender, EventArgs e);
public partial class buttonTest : UserControl
{
public event CustomClickEventHandler CustomClick;
public buttonTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CustomClick(sender, e);
}
}
and in your Form
public Form1()
{
InitializeComponent();
buttonTest1.CustomClick +=new CustomClickEventHandler(userControl1_ButtonClick);
}
private void userControl1_ButtonClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
Or as my second option try.
private void button2_Click(object sender, EventArgs e)
{
OnClick(e);
}
and in your Form subscribe to the UserControl's Click event.
buttonTest1.Click +=new EventHandler(buttonTest1_Click);
private void buttonTest1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Again");
}