If textboxes are empty, how do I disable a button?

Tim 60 Reputation points
2024-01-15T16:23:47.9233333+00:00

I have a button connected to textboxes. How do I disable the button if textboxes are empty? I've tried a few things, cannot seem to get it to function. Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,921 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,090 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,405 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.6K Reputation points
    2024-01-15T17:13:15.1866667+00:00

    For instance, handle the TextChanged event of textboxes. (The handlers can be added by double-clicking the textboxes). Also handle the Load event of the form.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            UpdateButton()
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            UpdateButton()
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            UpdateButton()
        End Sub
    
        Sub UpdateButton()
            Button1.Enabled = Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso Not String.IsNullOrWhiteSpace(TextBox2.Text)
        End Sub
    

    It is possible to use a unique TextChanged handler for textboxes.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.