Reputation: 107
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
Upvotes: 3
Views: 816
Reputation: 125342
You can use:
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, MyBase.Load
Button1.Enabled = Not (String.IsNullOrEmpty(TextBox1.Text) OrElse
String.IsNullOrEmpty(TextBox2.Text))
End Sub
Notes about above code:
String.IsNullOrEmpty
to check if the text is emptyOrElse
instead of Or
Enabled
property with a single expression.Upvotes: 2
Reputation: 9
Create a timer, and have it start when the form is run, Put this code on the timer tick
If INSERTTEXTBOXNAMEHERE.text = "" Then
INSERTBUTTONNAMEHERE.enabled = false
Else INSERTBUTTONNAMERHERE.enabled = True
Upvotes: 0
Reputation: 21572
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
Upvotes: 4