2

I honestly don't know what I'm doing wrong. I have a textbox named "txtNumSticks" where the user enters a number. After the user hits start, I want a message box to pop up that says "Okay! We'll play with (x) sticks!" But I can't get it to work. First day learning VB.net. Thanks in advance!

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    Dim NumSticks As String
    txtNumSticks.Text = NumSticks
    Game.Show()
    Me.Close()
    MessageBox.Show("Okay! We'll play with " & NumSticks & "sticks!")
End Sub
0

2 Answers 2

4

You are setting the variable the wrong way around you should be assigning NumSticks to the value in the text box so:

NumSticks = txtNumSticks.Text 

or alternatively without the use of a variable

MessageBox.Show("Okay! We'll play with " & txtNumSticks.Text & "sticks!")
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Switching the variable assignment worked! However, I also tried the second way you showed me without the variable, but it didn't work. Any idea why that wouldn't work? It just says, "Okay! We'll play with sticks!"
NumSticks = txtNumSticks.Text.ToString
txtNumSticks.Text does not need .ToString() applied to it as it is already a string.
3

You may want to add a little bit of error checking in your program to make sure your value entered is Numeric.

    Dim NumSticks As String

    NumSticks = txtNumSticks.Text.ToString

    If IsNumeric(NumSticks) Then
        Game.Show()
        MessageBox.Show("Okay! We'll play with " & NumSticks & " sticks!")
        Me.Close()

    Else
        ' Let user know the value is non-numeric
        MessageBox.Show("Non Numeric Value entered", "Error!", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub

    End If

2 Comments

Awesome! I was actually just figuring out a way to do that too!
You should try getting familiar with Try..Parse..Catch..Finally as well. This with added logging saves so many headaches trying to solve where your code went wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.