Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: Enable/Disable Button Through TextBox

What I am trying to do is when the one of the textboxes is empty the button is disabled, but once the two textboxes are filled-up it will enable the button.. What am I doing wrong? thank you in advance!

Public Class ModifiedLoanCalculatorFRM
Private Sub calculateBTN_Click(sender As Object, e As EventArgs) Handles calculateBTN.Click
    If mortgageAmountTBX.Text.Equals("") Or interestRateTBX.Text.Equals("") Then
        calculateBTN.Enabled = False
    Else
        calculateBTN.Enabled = True
    End If
like image 984
kpAtCh Avatar asked Nov 17 '25 04:11

kpAtCh


1 Answers

You're putting the code for testing the contents of the text boxes and then setting the enabled state of the button into the button click handler. That means that it's only ever going to fire when the button is clicked, and if it ever gets disabled, there's no bringing it back.

If your intent is to have the button enable or disable dynamically based on whether or not either of the text boxes is empty, you can move the code from your button click handler into its own subroutine, then make the "Changed" event on both of your text boxes, and your form's load event, call that subroutine:

Private Sub setButtonState()
    If mortgageAmountTBX.Text.Equals("") Or interestRateTBX.Text.Equals("") Then
        calculateBTN.Enabled = False
    Else
        calculateBTN.Enabled = True
    End If
End Sub

Private Sub interestRateTBX_TextChanged(sender As Object, e As EventArgs) Handles interestRateTBX.TextChanged
    setButtonState()
End Sub

Private Sub mortgageAmountTBX_TextChanged(sender As Object, e As EventArgs) Handles mortgageAmountTBX.TextChanged
    setButtonState()
End Sub

Private Sub ModifiedLoanCalculatorFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    setButtonState()
End Sub
like image 102
Govind Parmar Avatar answered Nov 19 '25 18:11

Govind Parmar