Author | Message | Time |
---|---|---|
LivedKrad | I'm putting together a clientban moderation option, and I have checkboxes representing each client that can be banned. [code] Dim tmpString As String If chkSEXP.value = vbChecked Then tmpString = tmpString & "SEXP:" End If If chkSTAR.value = vbChecked Then tmpString = tmpString & "STAR:" End If If chkD2DV.value = vbChecked Then tmpString = tmpString & "D2DV:" End If If chkD2XP.value = vbChecked Then tmpString = tmpString & "D2XP:" End If If chkW2BN.value = vbChecked Then tmpString = tmpString & "W2BN:" End If If chkWAR3.value = vbChecked Then tmpString = tmpString & "WAR3:" End If If chkW3XP.value = vbChecked Then tmpString = tmpString & "W3XP:" End If tmpClients() = Split(tmpString, ":") modOpts.cbClientList = Replace(tmpString, ":", "") [/code] Here, I develope a string that contains all of the clients that the user wishes to ban. I then split up this string to an array by delimeter ":", and add them to the array tmpClients(). Now, at the form's load event I want to check each box that applies to a desired client to ban, but I do not want to set up several unnecessary control statements to do check each box that applies. So, I set up a simple For statement to cycle through the elements: [code] (On form load) Dim i As Byte For i = 0 To UBound(tmpClients) - 1 chk(tmpClients(i)).value = vbChecked Next i [/code] When I compile and test however, I get a "Sub or function not defined error". Obviously this is because it's trying to read chk as a function/sub named "chk()". Yet, I don't know any other way of appending the client ID to the end of "chk" so that it matches each CheckBox. (BTW, my checkboxes are named as chkSEXP, chkSTAR, etc.) Any ideas? | November 2, 2004, 7:43 PM |
LivedKrad | [BUMP] This is urgent, someone please respond. | November 2, 2004, 11:34 PM |
Myndfyr | Did you think of maybe creating an array of CheckBoxes? | November 2, 2004, 11:53 PM |
Adron | [quote author=MyndFyre link=topic=9398.msg87159#msg87159 date=1099439584] Did you think of maybe creating an array of CheckBoxes? [/quote] He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays? | November 3, 2004, 1:20 AM |
UserLoser. | [quote author=Adron link=topic=9398.msg87172#msg87172 date=1099444831] [quote author=MyndFyre link=topic=9398.msg87159#msg87159 date=1099439584] Did you think of maybe creating an array of CheckBoxes? [/quote] He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays? [/quote] Yes, my example: Add checkboxes to a form, name form to form1, slap on a command button: [code] Private Sub Command1_Click() Dim Apple For Each Apple In Form1 If (Apple.Value = vbChecked) Then Debug.Print Apple.Name & " is checked" End If Next Apple End Sub [/code] | November 3, 2004, 1:31 AM |
Myndfyr | So could you then adapt your code to: [code] Private Sub Command1_Click() Dim Apple For Each Apple In Form1 If Apple.Name = "chk" & GameID Then ' ... do something End If Next Apple End Sub [/code] ? | November 3, 2004, 2:04 AM |
UserLoser. | [quote author=MyndFyre link=topic=9398.msg87190#msg87190 date=1099447486] So could you then adapt your code to: [code] Private Sub Command1_Click() Dim Apple For Each Apple In Form1 If Apple.Name = "chk" & GameID Then ' ... do something End If Next Apple End Sub [/code] ? [/quote] It should | November 3, 2004, 2:18 AM |
LivedKrad | [quote author=Adron link=topic=9398.msg87172#msg87172 date=1099444831] [quote author=MyndFyre link=topic=9398.msg87159#msg87159 date=1099439584] Did you think of maybe creating an array of CheckBoxes? [/quote] He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays? [/quote] Yes Adron, I do. Anyway, for some reason I wasn't thinking that you could treat control names as strings, such as "chk" & tmpClients(i).value = vbChecked. If you can, I guess I'm horribly mistaken and stupid. :P | November 3, 2004, 2:00 PM |
Grok | If iterating over all controls in a Form, be sure to check the control type before checking properties. Value might not be present in all of the controls, causing an error that property is not supported by object. I think "TypeName()" will return the name of the control type, i.e. CheckBox. I dropped a control of each basic type on a form and ran this code: [code] Dim C As Control Dim M As String For Each C In Form1.Controls M = M & vbCrLf & TypeName(C) Next ClipBoard.SetText M, vbCFText [/code] Producing this output: CommandButton ListBox ComboBox OptionButton CheckBox CommandButton Frame TextBox Label | November 3, 2004, 3:26 PM |
LivedKrad | Why is that needed when I know the type of property I'm dealing with? Anyway, that's not my problem. Is it possible to refer to a property's name through a string? For instance, I have 6 CheckBoxes named chk1, chk2, chk3, chk4, chk5, and chk6. I have an array consisting of (1,3,4,5). Can I create a For loop to select the checkboxes like so? [code] Dim i as byte For i = 0 to Ubound(Array) "chk" & Array(i) & ".value" = vbChecked next i [/code] Would this work? Is there a variation that will make it work? Is it all possible? | November 3, 2004, 6:09 PM |
Adron | [quote author=LivedKrad link=topic=9398.msg87235#msg87235 date=1099505385] Would this work? Is there a variation that will make it work? Is it all possible? [/quote] The obvious way is to make them an array. Other than that, yes, it's possible to change the syntax a bit and make it work. | November 3, 2004, 6:13 PM |
LivedKrad | If I didn't (for some stupid reason) want to choose the obvious way, how would I go about using my idea yet actually making it work? | November 3, 2004, 6:15 PM |
Adron | [quote author=LivedKrad link=topic=9398.msg87237#msg87237 date=1099505728] If I didn't (for some stupid reason) want to choose the obvious way, how would I go about using my idea yet actually making it work? [/quote] Something like this works (not to say it's a good solution): [code] Private Function ctrl(ByVal name As String) As Control Dim C As Control For Each C In Me.Controls If C.name = name Then Set ctrl = C: Exit Function Next End Function Private Sub Command1_Click() Dim i As Byte Dim a(0 To 1) As Integer a(0) = 1 a(1) = 3 For i = 0 To UBound(a) ctrl("chk" & a(i)).Value = vbChecked Next i End Sub [/code] | November 3, 2004, 8:56 PM |