Massy Member Licensed User Longtime User May 22, 2015 #1 Let's say I have 10 labels named "label0", "label1" and so on... and a list with 10 strings "listNames(10) As String" and I want programmatically send the 10 strings that are in the list at every label depending on their name... is it possible to shorten the code doing something like this?: For i = 0 To 9 label(i).text = listNames(i) Next Thansk for any help Massy
Let's say I have 10 labels named "label0", "label1" and so on... and a list with 10 strings "listNames(10) As String" and I want programmatically send the 10 strings that are in the list at every label depending on their name... is it possible to shorten the code doing something like this?: For i = 0 To 9 label(i).text = listNames(i) Next Thansk for any help Massy
imbault Well-Known Member Licensed User Longtime User May 22, 2015 #2 You should do that : Code: Copy Sub Globals 'These global variables will be redeclared each time the activity is created. 'These variables can only be accessed from this module. Dim Label() As Label End Sub Sub Activity_Create(FirstTime As Boolean) 'Do not forget to load the layout file created with the visual designer. For example: 'Activity.LoadLayout("Layout1") Dim Label(10) As Label For i = 0 To 9 Label(i).Initialize("") Label(i).text = listNames(i) Activity.AddView(Label(i),0dip,(i+1)*10dip,350dip,10dip) Next End Sub Upvote 0
You should do that : Code: Copy Sub Globals 'These global variables will be redeclared each time the activity is created. 'These variables can only be accessed from this module. Dim Label() As Label End Sub Sub Activity_Create(FirstTime As Boolean) 'Do not forget to load the layout file created with the visual designer. For example: 'Activity.LoadLayout("Layout1") Dim Label(10) As Label For i = 0 To 9 Label(i).Initialize("") Label(i).text = listNames(i) Activity.AddView(Label(i),0dip,(i+1)*10dip,350dip,10dip) Next End Sub
Massy Member Licensed User Longtime User May 22, 2015 #3 Hey thanks for your answer, but... what if the labels are already in the Designer and I don't want to add them programmatically with Activity.AddView? Upvote 0
Hey thanks for your answer, but... what if the labels are already in the Designer and I don't want to add them programmatically with Activity.AddView?
S sorex Expert Licensed User Longtime User May 22, 2015 #4 if you give them a tag value like label0 label1 etc you could run through all the views and check if the tag equals the loop value and set the text if needed. this works for both designer or scripted added views. search for getallviewsrecursive Upvote 0
if you give them a tag value like label0 label1 etc you could run through all the views and check if the tag equals the loop value and set the text if needed. this works for both designer or scripted added views. search for getallviewsrecursive
Massy Member Licensed User Longtime User May 22, 2015 #5 Thanks Sorex, I got your point of using tags... I only don't have clear how to use them with getallviewsrecursive... I tryed to search for an example but all I found made me more confused.... could you give me an example? Thanks a lot Upvote 0
Thanks Sorex, I got your point of using tags... I only don't have clear how to use them with getallviewsrecursive... I tryed to search for an example but all I found made me more confused.... could you give me an example? Thanks a lot
DonManfred Expert Licensed User Longtime User May 22, 2015 #6 Put your 10 labels into a list a list after you have loaded the layout Code: Copy Dim Label(10) As Label Label(0) = lblxy1 Label(1) = lblxxx Label(2) = lbltest [...] then you can use something like Code: Copy For i = 0 To 9 dim lbl As Label = Label(i) lbl.text = listNames(i) Next Upvote 0
Put your 10 labels into a list a list after you have loaded the layout Code: Copy Dim Label(10) As Label Label(0) = lblxy1 Label(1) = lblxxx Label(2) = lbltest [...] then you can use something like Code: Copy For i = 0 To 9 dim lbl As Label = Label(i) lbl.text = listNames(i) Next
S sorex Expert Licensed User Longtime User May 22, 2015 #7 not sure if this works but it will be close Code: Copy for x=0 to 9 setLabelText("label"&x,labeltexts(x)) next sub setLabelText(lTag as string,lText as string) For Each v As View In pnlLabels.GetAllViewsRecursive If v.tag=lTag Then v.text=lText Next end sub Upvote 0
not sure if this works but it will be close Code: Copy for x=0 to 9 setLabelText("label"&x,labeltexts(x)) next sub setLabelText(lTag as string,lText as string) For Each v As View In pnlLabels.GetAllViewsRecursive If v.tag=lTag Then v.text=lText Next end sub
DonManfred Expert Licensed User Longtime User May 22, 2015 #8 Better check it it is really a label... And you need to bind the "v" to an objectinstance (dim) Code: Copy sub setLabelText(lTag as string,lText as string) For Each v As View In pnlLabels.GetAllViewsRecursive if v is Label then dim lbl as Label = v If v.tag=t Then v.text=lText Next end if Upvote 0
Better check it it is really a label... And you need to bind the "v" to an objectinstance (dim) Code: Copy sub setLabelText(lTag as string,lText as string) For Each v As View In pnlLabels.GetAllViewsRecursive if v is Label then dim lbl as Label = v If v.tag=t Then v.text=lText Next end if
S sorex Expert Licensed User Longtime User May 22, 2015 #9 I don't use the label check since I don't tag an image with label or lbl but you're right for the dim, otherwise it doesn't know it has that property. Upvote 0
I don't use the label check since I don't tag an image with label or lbl but you're right for the dim, otherwise it doesn't know it has that property.
klaus Expert Licensed User Longtime User May 22, 2015 #10 As the Labels already exist, I would do it that way: Code: Copy Dim Labels(10) As Label Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9) For i = 0 To 9 Labels(i).text = listNames(i) Next Upvote 0
As the Labels already exist, I would do it that way: Code: Copy Dim Labels(10) As Label Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9) For i = 0 To 9 Labels(i).text = listNames(i) Next
Massy Member Licensed User Longtime User May 22, 2015 #11 klaus said: As the Labels already exist, I would do it that way: Code: Copy Dim Labels(10) As Label Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9) For i = 0 To 9 Labels(i).text = listNames(i) Next Click to expand... Very good Klaus! Thanks a lot Upvote 0
klaus said: As the Labels already exist, I would do it that way: Code: Copy Dim Labels(10) As Label Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9) For i = 0 To 9 Labels(i).text = listNames(i) Next Click to expand... Very good Klaus! Thanks a lot
walterf25 Expert Licensed User Longtime User May 22, 2015 #12 Massy said: Very good Klaus! Thanks a lot Click to expand... Yeah very good Klaus, you passed the test, LOL Upvote 0
Massy said: Very good Klaus! Thanks a lot Click to expand... Yeah very good Klaus, you passed the test, LOL