Question

In visual basic, I want to make a TextBox2 visible when TextBox1.Text = "SHUTDOWN"

this is my code

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) 
                                                     Handles TextBox1.TextChanged
    AcceptButton = Button1
    If TextBox1.Text = "SHUTDOWN" Then
        TextBox2.Visible = True
    End If
End Sub

but it's not working

note: there is no error message... and the textbox1 charachter casing is "upper" so it will be "SHUTDOWN" and not working means that when i write "SHUTDOWN" in textbox1, textbox2 don't become visible

Was it helpful?

Solution

.NET is case sensitive, that means that shutdown is not the same as SHUTDOWN. You can use Equals with the overload that takes a StringComparison:

If TextBox1.Text.Equals("SHUTDOWN", StringComparison.CurrentCultureIgnorecase) Then
    TextBox2.Visible = True
End If

Another option in VB.NET only is to use OPTION Compare on file- or on project level.

If you use this as first line in your file:

Option Compare Text

You get a case-insensitive comparison. However, i would prefer the .NET way.

Text: Results in string comparisons based on a case-insensitive text sort order determined by your system's locale. This type of comparison is useful if your strings contain all text characters, and you want to compare them taking into account alphabetic equivalences such as case insensitivity and closely related letters. For example, you might want to consider A and a to be equal, and Ä and ä to come before B and b.

OTHER TIPS

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

Textbox2.visible = False

End Sub


Protected Sub TxtBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtBox1.TextChanged
    If TxtBox1.Text.ToUpper = "SHUTDOWN" Then
       TextBox2.Visible = True
    Else
       TextBox2.Visible = False
    End If
End Sub

To ignore the case of your text (if that is the problem) you could use the following:

If TextBox1.Text.ToUpper() = "SHUTDOWN" Then
    TextBox2.Visible = True
End If

also be sure your TextBox2 has the right coordinates when became visible.

Just did a test with:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If (TextBox1.Text = "SHUTDOWN") Then
        TextBox2.Visible = True
    End If
End Sub

Found no problems - it may be AcceptButton = Button1. I'm not sure why it isn't working but mine is working when "SHUTDOWN" is entered. Try changing the AcceptButton to a comment (') and then trying the code, if it works then its the AcceptButton variable.

May not be declared probably to store a button?

Dim Wrd As String = "SHUTDOWN"
Protected Sub TxtBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtBox1.TextChanged
        If Wrd = TxtBox1.Text.ToUpper Then
            TxtBox5.Visible = True
         End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top