Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with controlling characters entered in a combo box of a Windows application.

Hi,

I'm using the cmbobox that comes with VS2010. How do you only allow users to only numbers 0 -9 and symbos "/" and "-" in the combobox using vb.net?

Victor
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

hi ...Can you clarify a little bit more.In ComboBoxes we have a list of Values and we choose one of them...you need to filter these choises while typing only numbers 0 -9 and symbos "/" and "-" ?
Avatar of Victor  Charles

ASKER

Yes that is correct, forgot to mention letters A-Z are also allowed.
take a look at this
https://www.experts-exchange.com/questions/20866840/Allow-only-letter-number-or-backspace-in-textbox-vb-net.html
    Private Sub ComboBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or Char.IsLetter(e.KeyChar) Or e.KeyChar = "-")
    End Sub

Open in new window

Thanks for the code, need to leave office for about two hours, will get back to you later.
Note that the above code does not restrict the Left Right Directional Keys..
If you want to Restrict these Keys Also Use The Key Down Event like:
If e.KeyCode = Keys.Left Then
            e.SuppressKeyPress = True
        End If

Open in new window

Hi,

The code works but I am unable to remove data, back space does not work. Also when I include the second part of your in the key press event. I receive the following error:
keycode is not a member of system.Windows.Forms.KeyPressEventArgs, Í receive the same error message for SupressKeyPress.

Thanks,

V.
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
It works!

Thank You.

Victor
glad i help you Victor...
I appreciate your Help.