-2

I wan't to make a PictureBox and a Label show if TextBox1 and TextBox2 Texts equal definite word.

But I receive an error...
Please, help

Here is the code:

Public Class Appearance
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then
            PictureBox4.Image = My.Resources.brown
            PictureBox2.Image = My.Resources.blue
            PictureBox5.Image = My.Resources.green
            PictureBox4.Visible = True
            PictureBox2.Visible = True
            PictureBox5.Visible = True
            Label7.Visible = True
            Label8.Visible = True
            Label9.Visible = True
        End If
    End Sub
End Class
GSerg
  • 71,102
  • 17
  • 141
  • 299
Marko Polo
  • 13
  • 4

2 Answers2

1

In VB.NET & means string concatenation. You probably want to use AndAlso:

If TextBox1.Text = "Brown" AndAlso TextBox2.Text = "Brown" Then
Community
  • 1
  • 1
Neolisk
  • 23,880
  • 16
  • 72
  • 135
0

Edit : Sorry, missed you wanted to compare both TextBox1 and TextBox2 values to one String Value

Right evaluation is already provided by Neolisk :

    If TextBox1.Text = "Brown" AndAlso TextBox2.Text = "Brown" Then

EDIT : the following suggestion is wrong..

Replace this

    If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then

by this :

    If TextBox1.Text = TextBox2.Text Then

EDIT : I leave the explanation here

If you write If TextBox1.Text = "Brown" & TextBox2.Text = "Brown" Then, and we suppose that TextBox2.Text is "Brown" what's happening is :

  • Since the & is for String concatenation in VB, just like +, TextBox1.Text will be compared to the chain "Brown" & TextBox2.Text = "Brown".
  • With "Brown" & TextBox2.Text = "Brown", "Brown" & TextBox2.Text will be evaluated first, since the & string concatener takes precedence over the = boolean evaluation. You'll get the concatenated string "BrownBrown" (= "Brown").
  • But then, you'll have "BrownBrown" = "Brown", and at this stage, the full chain of evaluation is If TextBox1.Text = "BrownBrown" = "Brown" Then.
  • TextBox1.Text = "BrownBrown" will be evaluated first and will return False. In the end, you're evaluating :

    If False = "Brown" Then ' <- Boolean comparison with String Error !

Since you're new to VB, good to know :

Note that AndAlso has the shorcut feature. If the evaluation on the left part is False, the right part is not evaluated.

To evaluate both left and right, there is the And comparer. But, honestly, comparing

    If False = True And "SameValue" = "SameValue" ' will always return False.

There is NO meaning in comparing the right side if the left is already False. You ought to know that this is a unique glitch of VB. However, I don't know if it was fixed since. But if not, the same applies for Or and OrElse.

=> You'd better use AndAlso and OrElse instead of And/Or from the start.

Community
  • 1
  • 1
Karl Stephen
  • 1,030
  • 8
  • 17