Changing Name of View

TedN

Member
Licensed User
Longtime User
The name of a View created with the Designer can be changed in the Designer.
How does one change the name of a view which was created programatically?

Is there something like lblTemp.Name = "lblFirstName"


Thanks,
 

agraham

Expert
Licensed User
Longtime User
Views don't actually have a name. Variables containing references to a View are named but the Views themselves are anonymous. The following code fragment is legal, but confusing.

B4X:
Dim Label1, Label2, Label3 As Label
...
Label1.Initialize("Label1")
Label2.Initialize("Label2")
...
Label3 = Label1
Label1 = Label2
Label2 = Label3
Also you can do this
B4X:
For i = 0 To 9
    Dim Lbl As Label ' makes a new Label instance
    Lbl.Initialize("Lbl")
    Activity.AddView(Lbl, 0, 40*i, 100, 30)
Next
To identify an individual View you will need to use its Tag property that can usefully contain any sort of Object, including a custom Type.
 
Upvote 0

TedN

Member
Licensed User
Longtime User
Thanks for your replies. The problem I have is that my app needs to allow the user to create views.

So I'm trying to find a way that users can provide their own name for a view.

If say "Label" is clicked on a pull down menu I can get the code to create a label, but I obviously need to provide a name, say lblTemp, in the Dim statement, and then Initialize it, etc.

The problem is that if the user creates a second label it will have the same name as the first, i.e. lblTemp.

Is there a way around this problem.

Thanks
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
If the label (in your example) is created in a sub, the name can be the same every time, as when you dim the label it creates a new instance of it, once the label is created the name is not important.

if you need to link the label to the name entered by the user you could use a map With the user-entered name as the key.
 
Last edited:
Upvote 0

TedN

Member
Licensed User
Longtime User
I've not used Map before. If I understand correctly I could pair each instance of lblTemp (value) with the user's label name (key).

I'll play around with this and get a better understanding.

One quick question, can a label's property be applied to a key, e.g. if a user entered lblDate as the name for a label which is entered as a key in the map, can say Top be applied to lblDate, i.e. lblDate.Top.

Thanks
 
Upvote 0

TedN

Member
Licensed User
Longtime User
OK. I see how Map works now. Ignore my last question.

This should help a lot now.


Thanks
 
Upvote 0
Top