Results 1 to 3 of 3

Thread: Any VB help?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    24

    Any VB help?

    OK, I got the code:


    If TextBox1.Text = Label1.Text Then
    MsgBox("Congrats")
    End If

    Say the label1 text was "89". I want the message to still come up if the person said "88" or "90", so pretty much I want the margin to be 1 going in either direction, but I dont know how to do that, help?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Any VB help?

    vb Code:
    1. If TextBox1.Text = Label1.Text OrElse (TextBox1.Text -1) = Label1.Text OrElse (TextBox1.Text +1) = Label1.Text Then
    2. MsgBox("Congrats")
    3. End If

    You'll also need to be checking that Textbox1 contains text.

    Cheers
    Why do today what you can tomorrow...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Any VB help?

    If you want to work with numbers then you should work with numbers:
    vb.net Code:
    1. If Math.Abs(CInt(TextBox1.Text) - CInt(Label1.Text)) <= 1 Then
    This assumes that the Text of each control can be converted to a number. In the real world you would use TryParse for the Textbox in case the user entered a non-numeric value, plus you wouldn't use the Text of the Label because you'd use the actual number that you put in the Label in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width