none
Enable button only if all text fields are populated? RRS feed

  • Question

  • Hi everyone,

    I'm attempting to write code that enables a button only if all text fields have text. Here is my current code:

        Sub Btn_State()
            If Txt1.Text = "" Or Txt2.Text = "" Or Txt3.Text = "" Or Txt4.Text = "" Then
                Btn.Enabled = False
            Else
                Btn.Enabled = True
            End If
        End Sub

    Now, to me the main bulk of my code looks okay, however the sub in which it's contained is all wrong. I don't know which sub/event to put it under. So that's what I've tried, I then tried looking online for a solution, however I couldn't find any resources relating to this particular issue (surprisingly).

    Thanks for your help

    Sunday, December 10, 2017 9:21 PM

Answers

  •  I don't know which sub/event to put it under. So that's what I've tried, I then tried looking online for a solution, however I couldn't find any resources relating to this particular issue (surprisingly).

    You need to check whether the button can be enabled after any event that could change the text in a text box. The best place to do that is probably the validate event for the text box.   You can call your sub in each text box validate event handler, or you could create one textbox validate event handler for all text boxes and put your code in that event handler.
    https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-connect-multiple-events-to-a-single-event-handler-in-windows-forms

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:28 PM
  •  Add all the TextChanged event handlers to one Event handler sub as shown below.  It will enable/disable the button whenever the text in any of the textboxes are changed.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Btn.Enabled = False
        End Sub
    
        'add the handlers for all your textboxes to the end of this TextChanged event handler sub
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles Txt1.TextChanged, Txt2.TextChanged, Txt3.TextChanged, Txt4.TextChanged
            If Txt1.Text = "" Or Txt2.Text = "" Or Txt3.Text = "" Or Txt4.Text = "" Then
                Btn.Enabled = False
            Else
                Btn.Enabled = True
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:33 PM
  • Alex,

    As a thought:

        Private Function SomeFieldsEmpty() As Boolean
    
            Dim retVal As Boolean = True
    
            Dim qry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                From tb As TextBox In _
                    Me.Controls.OfType(Of TextBox)() Where tb.Text.Trim.Length = 0
    
            If qry.Count = 0 Then
                retVal = False
            End If
    
            Return retVal
    
        End Function

    Do see that this will only work for TextBoxes which aren't inside container controls.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:34 PM
  • Frank - Unfortunately all of my textboxes are in groupboxes.

    A little more involved but you can still use LINQ:

        Private Function SomeFieldsEmpty() As Boolean
    
            Dim retVal As Boolean = False
    
            For Each gb As GroupBox In Me.Controls.OfType(Of GroupBox)()
                Dim findQry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                    From tb As TextBox In gb.Controls.OfType(Of TextBox)()
    
                If findQry.Count > 0 Then
                    Dim emptyQry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                        From tb As TextBox In findQry _
                            Where tb.Text.Length = 0
    
                    If emptyQry.Count > 0 Then
                        retVal = True
                        Exit For
                    End If
                End If
            Next
    
            Return retVal
    
        End Function


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Alex XZ Sunday, December 10, 2017 11:09 PM
    Sunday, December 10, 2017 10:02 PM

All replies

  •  I don't know which sub/event to put it under. So that's what I've tried, I then tried looking online for a solution, however I couldn't find any resources relating to this particular issue (surprisingly).

    You need to check whether the button can be enabled after any event that could change the text in a text box. The best place to do that is probably the validate event for the text box.   You can call your sub in each text box validate event handler, or you could create one textbox validate event handler for all text boxes and put your code in that event handler.
    https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-connect-multiple-events-to-a-single-event-handler-in-windows-forms

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:28 PM
  •  Add all the TextChanged event handlers to one Event handler sub as shown below.  It will enable/disable the button whenever the text in any of the textboxes are changed.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Btn.Enabled = False
        End Sub
    
        'add the handlers for all your textboxes to the end of this TextChanged event handler sub
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles Txt1.TextChanged, Txt2.TextChanged, Txt3.TextChanged, Txt4.TextChanged
            If Txt1.Text = "" Or Txt2.Text = "" Or Txt3.Text = "" Or Txt4.Text = "" Then
                Btn.Enabled = False
            Else
                Btn.Enabled = True
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:33 PM
  • Alex,

    As a thought:

        Private Function SomeFieldsEmpty() As Boolean
    
            Dim retVal As Boolean = True
    
            Dim qry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                From tb As TextBox In _
                    Me.Controls.OfType(Of TextBox)() Where tb.Text.Trim.Length = 0
    
            If qry.Count = 0 Then
                retVal = False
            End If
    
            Return retVal
    
        End Function

    Do see that this will only work for TextBoxes which aren't inside container controls.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Alex XZ Sunday, December 10, 2017 9:50 PM
    Sunday, December 10, 2017 9:34 PM
  • Thank you all for your suggestions,

    I settled for Acamars' and Irons' solutions (I had no idea that you could have multiple handlers! - learn something new everyday). It's all solved now

    Frank - Unfortunately all of my textboxes are in groupboxes.

    Thanks for everyone's help!

    Sunday, December 10, 2017 9:50 PM
  • Frank - Unfortunately all of my textboxes are in groupboxes.

    A little more involved but you can still use LINQ:

        Private Function SomeFieldsEmpty() As Boolean
    
            Dim retVal As Boolean = False
    
            For Each gb As GroupBox In Me.Controls.OfType(Of GroupBox)()
                Dim findQry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                    From tb As TextBox In gb.Controls.OfType(Of TextBox)()
    
                If findQry.Count > 0 Then
                    Dim emptyQry As System.Collections.Generic.IEnumerable(Of TextBox) = _
                        From tb As TextBox In findQry _
                            Where tb.Text.Length = 0
    
                    If emptyQry.Count > 0 Then
                        retVal = True
                        Exit For
                    End If
                End If
            Next
    
            Return retVal
    
        End Function


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Alex XZ Sunday, December 10, 2017 11:09 PM
    Sunday, December 10, 2017 10:02 PM