hello
i want to know How to enabling a command only after text boxes are filled
in VB6
Printable View
hello
i want to know How to enabling a command only after text boxes are filled
in VB6
Try this
ORCode:Private Sub Text1_Change()
If Text1.Text = vbNullString Then
Command1.Enabled = False
Else
Command1.Enabled = True
End If
End Sub
Code:Private Sub Text1_Change()
Command1.Enabled = Not (Text1.Text = vbNullString)
End Sub
this would work alsoCode:Private Sub Text1_Change()
Command1.Enabled = Len(Text1.Text)
End Sub
If there are several textboxes the you could iterate thru the form's controls (Controls Collection)
Declare a boolean... set it to true
Inside the loop
Ask if it is textbox (TypeOf Is Textbox) and if it's filled... set the boolean to false if empty
...
Outside of the loop
Ask for the boolean variable
If it is true then enable command button
This is VB6 forum :DQuote:
Ask if it is textbox (TypeOf Is Textbox)
TypeOf is a valid keyword in VB6
The next piece of code changes the forecolor of all Text Box controls on Form1 to red:
You'll also find this approach useful for many other tasks, such as unchecking all of the CheckBox controls.Code:Dim c As Control
For Each c In Form1.Controls
If TypeOf c Is TextBox Then
c.ForeColor = vbRed
End If
Next
In form load make command button disabled. In each textbox's change event check for emptiness (Len(Text...)=0) of all text boxes, if any one is empty keep Command Button disabled.
Along the same lines as Smartchap, you can grab the length of a series of textboxes and multiply those lengths together. If any textbox is empty, it will equate to 0 and any number multipled by 0 becomes 0 and 0 will equate to False. Example:Code:Option Explicit
Private Sub EnableButton()
Command1.Enabled = Len(Text1.Text) _
* Len(Text2.Text) _
* Len(Text3.Text) _
* Len(Text4.Text) _
* Len(Text5.Text)
End Sub
Private Sub Command2_Click()
EnableButton
End Sub
hello all
thanks for all but i want that the command button disabled at default then enabled after After writing anything on textbox
hello
yes i've tried it but the problem is that the command button shift to disabled after writing anything on textbox and clean it
i'm loocking for any way to disable the command button at default
for exemple if you open my program you will see the command button disabled at default
Set Enabled = False in design mode, on the button's property page.
thanks so much
now it work but there is a small mistake
i've 3 textbox after writing anything on one textbox, the command button shift to Enabled without considering the two other textbox
this is the codethat i've used from 4x2y:
Code:Private Sub Text1_Change()
If Text1.Text = vbNullString Then
calculateBtn.Enabled = False
Else
calculateBtn.Enabled = True
End If
End Sub
Private Sub Text2_Change()
If Text2.Text = vbNullString Then
calculateBtn.Enabled = False
Else
calculateBtn.Enabled = True
End If
End Sub
Private Sub Text3_Change()
If Text3.Text = vbNullString Then
calculateBtn.Enabled = False
Else
calculateBtn.Enabled = True
End If
End Sub
Or he can do it like this:
Code:Private Function EnableButton() As Boolean
EnableButton = Len(Text1.Text) _
* Len(Text2.Text) _
* Len(Text3.Text) _
* Len(Text4.Text) _
* Len(Text5.Text)
End Function
'In Every Change-Event of the particular TextBoxes:
Private Sub Text1_Change()
Command1.Enabled = EnableButton
End Sub
'The same in Form_Load-Event
Private Sub Form_Load()
Command1.Enabled = EnableButton
End Sub
To stay in your example:
vb Code:
Private Function EnableButton() As Boolean EnableButton = Len(Text1.Text) * Len(Text2.Text) * Len(Text3.Text) End Function Private Sub Text1_Change() Command1.Enabled = EnableButton End Sub Private Sub Text2_Change() Command1.Enabled = EnableButton End Sub Private Sub Text3_Change() Command1.Enabled = EnableButton End Sub
What is the error message?