-3

I've a form with 3 textboxes and 2 buttons. Button 1 is enabled if all 3 textboxes are filled in. Button 2 is a quit program button. Let's see my code for the textboxes

Private Sub TextBoxes_TextChanged(
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles _
        TextBox1.TextChanged,
        TextBox2.TextChanged,
        TextBox3.TextChanged

    Dim textBoxes = {TextBox1, TextBox2, TextBox3}
    Button1.Enabled = textBoxes.All(Function(tb) tb.Text <> "")
End Sub

So now I am wondering how can I change the value of textbox 2 to only numerics and of textbox 1 and 3 to only text. I hope you'll understand my questions. I hope you guys can help me out.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Slamnose
  • 1
  • 1
  • By "change" you mean "validate only numbers/text are written on the rspective textbox" or "only allow numbers/text on the respective textbox"? – Josh Part Mar 03 '17 at 16:43
  • 2
    Why not validate the entries? There is a control that only allows numeric values - `NumericUpDown` – Ňɏssa Pøngjǣrdenlarp Mar 03 '17 at 16:43
  • use JavaScript to set only numeric or only text if it is web project. – Tony Dong Mar 03 '17 at 16:44
  • 1
    @TonyDong -- I'm guess this is WinForms, not web. – rory.ap Mar 03 '17 at 16:44
  • Take a look at https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx – Aaron Mar 03 '17 at 16:47
  • http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers – Steve Mar 03 '17 at 17:19
  • 1
    Possible duplicate of [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – Taegost Mar 03 '17 at 18:11
  • Taegost isn't there a difference between C# and VB.net? But thanks in advance. @Tony Dony its a WinForms like Rory.ap said. Josh Part I indeed mean validate only numbers/text. Plutonix thanks i think i worked it out. Aaron thanks, i took a look and it did help me :) – Slamnose Mar 04 '17 at 07:28

1 Answers1

-2

Use the KeyPress event to filter the keys you want the user to be able to press.

For example to filter for only positive numbers.......

Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
    Dim AllowedKeys As String = "0123456789" & vbBack
    e.Handled = Not AllowedKeys.Contains(e.KeyChar)
End Sub

Note: For numeric, decimal point and the minus sign need special handling to ensure there is only one and in the right place.

However you still need to validate the value before you use it. It can be set other ways and can always be blank.

However, as Plutinix mentions, it is better to use the right control for the value you want to collect. Eg NumericUpDown for numbers, DatePicker for dates, TextBox for TEXT whenever you can.

Trevor_G
  • 1,331
  • 1
  • 8
  • 17
  • 5
    Can you avoid a invalid paste operation (Ctrl+V) with this code? And what if the pasted text is a correct number? And what if you want a negative number? (With the minus sign as the first character and not in the middle of the input text) Sorry but as simple as is your solution cannot handle a bit more complex situations – Steve Mar 03 '17 at 17:17
  • @Steve, true enough. – Trevor_G Mar 03 '17 at 17:21
  • Thanks, the textbox is to fill in your age, so there wont be negatives numbers i hope :P. This did work for me, and I understand the lines except the "& vbBack" what does that do? – Slamnose Mar 04 '17 at 07:30
  • Ahh i already found it. Without the &vbBack you cant backspace haha – Slamnose Mar 04 '17 at 07:41