0
private Sub Command1_Click()
a = InputBox("What is the Number ?", "Input Example"," ")
If a = "-1" Then
End If
End Sub

the whole question is: Enter some numbers until a negative value is entered to stop the program(stop running the what is your number thing). then find the average of the entered numbers.

What I want to do, for now, I want to know how can I make my "a" variable accept any negative value like in the code above it stops when I enter -1 but I want to stop when I enter -3, -10, or any negative value.

There are some helpful answers down below

sayo
  • 56
  • 1
  • 8
  • 3
    What does *make my program stop* mean? Btw, you can use a NumericUpDown control to accept numeric input from a User (you can set Minimum and Maximum values). I'd ditch the InputBox thing. BTW, this looks like a WinForms Project, but you should specify the Platform in use, using the appropriate Tag. – Jimi Jun 23 '20 at 10:24
  • sorry im very new to stack overflow and to visual basic. do you have anything that might help me about the InputBox thing ? – sayo Jun 23 '20 at 10:30
  • Negative number? You are clearly asking for a message "What is your message?". Beside, stop means… ending the execution of the application? - I would encourage to create custom forms if you need something InputBox does not provide. The answer from Andrew Morton is - at the moment of writing - taking a very literal interpretation of your question, if that is not working for you, please [edit your question](https://stackoverflow.com/posts/62532509/edit) to clarify what you mean. – Theraot Jun 23 '20 at 10:30
  • Has "a" been declared somewhere else? – Nathan Champion Jun 23 '20 at 12:06
  • The use of "Command1", no parameters in the Click() handler, and `InputBox` lead me to believe you are using VB6 and not VB.Net. Please clarify what platform you are developing in as these two are very different! – Idle_Mind Jun 23 '20 at 13:09
  • @sayo You have completely changed the question. Please ask a new question if you are going to change the programming language used. – Andrew Morton Jun 24 '20 at 16:32

2 Answers2

2

If you are expecting the usual input to be text then you can use the Double.TryParse method to check if a number was entered. If it was, then you can check if that number is negative, and exit the application if so:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim userMsg As String
    userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
    If userMsg <> "" Then
        Dim x As Double
        If Double.TryParse(userMsg, x) AndAlso x < 0 Then
            Application.Exit()
        End If

        MessageBox.Show(userMsg)

    Else
        MessageBox.Show("No Message")

    End If

End Sub

The AndAlso operator only looks at the second argument if the first evaluated to True.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

If you would like to repeat some portion of code till specific condition is met, you need to use:

While...End While Statement (Visual Basic)

or

Do...Loop Statement (Visual Basic)

It's also possible to write conditional loop using For... Next statement

Dim myNumber As Integer = 0
Dim mySum As Integer = 0
Dim myCounter As Integer = 0
Do
    Dim answer As Object = InputBox("Enter integer value", "Getting average of integer values...", "")
    'check if user clicked "Cancel" button
    If answer <> "" Then
        'is it a number?
        If Int32.TryParse(answer, myNumber)
            If myNumber >=0 Then
                mySum += myNumber
                myCounter += 1
            Else
                Exit Do
            End If
        End If
    End If
Loop
Dim average As Double = mySum/myCounter
'you can display average now

Tip: Do not use InputBox, because this "control" is VisualBasic specific. Use custom Form instead. There's tons of examples on Google!

Maciej Los
  • 8,468
  • 1
  • 20
  • 35