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

Labels could not be removed from the form due to their names. #47

Closed dynamicboy closed 5 months ago

dynamicboy commented 10 months ago

I am creating an application to split long images. I add labels through code to display the sub-images. When I choose another long image, these labels need to be removed from the form. I attempted to remove them using the Me.RemoveControl(nameOfLbl) function, but I failed.

Version:2.8.8.1 Details: Demo code as below:

For I = 1 To 3
   Name = "Label" + I
   Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30)
   Lbl.BackColor = Colors.YellowGreen
Next

For I = 1 To 3
   Name = "Label" + I
   Me.RemoveControl(Name)
Next

For I = 1 To 4
   Name = "Label" + I
   Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30)
   Lbl.BackColor = Colors.YellowGreen
Next

Running the form will result in an error. Screenshot: 2024-01-08_201108

However, when I change the label name prefix to "lbl":

For I = 1 To 3
   Name = "lbl" + I
   Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30)
   Lbl.BackColor = Colors.YellowGreen
Next

For I = 1 To 3
   Name = "lbl" + I
   Me.RemoveControl(Name)
Next

For I = 1 To 4
   Name = "lbl" + I
   Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30)
   Lbl.BackColor = Colors.YellowGreen
Next

Then, when I run the form, the four labels are displayed correctly. 2024-01-08_201510

I tried to change the name prefix to "lblS" or "lblSubImage", but I always encounter an error indicating that there is a label with the same name. It's really weird.

VBAndCs commented 10 months ago

The key of any control consists of the formNme.ControlName. This key is returned for you by the Form.AddXX method. If you want to remove the control, send this key to the Remove method, so you should save labels to an array: labels= [] for ....

Lbl = Me.AddLabel(

labels[i] = lbl Next and use labels[i] in the removal loop. or avoid all of this this and let sVB do the work for you:

C = Me.Controls For I = C.Count To 1 Step -1 Control1 = C[I] If Control1.TypeName = ControlTypes.Label Then Me.RemoveControl(Control1) EndIf Next


From: dynamicboy @.> Sent: Monday, January 8, 2024 12:20 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Subscribed @.***> Subject: [VBAndCs/sVB-Small-Visual-Basic] Labels could not be removed from the form due to their names. (Issue #47)

I am creating an application to split long images. I add labels through code to display the sub-images. When I choose another long image, these labels need to be removed from the form. I attempted to remove them using the Me.RemoveControl(nameOfLbl) function, but I failed.

Version:2.8.8.1 Details: Demo code as below:

For I = 1 To 3 Name = "Label" + I Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30) Lbl.BackColor = Colors.YellowGreen Next

For I = 1 To 3 Name = "Label" + I Me.RemoveControl(Name) Next

For I = 1 To 4 Name = "Label" + I Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30) Lbl.BackColor = Colors.YellowGreen Next

Running the form will result in an error. Screenshot: 2024-01-08_201108.png (view on web)https://github.com/VBAndCs/sVB-Small-Visual-Basic/assets/7421525/b34b5853-6918-4b6d-a270-6734a6a3ad88

However, when I change the label name prefix to "lbl":

For I = 1 To 3 Name = "lbl" + I Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30) Lbl.BackColor = Colors.YellowGreen Next

For I = 1 To 3 Name = "lbl" + I Me.RemoveControl(Name) Next

For I = 1 To 4 Name = "lbl" + I Lbl = Me.AddLabel(Name, 0, I * 50, 50, 30) Lbl.BackColor = Colors.YellowGreen Next

Then, when I run the form, the four labels are displayed correctly. 2024-01-08_201510.png (view on web)https://github.com/VBAndCs/sVB-Small-Visual-Basic/assets/7421525/4ba559db-fabb-4821-97a2-1afeee7c8069

I tried to change the name prefix to "lblS" or "lblSubImage", but I always encounter an error indicating that there is a label with the same name. It's really weird.

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

[https://s-install.avcdn.net/ipm/preview/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif]https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free.www.avast.comhttps://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail