如果form的acceptbutton,cancelbutton不设置的话,在textbox中键入enter,escape就会发出Beep响声.
在keypress事件,或者processdialogkey中可以通过屏蔽enter,escape消息的办法来解决.
请问,有什么办法能只去掉Beep响声,而不屏蔽enter,escape消息?

解决方案 »

  1.   

    KeyDown事件
    e.Handled = true;
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            TextBox TB = new TextBox();
                TB.Parent = this;
                TB.KeyDown += new KeyEventHandler(TB_KeyDown);
            }        void TB_KeyDown(object sender, KeyEventArgs e)
            {
                e.SuppressKeyPress = e.KeyData == Keys.Enter || e.KeyData == Keys.Escape;
            }
        }
    }
      

  3.   


     // KeyPress事件处理器
     private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
     {
         //设置按Enter键和Esc键不发出Beep声音
         if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Escape)
         {
             e.Handled = true;
         }
     }
      

  4.   

    完整程序,既没有声音又不屏蔽
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            TextBox TB = new TextBox();
                TB.Parent = this;
                TB.KeyDown += new KeyEventHandler(TB_KeyDown);
                TB.KeyPress += new KeyPressEventHandler(TB_KeyPress);
            }        void TB_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (Char)Keys.Enter || e.KeyChar == (Char)Keys.Escape)
                    MessageBox.Show("Enter或Escape键");
            }        void TB_KeyDown(object sender, KeyEventArgs e)
            {
                e.SuppressKeyPress = e.KeyData == Keys.Enter || e.KeyData == Keys.Escape;
                if (e.SuppressKeyPress)
                    TB_KeyPress(sender, new KeyPressEventArgs((Char)e.KeyData));
            }
        }
    }
      

  5.   

    void TB_KeyPress(object sender, KeyPressEventArgs e)
            {
                this.Text = ((int)e.KeyChar).ToString(); 
            }
    这个听更清楚点,messagebox也有beep