Question:
In Visual Basic, How do I say in coding: "If not all textboxes are filled, then button is disabled"?
samyb0ii
2011-05-08 13:17:11 UTC
I've gotten this far, but doesn't seem to work:
If Textbox1.Text = "" Then Button1.Enabled = False Else Button1.Enabled = True
Seven answers:
texasmaverick
2011-05-08 14:43:42 UTC
The following code will work, using some text boxes and a button. You can use the code in other manners, such as putting it in TextBox change events. I used it with the button. I tested it.



Enjoy



Public Class Form1



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Cnt As Integer = 1



For Z = 0 To Me.Controls.Count - 1

If (TypeOf (Me.Controls(Z)) Is TextBox) Then

If Len(Me.Controls(Z).Text) = 0 Then

Cnt = 0

End If

If Cnt = 0 Then

Button1.Enabled = False

Else

Button1.Enabled = True

End If

End If

Next

End Sub



Edit

Deleted a line of code which was useless (had an apostrophe in front of it)



Edit2

In case you wish to look at only a select group of textboxes, use the following code. Note this code looks at only textbox2 and textbox4.





Public Class Form1







Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Cnt As Integer = 1





For Z = 0 To Me.Controls.Count - 1

If (TypeOf (Me.Controls(Z)) Is TextBox) Then

If (Me.Controls(Z)).Name.Contains(2) Or (Me.Controls(Z)).Name.Contains(4) Then

If Len(Me.Controls(Z).Text) = 0 Then

Cnt = 0

End If

If Cnt = 0 Then

Button1.Enabled = False

Else

Button1.Enabled = True

End If

End If

End If

Next

End Sub

End Class



EDIT3

I just noticed you said none of the answers worked. Either your vb.net or my vb.net is corrupt. The codes (both) work perfectly on my computer. I have 4 textboxes (named textbox1, textbox2, textbox3 and textbox4, and a button.



If you have named yopur textboxes by function, rather than by numbers, this will not work. Change your textbox names to their default names and my code works. If you can't change them back, you will have a hard time finding your answer.





TexMav
Nick
2011-05-11 07:57:47 UTC
if TextBox1.Text = Nothing or TextBox2.Text = Nothing or .......... etc etc then

button1.enabled = false

end if



Or you could, make just make an array.



Dim myTextBoxData(4) as String 'To hold 5 strings, hold as many as you like



Store your text in the array.



myTextBoxData(0) = Textbox1.Text 'And so on for each TextBox



Then iterate through the array and check for any null entries. You can then also grab the data from the array to put it where ever it's supposed to go next.



Dim AllBoxesHaveData as Boolean = True



For i = 0 to 4

if str(i) = Nothing Then

AllBoxesHaveData = False

end if

Next



If AllBoxesHaveData = True Then

Button1.enabled = True

else

Button1.enabled = false

End If
Leaira Hathaway
2011-05-08 13:19:34 UTC
There is no way. You need to test each individual text box to make sure it has text in it. Your code only tests the text in Textbox1. Repeat the code for each text box. But don't put a Button1.Enabled=True until the end after you have verified all text boxes. Exit the test routine before reaching the button enable line, if any text boxes are found to be empty.
anonymous
2011-05-08 13:31:48 UTC
here's two methods (the second one is the better one if you have more than few boxes to check, the first one is alright for a small number of boxes but where there are say a million text boxes being used, the second will be a lot faster)





method 1:

--------------



i would keep a counter.



let's say you have 5 text boxes.



Dim tbcount as integer 'this will be my counter

tbcounter = "0"





if txtbox1.txt <> "" then

tbcounter = tbcounter +1

end if



if txtbox2.txt <> "" then

tbcounter = tbcounter +1

end if



if txtbox3.txt <> "" then

tbcounter = tbcounter +1

end if



if txtbox4.txt <> "" then

tbcounter = tbcounter +1

end if



if txtbox5.txt <> "" then

tbcounter = tbcounter +1

end if





'final check, if counter is less than 5 then one was blank



if counter < 5 then

button1.enabled = false

end if































method 2

-------------





if your text boxes are in an array it can be a lot more easy (each text box is txtbox(n) where n is a position in the array):





dim x as integer

dim i as boolean 'i will act as a flag. set i to true initially.

' each time we check a text box if the box is filled in we do nothing

' if box is blank we change indicator to false

if the value at the end is false, then we know a box was blank



i = "True"



for x = 1 to 5

if txtbox(x) = "" then

i = "False"

end if

next





if i = "False" then

button1.enabled = false

end if





the advantage of this one is you can increase the number of boxes as much as you like and only line "for x = 1 to 5" will need to change
anonymous
2011-05-08 13:28:44 UTC
You can either abuse stacked if statements i.e. (assuning you have 2 textboxes)



button1.enabled = false

if textbox1.text <>"" then

if textbox2.text<>"" then

button1.enabled = true

end if

end if



OR you could just abuse if statements i.e. (again, for 2 textboxes)

button1.enabled = true

if textbox1.text ="" then button1.enabled = false

if textbox2.text ="" then button1.enabled = false



OR you could abuse the and operator i.e. (for 2 textboxes)

button.enabled = false

if (textbox1.text <> "") and (textbox2.text <>"") then

button.enabled = true

end if



Hope I helped.
tenofsky
2016-10-19 05:19:37 UTC
ok. set the buttons text textile properties to "a" "b" and "c" double click on the button on your style and examine out this code in its click journey handler... outputTextbox.text textile &= sender.text textile you ought to use "sender.text textile" so as which you will in basic terms replica and previous that to all of the buttons click events. the sender merchandise is what had led to that journey in actuality. in case you dont choose for to apply "sender" you ought to use the call of the particular button additionally
Daniel
2011-05-08 13:22:06 UTC
On Button Click, Im assuming you want it to do nothing if a textbox is not filled out. Here is the code:



___________________________________________________________________________________



Public Class Form1



Private Sub Button1_Click

If textbox1.text = ("") Then

MsgBox("Please Fill Something Out In The Textbox...")

Else

' "What you want it to do here"

End If

End Sub

End Class


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...