Nuke2000 09-08-2000, 12:17 PM ok i want to click on a botton and hide all the combo boxs on that form. this is the code i uses
Private Sub yes_Click()
If Combo1.Visible = True Then
Combo1.Visible = False
Else
Combo1.Visible = True
End If
End Sub
but there could be up to 20-25 combo boxs on one form is there a better way the doing each combo box like .
Private Sub yes_Click()
If Combo1.Visible = True Then
Combo1.Visible = False
Else
Combo1.Visible = True
end if
If Combo2.Visible = True Then
Combo2.Visible = False
Else
Combo2.Visible = True
End If
If Combo3.Visible = True Then
Combo3.Visible = False
Else
Combo3.Visible = True
end If
End Sub
and so on there has to be a better way ?
BillSoo 09-08-2000, 12:34 PM It looks like pressing the button toggles the combo boxes, that is, if they are visible, it makes them invisible and if they are invisible, it makes them visible.
So you could do:
Private Sub yes_Click()
Combo1.Visible = Not Combo1.Visible
Combo2.Visible = Not Combo2.Visible
...
End Sub
Another think you might have done, although you may not want to change now, is to make an array of comboboxes instead of each individual box. Then you could just iterate through the array.
Private Sub yes_Click()
dim i%
For i% = 0 to Ubound(ComboArray)
ComboArray(i%).Visible = Not ComboArray(i%).Visible
Next i%
End Sub
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Nuke2000 09-08-2000, 01:08 PM This might be a stupit thing to say on my part but what is
Private Sub yes_Click()
dim i%
For i% = 0 to Ubound(ComboArray)
ComboArray(i%).Visible = Not ComboArray(i%).Visible
Next i%
End Sub
this can you go in to more details in what thats does and how it works. iam sorry for asking so much but i like to now how it works befor useing it.
A better solution would be :-
Private Sub Command1_Click()
For x = 0 to Controls.Count-1
If Left(Controls(x).Name, 3) = "cmb" Then
Controls(x).Visible = Not Controls(x).Visible
Endif
Next
This will toggle on/off all of your Combo Boxes, as long as your naming convention for Combo boxes is 'cmbComboname', as it should be.
Hope it helps !
qwertyjustin 09-08-2000, 01:16 PM mm.. well.. i'm assuming you could just add arrays in, such that...
Combo1(i).visible = flase
[because there's that general rule that arrays greatly reduce code];)
, your best bet is arrays champ;)
BillSoo 09-08-2000, 01:32 PM Good one Dazz,
You could also make the test:
If typeof Controls(x) is ComboBox then
That way, it doesn't matter what he called them.
Nuke2000:
The comboarray code thing was just to suggest what the code *could* have looked like IF you had coded using arrays of combo boxes instead of declaring each box separately.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Nuke2000 09-08-2000, 01:41 PM ok thank you all but you all lost me BIG TIME.
all my combo boxs are name combo1 combo2 and so on
but some forms have 3 combo boxs and some have 30.
how who i write something that will get them all even if there are only 1 combox on one form or 30 on one form ?
if you need a pic go here
http://www6.50megs.com/nuke3000/premonition.jpg?50m=image
all the combo boxs are named combo1 and so on
Even better one BillSoo, I didn't think of the typeof command, but I think the naming convention was still worth a mention.
If I had a hat on I'll take it of to ya!
Don't worry Nuke2000 I'll walk you through it!
Private Sub Command1_Click() 'This is the Button Sub
For x=0 to Controls.Count-1 'loop for every Control on your form
if typeof Controls(x) is ComboBox then 'If the contol is a ComboBox
Controls(x).Visible = Not Controls(x).Visible 'Toggle the visability On/Off
Endif
Next
This way it does not matter how many controls you have as the For Next loop goes through all controls on your form.
Get It !
BillSoo 09-08-2000, 01:51 PM On your form, in the yes button click event, add code that iterates through all controls on that form. If the control is a combobox, toggle it's visible state.
Private Sub yes_Click()
Dim i%
For i% = 0 to me.Controls.Count - 1
If TypeOf me.Controls(i%) is ComboBox Then
me.Controls(i%).Visible = Not me.Controls(i%).Visible
EndIf
Next i%
End Sub
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Valkyrie 09-08-2000, 01:54 PM Hey Nuke...use this. It will toggle your combo boxes on and off regardless of the name or number of them!
<font color=blue><pre>Dim x As Integer
For x = 0 To Controls.Count - 1
If TypeOf Controls(x) Is ComboBox Then
Controls(x).Visible = Not Controls(x).Visible
End If
Next
</font color=blue></pre>
Cheers.
Quote of the moment....
"My job is so top secret even I don't know what I'm doing!"
BillSoo
The horse was flogged so hard it actually expired.
Nuke2000
Just copy the code into your button click sub and watch what happens, it will work trust us (speaks for Dazz and Bill)
Nuke2000 09-08-2000, 02:46 PM thank you all i did not expect so many replys. and they all work great think you all
NoahBody 09-08-2000, 02:59 PM Here let me confuse you some more:
<------------------------ cut here ------------------------>
Private Sub Command1_Click() 'This is the Button Sub
Dim varControl as Variant
For Each varControl in Controls 'loop for every Control on your form
if typeof varControl is ComboBox then 'If the contol is a ComboBox
varControl.Visible = Not varControl.Visible 'Toggle the visability On/Off
Endif
Next varControl
<------------------------ cut here ------------------------>
I like this because it doesn't depend on indexes or counts.
Haven't actually tried this code, but, it should be close.
->Noah
|