dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Disabling button unless all fields are filled

    Hello, I'm trying to disable button if any of the fields are empty and enable only if all are filled. I was trying to use leave event and Text Changed event but it doesn't work, using couple of combo boxes and textboxes. Thanks

    Code:
    Code:
     Private Sub txtCreditNo_Leave(sender As Object, e As EventArgs) Handles txtCreditNo.Leave, txtCCV.Leave, cboTop.Leave,
                 cboCreditType.Leave, cboMonth.Leave, cboYear.Leave
    
            If txtCCV.Text = String.Empty Or txtCreditNo.Text = String.Empty Or cboTop.SelectedIndex > -1 Or cboMonth.SelectedIndex > -1 _
               Or cboYear.SelectedIndex > -1 Or cboCreditType.SelectedIndex > -1 Then
    
                btnComplete.Enabled = False
    
            Else
    
                btnComplete.Enabled = True
            End If

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    337

    Re: Disabling button unless all fields are filled

    Quote Originally Posted by BloodySandwich View Post
    Hello, I'm trying to disable button if any of the fields are empty and enable only if all are filled. I was trying to use leave event and Text Changed event but it doesn't work, using couple of combo boxes and textboxes. Thanks

    Code:
    Code:
     Private Sub txtCreditNo_Leave(sender As Object, e As EventArgs) Handles txtCreditNo.Leave, txtCCV.Leave, cboTop.Leave,
                 cboCreditType.Leave, cboMonth.Leave, cboYear.Leave
    
            If txtCCV.Text = String.Empty Or txtCreditNo.Text = String.Empty Or cboTop.SelectedIndex > -1 Or cboMonth.SelectedIndex = -1 _
               Or cboYear.SelectedIndex = -1 Or cboCreditType.SelectedIndex = -1 Then
    
                btnComplete.Enabled = False
    
            Else
    
                btnComplete.Enabled = True
            End If
    Since your if statement is coded to check if the form is NOT completed, then you should be checking if ComboBox.SelectedIndex = -1, not > -1. Bolded above.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Disabling button unless all fields are filled

    Thank you man for your time and help really appreciate.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    431

    Re: Disabling button unless all fields are filled

    Kind of drunk but this should pan out for you.

    Code:
    Public Class FormTest
        Private Sub FormTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each ctrl As Control In GroupBox1.Controls
                If TypeOf ctrl Is TextBox Then
                    Dim TBctrl As TextBox = CType(ctrl, TextBox)
                    If TBctrl.Text.Length <= 0 Then
                        BtnTest.Enabled = False
                        AddHandler TBctrl.KeyUp, AddressOf EnableButton
                    End If
                ElseIf TypeOf ctrl Is ComboBox Then
                    Dim CBctrl As ComboBox = CType(ctrl, ComboBox)
                    If CBctrl.Text.Length <= 0 Then
                        BtnTest.Enabled = False
                        AddHandler CBctrl.KeyUp, AddressOf EnableButton
                    End If
                End If
            Next
        End Sub
        Private Sub EnableButton()
            Dim TBCheck As Boolean = False
            Dim CBCheck As Boolean = False
            For Each ctrl As Control In GroupBox1.Controls
                If TypeOf ctrl Is TextBox Then
                    Dim TBctrl As TextBox = CType(ctrl, TextBox)
                    If TBctrl.Text.Length >= 1 Then
                        TBCheck = True
                    Else
                        TBCheck = False
                        Exit For
                    End If
                ElseIf TypeOf ctrl Is ComboBox Then
                    Dim CBctrl As ComboBox = CType(ctrl, ComboBox)
                    If CBctrl.Text.Length >= 1 Then
                        CBCheck = True
                    Else
                        CBCheck = False
                        Exit For
                    End If
                End If
            Next
            If TBCheck Or CBCheck Then
                BtnTest.Enabled = True
            Else
                BtnTest.Enabled = False
            End If
    
        End Sub
    End Class

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: Disabling button unless all fields are filled

    Nothing more to add except consider use OrElse instead of Or
    Code:
        Private Sub txtCreditNo_Leave(sender As Object, e As EventArgs) Handles txtCreditNo.Leave,
                                                                                txtCCV.Leave,
                                                                                cboTop.Leave,
                                                                                cboCreditType.Leave,
                                                                                cboMonth.Leave,
                                                                                cboYear.Leave
    
            If txtCCV.Text = String.Empty OrElse
               txtCreditNo.Text = String.Empty OrElse
               cboTop.SelectedIndex = -1 OrElse
               cboMonth.SelectedIndex = -1 OrElse
               cboYear.SelectedIndex = -1 OrElse
               cboCreditType.SelectedIndex = -1 Then
    
                btnComplete.Enabled = False
            Else
    
                btnComplete.Enabled = True
            End If
        End Sub



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.