sender returning last tag in scrollview

AnverDawood

New Member
Licensed User
Longtime User
I am using a scrollview to display all files in a folder

For the life of me I cannot understand why the click event returns only the last value in the list

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("ScrollView")
Dim files As List
lblFolder.Text=File.DirRootExternal
files = File.ListFiles(File.DirRootExternal) 'get all files in this folder
ScrollView1.Panel.Height = 20dip * files.Size
For i = 0 To files.Size - 1
Label1.Initialize("Label1")
Label1.Text = i
Label1.Tag=i
ScrollView1.Panel.AddView(Label1, 1dip, 1dip + i * 20dip, 20dip, 18dip)
Log(Label1.Text)
Label2.Initialize("Label2")
Label2.Text = files.Get(i)
Label2.Tag=files.Get(i)
ScrollView1.Panel.AddView(Label2, 25dip, 1dip + i * 20dip, ScrollView1.Width, 18dip)
Log(Label2.Text)
Next
End Sub

Sub Label1_Click
Dim Send As Label
Send=Sender
Activity.Title=Send.Text
Msgbox(Send.Tag,Send)
End Sub
Sub Label2_Click
Dim Send As Label
Send=Sender
Activity.Title=Send.Tag
Msgbox(Send.Tag,Send)
End Sub
 
Because this is what you are making it do. Move all your Dim statements inside the loop. Something like

For I = 0 To 5
Dim lbl As Label
lbl.Tag = i
Next

With a Dim outside the loop, you refer to the same label. By dimming inside, you are recreating the object at every iteration.

Sent from my Next7P12 using Tapatalk 2
 
Upvote 0
Thank you for your response

Hey Penko,

Thank you kindly for your prompt response. Your solution worked perfectly.

Regards,

Anver
 
Upvote 0
Back
Top