If & Else

Posts 1–12 of 12 · Page 1 of 1
If & Else
Hi,
I've been facing the same problem since I started with VB.
I'll give you an Example:
Code:
If Textbox1.Text.Contains("Word") Then
Label1.Text = "Word Found"
Else
Label1.Text = "Word not Found"
EndIf

If Textbox1.Text.Contains("Hello") Then
Label2.Text = "Hello Found"
Else
Label2.Text = "Hello not Found"

The Problem is Label2 works probably but Label1 not.
When Textbox1 doesn't have "Word" in it, it writes "Word found" but when Texbox1 doesn't have "Hello" in it, it writes "Hello not found"

I hope you could help me.
Thanks.
Nothing in this code for not work ...

Code:
  If Textbox1.Text.Contains("Word") Then
            Label1.Text = "Word Found"
        Else
            Label1.Text = "Word not Found"
        End If

        If TextBox1.Text.Contains("Hello") Then
            Label2.Text = "Hello Found"
        Else
            Label2.Text = "Hello not Found"
        End If
Post more lines of ur code
Quote Originally Posted by MasterBS View Post
Nothing in this code for not work ...

Code:
  If Textbox1.Text.Contains("Word") Then
            Label1.Text = "Word Found"
        Else
            Label1.Text = "Word not Found"
        End If

        If TextBox1.Text.Contains("Hello") Then
            Label2.Text = "Hello Found"
        Else
            Label2.Text = "Hello not Found"
        End If
Post more lines of ur code
Well.. That's the code I used as example and it's pretty much similar to the code I'm having problem with.
It's just fine.. you probably didn't see your mistake in the other code then..
code is fine. Your input (textbox1.text) must not be what you think.. are you typing it in after the program starts? ..perhaps "word" was lowercase if you entered "Hello word" (vs. "Hello Word")
Anyway, this* code is fine: maybe you have a typo in your original? If you have any other textboxes on the form, make sure you're accessing TextBox1.
Quote Originally Posted by abuckau907 View Post
code is fine. Your input (textbox1.text) must not be what you think.. are you typing it in after the program starts? ..perhaps "word" was lowercase if you entered "Hello word" (vs. "Hello Word")
Anyway, this* code is fine: maybe you have a typo in your original? If you have any other textboxes on the form, make sure you're accessing TextBox1.
The code used after clicking a button, The form has: Textbox, Button and two labels.
Textbox: Contains some random long text
Button: tries to find word: "Hello" & "Word" (Both of them are the same case)
Label1: writes if "Hello" word found
Label2: writes if "Word" word found

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Textbox1.Text.Contains("Word") Then
Label1.Text = "Word Found"
Else
Label1.Text = "Word not Found"
EndIf
If Textbox1.Text.Contains("Hello") Then
Label2.Text = "Hello Found"
Else
Label2.Text = "Hello not Found"
EndIf
That's all the code I use.
@ @Biesi would be nice if you and abuckau907 helped me
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = "Hello1234Word5678"  '// should results in  Found&Found

If Textbox1.Text.Contains("Word") Then
  Label1.Text = "Word Found"
Else
  Label1.Text = "Word not Found"
EndIf
If Textbox1.Text.Contains("Hello") Then
  Label2.Text = "Hello Found"
Else
  Label2.Text = "Hello not Found"
EndIf

End Sub
Just for testing, please set the value of textbox1 inside the button_click and see if you get the output you expect.

Please take a screenshot to prove the code isn't working...we already said it looks correct : p
(ie. set textbox1.text to "Hello1234Word5678" and see which path the IF statements take)

edit: did you change Option Compare ..?
Maybe Hello and Word do not consist of the chars you compare.. some special ones maybe
Quote Originally Posted by abuckau907 View Post
TextBox1.Text = "Hello1234Word5678" '// should results in Found&Found
The text in Textbox1 is generated randomly so I don't know what will be the content.
@ @Biesi They both are the same.
Quote Originally Posted by Mayion View Post
The text in Textbox1 is generated randomly so I don't know what will be the content.
Just post every bit of the code which is related to the problem..
Posts 1–12 of 12 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us