简体   繁体   English

C#WinForms如何在TreeView中停止Ding声音

[英]C# WinForms how to stop Ding sound in TreeView

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! 如果在TreeView的keydown事件中打开任何模态对话框(或者如果焦点在TreeView上,则在KeyPreview = true的表单中),您将听到恼人的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). 这个ding通常是一个没有处理键事件的信号(比如,默认情况下TextBox会为Ctrl + A等)。 However, setting e.Handled or e.SuppressKeyPress doesnt help in the case of modal dialog in TreeView. 但是,设置e.Handled或e.SuppressKeyPress对于TreeView中的模式对话框没有帮助。 It helps when doing anything EXCEPT opening a modal dialog! 除了打开模态对话框之外,它在执行任何操作时都有帮助!

The native Windows treeview control gets very cranky when you pump a message loop in one of its events. 当您在其中一个事件中抽取消息循环时,本机Windows树视图控件会变得非常难以理解。 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. 使用Control.BeginInvoke()方法很容易。 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()
        ));
    }

Capturing keystrokes with ProcessCmdKey worked for me. 使用ProcessCmdKey捕获击键对我有用 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. 返回true表示按键被表单占用并停止进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM