What is Wrong With This VERY Simple Code Please?

NoobConfoobed

Member
Joined
Jul 12, 2012
Messages
5
Programming Experience
Beginner
I am just practicing. This is not a project. It's purely to see if I am on the right page:

VB.NET:
Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load




        If TextBox1.Text = 1 Then




            MsgBox("2")
        ElseIf TextBox1.Text = 2 Then


            MsgBox("3")
        End If


    End Sub
End Class

Why isn't the Message Box appearing?

Thanks, and apologies for my 'Noobiness'.
 
First, we don't know the value of TextBox1.Text; but that doesn't really matter here. You are comparing a string (the Text property of a TextBox is of type String) to an integer (the value 1 or 2, without quotes surrounding it), they will never be equal.

I always suggest turning Option Strict On; this restricts implicit data type conversions to only widening conversions which will cause Visual Studio to warn you of these types of problems.

There are many ways to get this to work:
-convert TextBox.Text to an integer first, possibly using Integer.Tryparse and some error handling in case the user enters a non-numeric value.
-Use a control that only accepts integer values such as the NumericUpDown control and use its Value property to compare (be sure to properly handle Decimal to integer conversion as the Value property is of Decimal type).
-Compare to a string: If TextBox1.Text = "1" Then
-etc...
 
First of all, the Form1_Load event takes place before anything can be entered into the textbox. You need to place your code inside a button's Click event. When user clicks the button, the code will execute.

Anything inside a textbox is a string, which is text, and all text must be placed within quotation marks.

Instead of MsgBox, please use the MessageBox.Show() method.

Your code should be as follows:

VB.NET:
Option Strict On
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "1" Then
            MessageBox.Show("2")
        ElseIf TextBox1.Text = "2" Then
            MessageBox.Show("3")
        End If
    End Sub

End Class

Next, you should place Option Strict On at the top of your code. This will enforce correct type conversion and avoid many errors.
 
Or if you are sure your input will be a number you can cast it as an integer.

VB.NET:
Cint(textbox1.text) = 1

All this does is take the string "1" and turn it into a number. If someone enters anything other than a number you would get an invalidcast exception so you would need validation on the field.
 
Generally if you want to show textbox contents in a message box you should do this.

MessageBox.Show(TextBox1.Text)
 
Sure it worked. But you're still avoiding the basic issue and can run into more serious problems later. Val() is an old legacy function left over from a previous version of BASIC, and is still available for backwards compatibility. You should be using the newer .NET methods. You should also be adding Option Strict On to the top of your code.
 
Back
Top