begginers help

hey guys, i have literaly started vb,net today, and need some help.

i have this code for a click button, which is a small part of my project, but i cant seem to get it to work, and this is crucial for the rest of my project. what i want to do is display a 1 in a label if a number entered in a textbox is found to be in a certain range

Dim label1 As Integer = 0

Dim textbox1 As Integer

If textbox1 >=10 and textbox1 <=1 then

label1 = label1 + 1

End If

any help would be realy appreciated, thanks




Answer this question

begginers help

  • Rattlerr

    Well, this is my first time helping.

    If textbox1.text >= 10 and textbox1.text <=1 then

    label1.text = "1"

    End If


    Well this is what i got but the value of the textbox can't be 10 or greater and at the same time be one or less than one. If you are trying to it it on the outside of the two values

    If textbox1.text >= 10 or textbox1.text <=1 then

    label1.text = "1"

    End If

    if you want it on the inside

    If textbox1.text <= 10 and textbox1.text >=1 then

    label1.text = "1"

    End If



  • Alex Pizarro

    your actually best converting the values to true Integer's before doing the comparison - your pretty much there in that example! Just do:

    If Convert.ToInt32(textbox1.text) >= 10 or Convert.ToInt32(textbox1.text) <=1 then

    label1.text = "1"

    End If

    however you need to be sure that the values entered are numeric otherwise doing a convert to an integer will result in a runtime exception being thrown



  • Jeph

    ahmedilyas wrote:

    your actually best converting the values to true Integer's before doing the comparison - your pretty much there in that example! Just do:

    If Convert.ToInt32(textbox1.text) >= 10 or Convert.ToInt32(textbox1.text) <=1 then

    label1.text = "1"

    End If

    however you need to be sure that the values entered are numeric otherwise doing a convert to an integer will result in a runtime exception being thrown


    Yeah, I forgot to add that, also wont interger.parse work

  • Eias Nabhan

    indeed it would, but again you have that chance of the exception being thrown if the text is not numeric, in which case you have 2 options here to avoid this (validation)

  • either only accept numeric inputs

  • do a TryParse

     

    the first one would be a better option:

  • done there and then instead of typing in millions of characters only to find out that numerics are only allowed to be entered

  • you would have less code than doing a TryParse when trying to compare the "could be" integer values



  • Lakshmi N

    You need to get input from the user for your textbox, unless you have and didn't show it.

    textbox1 = Val(Text1)

    This should work, it sets the value from the text box, default name Text1, to your variable.

    Next, you need to show the value of label1 in your label.

    Label1.caption = label1



  • CarlDemelo

    Remember to click what post answered your question.

  • Tomas L

    hey thanks guys, i have got it working now. thanks alot!!

  • begginers help