#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Question Just a little help plz c:


    Alright so, i'm new to Visual Basic and i'm trying to make just a simple program that tells you if the number entered in the text box is positive or negative when you click the button. I'm not sure if the code i put on the button is wrong or if nothing I did was right xD Here's the code for the button.

    Sub checkposneg()
    If text1 <= 0 Then Print "Positive"
    If text1 >= 0 Then Print "Negative"
    End Sub

    and i'm pretty sure that almost none of it is right xD
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    What visual basic are you using VB6, VB.NET, VB Script?

    I havnt ever written any command line VB only windows with a gui. I am assuming your writing console application with your use of "print" . Yet you also say you are using a textbox which would mean GUI?

    Either way with VB.net
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    if (convert.todecimal(textbox1.text)>=0) THEN
    label1.text="positive"
    ELSE
    label1.text="negative"
    END IF
    End Sub
    Originally Posted by dabadguy4273
    Alright so, i'm new to Visual Basic and i'm trying to make just a simple program that tells you if the number entered in the text box is positive or negative when you click the button. I'm not sure if the code i put on the button is wrong or if nothing I did was right xD Here's the code for the button.

    Sub checkposneg()
    If text1 <= 0 Then Print "Positive"
    If text1 >= 0 Then Print "Negative"
    End Sub

    and i'm pretty sure that almost none of it is right xD
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Originally Posted by dabadguy4273
    Alright so, i'm new to Visual Basic and i'm trying to make just a simple program that tells you if the number entered in the text box is positive or negative when you click the button. I'm not sure if the code i put on the button is wrong or if nothing I did was right xD Here's the code for the button.

    Sub checkposneg()
    If text1 <= 0 Then Print "Positive"
    If text1 >= 0 Then Print "Negative"
    End Sub

    and i'm pretty sure that almost none of it is right xD
    What you have to appreciate is that the contents of a TextBox are treated as a Variant. Since you are entering text, the variant will be treated as text, but what you are after is the numerical value. Create a new project and add a text box (default name Text1) and a CheckBox (default name Check1). Change the text property of Text1 to (blank), and Change the Caption property of Check1 to "Positive". Add the following code:
    Code:
    Function CheckPos(vNum As Variant) As Boolean
        If Val(vNum) >= 0 Then
            CheckPos = True
        Else
            CheckPos = False
        End If
    End Function
    
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            If CheckPos(Text1.Text) Then
                Check1.Value = 1
            Else
                Check1.Value = 0
            End If
        End If
    End Sub
    I have renamed the subroutine "checkposneg" that you wrote to a function "CheckPos" so that it will return a value. When you enter a number in the text box and hit the Enter key, it will call the function and the function will check the value of the text and return either "True" or "False". That value is used to set the Check Box.

    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo