0
votes

If you open any modal dialog inside a keydown event of a TreeView (or in the Form with KeyPreview=true if focus is on a TreeView) you will hear an annoying DING!

How do i prevent it from happening?

This ding usually is a signal that key event wasnt handled (like, by default TextBox will ding for Ctrl+A etc). However, setting e.Handled or e.SuppressKeyPress doesnt help in the case of modal dialog in TreeView. It helps when doing anything EXCEPT opening a modal dialog!

2
Can't reproduce this. Sound is not playing for displaying form via ShowDialog. It plays for MessageBox. But thats another story.Sergey Berezovskiy

2 Answers

3
votes

The native Windows treeview control gets very cranky when you pump a message loop in one of its events. The standard solution is to delay the processing of the event until all the events are complete. Easy to do with the Control.BeginInvoke() method. Worked in this case too:

    private void treeView1_KeyDown(object sender, KeyEventArgs e) {
        e.Handled = e.SuppressKeyPress = true;
        this.BeginInvoke(new Action(() => 
            (new Form1()).ShowDialog()
        ));
    }
0
votes

Capturing keystrokes with ProcessCmdKey worked for me. Override this method of your form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.O | Keys.Control))
    {
        openFileDialog1.ShowDialog();
        return true;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

Return true to show that keystroke was consumed by form and stop further processing.