Dear Experts

I have two textboxes

I want textbox1 to accept any character except 0 (zero)
When user enter 0 then it must not go to next control

I want textbox2 only accpet character "Y" or "N"

Please help

Something like the following should work for you:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = e.KeyChar = CChar("0")
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        e.Handled = Not (e.KeyChar = CChar("Y") Or e.KeyChar = CChar("N"))
    End Sub

You're going to also want to set the maximum characters on the second textbox to 1 so that only a Y or an N can be present, otherwise a user can type YNYYNN.

HTH.

Something like the following should work for you:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = e.KeyChar = CChar("0")
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        e.Handled = Not (e.KeyChar = CChar("Y") Or e.KeyChar = CChar("N"))
    End Sub

You're going to also want to set the maximum characters on the second textbox to 1 so that only a Y or an N can be present, otherwise a user can type YNYYNN.

HTH.

Dear Sir,

By using your codes:
Textbox1 does not allow 0 to write in it
0 shoud be displayed but focus must not go to other control.
I mean when user enter 0 then it should display 0 in it.

Textbox2 accepts only Y or N. BUT more that one time also accpets.
how to set format for only one character?

please modify

Edited 6 Years Ago by tqmd1: n/a

I misunderstood the 0 issue. There shouldn't be any code to allow the 0 key to be pressed.

If you only want Y and N, it may be better to use a combobox with only Y and N in the items collection. I don't know why I didn't think of that before, but that's most likely the better option. Also, the code I provided allows only a Y and N in the second text box (per your original request) and not y or n.

The following will allow the second text box to receive a Y,y,N,n and any control keys (like backspace).

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        e.Handled = Not (e.KeyChar = CChar("Y") Or e.KeyChar = CChar("N") Or e.KeyChar = CChar("y") Or e.KeyChar = CChar("n")) And Not Char.IsControl(e.KeyChar)
    End Sub

I misunderstood the 0 issue. There shouldn't be any code to allow the 0 key to be pressed.

If you only want Y and N, it may be better to use a combobox with only Y and N in the items collection. I don't know why I didn't think of that before, but that's most likely the better option. Also, the code I provided allows only a Y and N in the second text box (per your original request) and not y or n.

The following will allow the second text box to receive a Y,y,N,n and any control keys (like backspace).

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        e.Handled = Not (e.KeyChar = CChar("Y") Or e.KeyChar = CChar("N") Or e.KeyChar = CChar("y") Or e.KeyChar = CChar("n")) And Not Char.IsControl(e.KeyChar)
    End Sub

Dear Sir,

Textbox2 is still accepting character Y or N more than one time.
It must accept Y or N only once.

Dear Sir,

Textbox2 is still accepting character Y or N more than one time.
It must accept Y or N only once.

During the form load event, or in designer mode, ya need to set the maximum length to be 1.

TextBox2.MaximumLength = 1

Edited 6 Years Ago by Stevoni: n/a

This question has already been answered. Start a new discussion instead.