how do i....textbox?

Well basically i want to know how i can write an IF...THEN decision structure based on the contents of a textbox.

for example lets say i want to say something like
IF textbox1.text (has no text in the field) THEN textbox1.text = 0

Also how do i write a code that only allows integers of a positive value to be typed and accepted into the textbox



Answer this question

how do i....textbox?

  • AnOracle

    what does the e. mean in (e.keychar) because i wrote the code out, and createdthe Then code which is a true statement, yet the (e.keychar) has a build error.
  • Robin Moss

    e is a property passed in to the key pressed event, it's of type keypressedeventargs. You may have called it something different, such as ea.



  • az1538

    // IF textbox1.text (has no text in the field) THEN textbox1.text = 0

    You want to text to equal "0" You should put it in quotes. VB, being weakly typed, will do this for you, but it's better to say what you mean :-)

    if string.isnullorempty(textbox1.text) then textbox1.text = "0"

    The Leave event is the most logical place to be doing this sort of stuff ( in other words, when the textbox loses focus )

    as for the second question - the maskedtextbox is one way you can do this, the other is to handle one of the key events of the textbox, and use char.iscontrol and char.isdigit to set e.handled in that event handler ( e being the event passed in ). If Handled is true, then the key is rejected. I prefer to roll my own, I don't like the way the maskedtextbox works. It may, however, be an easier solution to your problem if you're happy with the way it does it's stuff.



  • antoan

    Also how do i write a code that only allows integers of a positive value to be typed and accepted into the textbox

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Char.IsNumber(e.KeyChar) Then



  • how do i....textbox?