SupaOden
SupaOden

Reputation: 742

event not being fired

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();

Upvotes: 0

Views: 271

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 3

Related Questions