VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
233 stars 16 forks source link

Array of Controls #90

Closed Drugo67 closed 1 month ago

Drugo67 commented 1 month ago

I can't create a label array (or other control) not in runtime, but with the Form designer. Is it possible to do that?

VBAndCs commented 1 month ago

Controls array was a feature in VB6, but removed from VB .NET. It is also not supported in sVB, but you can get the desired effect by other means:

  1. design a label, copy it, paste it multiple times, and rearrange the copies.
  2. In code, you can iterate through your labels by this code:

ForEach _Label In Me.Controls If _Label.TypeName = ControlTypes.Label Then _Label.Text = "Hello" EndIf Next

If you find this answer useful, please close this issue. Thanks


From: Drugo67 @.> Sent: Tuesday, October 8, 2024 1:28 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Subscribed @.***> Subject: [VBAndCs/sVB-Small-Visual-Basic] Array of Controls (Issue #90)

I can't create a label array (or other control) not in runtime, but with the Form designer. Is it possible to do that?

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/90, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVR3J5KE6H4ENQGW723Z2PMW3AVCNFSM6AAAAABPSHGHE6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGU3TGMRRGE2TKNY. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Drugo67 commented 1 month ago

Thanks for the quick reply, but it's not very clear to me because I was used to VB6. But if I wanted to access and change the text only to some specific labels (for example label1, label2, label3...label20) and not to mylabel1 and mylabel2 how should the code be? I mean what is the key to access a certain label[x] with a for-next or forEach loop? Could you give me a concrete example? Thanks in advance

VBAndCs commented 1 month ago

If you want to access a specific label use its name, like: label4.Text = "….." I used the index array of vb6 but I never needed it in VB .NET, because you can always use the Controls collection. If you want to get the labels array, use this code, but I don't find any need for such code, as the ForEach loop gives you access to each label directly: LabelsArr = [] ForEach _Control In Me.Controls If _Control.TypeName = ControlTypes.Label Then LabelsArr.AddItem(_Control) EndIf Next So, you can use the LabelsArr as you want. But note that the labels order wil be the same order of adding them on the form, regardless of how you name them. Lukily, the designer auto increments the numeric suffics of the labels as you paste them on the form. But I don't adivce to use such design as it is not practical. In real world, if you want to do something to all labels except one or two, you normally relize these two by some event (like mouse clic), so you should handle this event and set that label to a variable (say: clickedLabel), then except this label from what you are doing to others. I wrote a good example to show you how to highlight the mouse-over label, and how to toggle the clicked label back color: _Color = Colors.LightGreen

ForEach _Label In Me.Controls _Label.OnClick = Label_OnClick _Label.OnMouseEnter = Label_OnMouseEnter _Label.OnMouseLeave = Label_OnMouseLeave _Label.BackColor = Colors.LightGreen Next

' ------------------------------------------------ Sub Label_OnClick() ForEach _label In Me.Controls If _label = Event.SenderControl Then If _Color = Colors.LightGreen Then _label.BackColor = Colors.Red _label.FontBold = True Else _label.BackColor = Colors.LightGreen _label.FontBold = False EndIf _Color = _label.BackColor ElseIf _label.TypeName = ControlTypes.Label Then _label.BackColor = Colors.LightGreen _label.FontBold = False EndIf Next EndSub

' ------------------------------------------------ Sub Label_OnMouseEnter() _Label = Event.SenderControl _Color = _Label.BackColor If _Color = Colors.Red Then _Label.BackColor = Colors.Goldenrod Else _Label.BackColor = Colors.Gold EndIf

EndSub

' ------------------------------------------------ Sub Label_OnMouseLeave() _Label = Event.SenderControl _Label.BackColor = _Color EndSub


From: Drugo67 @.> Sent: Tuesday, October 8, 2024 1:53 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Comment @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] Array of Controls (Issue #90)

Thanks for the quick reply, but it's not very clear to me because I was used to VB6. But if I wanted to access and change the text only to some specific labels (for example label1, label2, label3...label20) and not to mylabel1 and mylabel2 how should the code be? I mean what is the key to access a certain label[x] with a for-next or forEach loop? Could you give me a concrete example? Thanks in advance

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/90#issuecomment-2399916134, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVVUFVEXZVCFWUD7Z5DZ2PPXNAVCNFSM6AAAAABPSHGHE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJZHEYTMMJTGQ. You are receiving this because you commented.Message ID: @.***>

Drugo67 commented 1 month ago

Thank You very Much!!!