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_clickdirectly from yourKeyDownevent handler. That’s not the same as raising theClickevent on the OK button itself – it’s just a method call. So it’s no wonder that other event handlers forOKButton.Clickaren’t being called.I don’t know of any way of manually raising the
Clickevent yourself. It sounds like really you should have one common method which is called from both theClickevent handler and theKeyDownevent handler.