Click to See Complete Forum and Search --> : ignore input from keyboard???


laptop_01
December 29th, 2003, 02:31 PM
I have a C# form with a combo box in it. Currently, the user can select one of three availbale items.

I also want to ignore any keyborad input (ie, I don't want the user to be able to type anything inside the combox box. How can I do this. I VB 6.0, I would trap the keypress event and set keyAscii to 0. How to do the same in C#?

Thanks

gjs368
December 29th, 2003, 02:50 PM
I am assuming that you only want the user to select an item from the drop-down list. If that is the case, all you need to do is to set the property 'DropDownStyle' for your combo box to DropDownList. This will force the user to select from the list, and not allow free typing.

laptop_01
December 29th, 2003, 03:00 PM
gjs368

Thanks a lot. This worked like a charm.

For my own education purposes, How do I duplicate what I used to do in VB 6.0 in C#. Say I have a text box and I want to ignore user's input if the keypress is not a number between 0 and 9.

Thanks

gjs368
December 29th, 2003, 04:24 PM
Originally posted by laptop_01
Say I have a text box and I want to ignore user's input if the keypress is not a number between 0 and 9.
Add a 'KeyPress' event handler for your text box. to limit input to numbers, you could code this in the handler:private void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = (e.KeyChar < '0' || e.KeyChar > '9');
}Setting e.Handled to true prevents the keystroke from being input.

laptop_01
December 29th, 2003, 05:05 PM
gjs368


Thanks a lot. :thumb