none
SendKeys and Bell

    Question

  • Hello,

    I am writting a windows application in C# for a client who wants his users to be able to use the "Enter" key to move between controls. Some of the controls on the form are textboxes and I have used the following code. Other controls on the form are dateTimePicker controls and comboBox controls

    private void txtYards_KeyPress(object sender, KeyPressEventArgs e)

    {

    if (e.KeyChar == 13)

    {

    SendKeys.Send("{TAB}");

    e.Handled = true;

    }

    }

     

    Other controls on the form are dateTimePicker controls and comboBox controls and I have use the following code for these controls.

    private void cbTruckNumber_KeyDown(object sender, KeyEventArgs e)

    {

    if (e.KeyCode == Keys.Enter)

    {

    SendKeys.Send("{TAB}");

    e.Handled = true;

    }

    }

     

    Question 1: Do I have to use the KeyPress eventhandler for textBox controls and the KeyDown eventhandler for comboBox and dateTimePicker controls? The reason why I ask is that if I use the KeyPress eventhandler on a comboBox the SendKeys does not work. Why is this?

     

    Question 2: On some of my forms whenever I use the SendKeys.Send() for textBox controls the system makes a beep sound. I thought the e.Handled() prevented the sound, however I am still getting a sound when I go from a comboBox control to a textBox. It only makes the sound apon entering the textBox control not when it leaves. Any suggestions would be greatly appreciated. Thanks.

     

     

    Friday, August 31, 2007 5:32 PM

Answers

  • You'll want to handle this before the keystroke reaches the control so you don't have to customize every control.  Check this thread.
    Friday, August 31, 2007 6:00 PM
  •  

    Hi McGray,

    KeyEventArgs.Handled Property indicates whether to bypass the control's default handling(your ComboBox’s KeyDown event) and the KeyEventArgs.SuppressKeyPress Property indicates whether to prevent the KeyPress event from happening for the current control(your ComboBox’s KeyPress event).

    Hope this helps.
    Best regards.

    Wednesday, September 05, 2007 5:38 AM

All replies

  • You'll want to handle this before the keystroke reaches the control so you don't have to customize every control.  Check this thread.
    Friday, August 31, 2007 6:00 PM
  • I think I found a solution to my problem I just dont understand how the solution is working.

    Instead of setting the e.Handled() = true of the comboBox

    private void cbTruckNumber_KeyDown(object sender, KeyEventArgs e)

    {

    if (e.KeyCode == Keys.Enter)

    {

    SendKeys.Send("{TAB}");

    e.Handled = true;

    }

    }

     

    I set the e.SuppressKeyPress() = true

    private void cbTruckNumber_KeyDown(object sender, KeyEventArgs e)

    {

    if (e.KeyCode == Keys.Enter)

    {

    SendKeys.Send("{TAB}");

    e.SuppressKeyPress = true;

    }

    }

    This keeps the bell or sound noise from happening when I press the Enter key and the SendKeys sends "TAB". Can you explain to me how the e.Handled() and e.SuppressKeyPress() works. I did some reading on msdn and it says the

    KeyEventArgs.SuppressKeyPress Property
    Gets or sets a value indicating whether the key event should be passed on to the underlying control.
    However, I dont understand exactly what this means. If you could explain I would greatly appreciate it.

     

    Friday, August 31, 2007 6:01 PM
  •  

    Hi McGray,

    KeyEventArgs.Handled Property indicates whether to bypass the control's default handling(your ComboBox’s KeyDown event) and the KeyEventArgs.SuppressKeyPress Property indicates whether to prevent the KeyPress event from happening for the current control(your ComboBox’s KeyPress event).

    Hope this helps.
    Best regards.

    Wednesday, September 05, 2007 5:38 AM