none
No ding sound after new form RRS feed

  • Question

  • Hi , when i in event keyPress put the enter key 
    a new form will open with ShowDialog command ,
    when window is opened a ding sound will play why?

    I don't want that ding sound

            private void tbGoods_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    e.SuppressKeyPress = true;
                    GoodsSearch oGoodsSearch = new GoodsSearch(tbGoods.Text);
                    oGoodsSearch.ShowDialog();
    }
    }


    Thursday, January 18, 2018 10:28 PM

Answers

  • As I look at this it will work if you don't perform an action such as show a MessageBox or show a Form. So with that known let's move on to KeyPress event and try that.

    using System.Windows.Forms;
    
    namespace SuppressEnter
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBox1.KeyPress += new KeyPressEventHandler(CheckEnterKeyPress);
            }
            private void CheckEnterKeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Return)
                {
                    e.Handled = true;
                    Form2 f = new Form2();
                    f.ShowDialog();
                }
            }
        }
    }
    
    Replace Form2 with your form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 19, 2018 12:50 AM
    Moderator

All replies

  • Try the following, of course add the code for the form too.

    private void TextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
    
            e.SuppressKeyPress = true;
            e.Handled = true;
        }
                
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 18, 2018 10:57 PM
    Moderator
  • Thanks , 

    Don't work

    Thursday, January 18, 2018 11:03 PM
  • As I look at this it will work if you don't perform an action such as show a MessageBox or show a Form. So with that known let's move on to KeyPress event and try that.

    using System.Windows.Forms;
    
    namespace SuppressEnter
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBox1.KeyPress += new KeyPressEventHandler(CheckEnterKeyPress);
            }
            private void CheckEnterKeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Return)
                {
                    e.Handled = true;
                    Form2 f = new Form2();
                    f.ShowDialog();
                }
            }
        }
    }
    
    Replace Form2 with your form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 19, 2018 12:50 AM
    Moderator
  • Hello Armin,

    You could try to use Form.Show instead of Form.ShowDialog(), which will prevent the "ding" sound occurring.

    private void tbGoods_KeyDown(object sender, KeyEventArgs e) {

    ...

    oGoodsSearch.Show(); }


    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 19, 2018 5:28 AM
    Moderator
  • Hi Armin,

    You just put the form open code under TextBox KeyDown event and code that the keypress is already is handled under TextBox KeyPress event will do the trick. Hope this helps you.

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Form2 frm = new Form2();
            frm.ShowDialog();                                
        }
    }
    
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = true;
    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Friday, January 19, 2018 11:43 AM
    Moderator
  • Thanks , It's Work.

    private void tbGoods_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    e.Handled = true;
    
                    GoodsSearch oGoodsSearch = new GoodsSearch(tbGoods.Text);
                    oGoodsSearch.ShowDialog();
    
                }
            }

    Friday, January 19, 2018 1:06 PM
  • It works for me also. For months now it's been nagging me.

    Sunday, March 31, 2019 3:14 PM