G GuyBooth Active Member Licensed User Longtime User Jan 14, 2013 #1 This sounds like a very basic question - but I don't know how to do it! I want to use a label many many times. Every time I use it it will have the same charactersitics (color, size, text size etc) but for each copy I will want to add different text. How do I do this without having to recreate all the properties every time? Somehow set the defaults to be the ones I want? I would like to initialize it just once, and then reuse it ...
This sounds like a very basic question - but I don't know how to do it! I want to use a label many many times. Every time I use it it will have the same charactersitics (color, size, text size etc) but for each copy I will want to add different text. How do I do this without having to recreate all the properties every time? Somehow set the defaults to be the ones I want? I would like to initialize it just once, and then reuse it ...
Informatix Expert Licensed User Longtime User Jan 14, 2013 #2 You cannot create a model and replicate it. But you can ease your work this way: Code: Sub Label_Initialize(MyLabel As Label, Text As String) MyLabel.Initialize("") MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT MyLabel.TextColor = Colors.Black MyLabel.TextSize = 18 MyLabel.Typeface = Typeface.DEFAULT MyLabel.Text = Text End Sub You call the function for each label. Example: Code: Dim Label1 As Label Label_Initialize(Label1, "text1") MyPanel.AddView(Label1, 0, 0, 100dip, 20dip) Dim Label2 As Label Label_Initialize(Label2, "text2") MyPanel.AddView(Label2, 0, 30dip, 100dip, 20dip) Upvote 0
You cannot create a model and replicate it. But you can ease your work this way: Code: Sub Label_Initialize(MyLabel As Label, Text As String) MyLabel.Initialize("") MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT MyLabel.TextColor = Colors.Black MyLabel.TextSize = 18 MyLabel.Typeface = Typeface.DEFAULT MyLabel.Text = Text End Sub You call the function for each label. Example: Code: Dim Label1 As Label Label_Initialize(Label1, "text1") MyPanel.AddView(Label1, 0, 0, 100dip, 20dip) Dim Label2 As Label Label_Initialize(Label2, "text2") MyPanel.AddView(Label2, 0, 30dip, 100dip, 20dip)
mc73 Well-Known Member Licensed User Longtime User Jan 14, 2013 #3 You can dim an array of labels and set their common properties using a loop. Upvote 0
M Mahares Expert Licensed User Longtime User Jan 14, 2013 #4 Here is my code which I think is in line with what mc73 is suggesting. Informatix's (Fred) recommendations are always top notch too. Suit yourself. Code: 'BELOW DISPLAYS A SERIES OF LABELS THAT SHARE COMMON PROPERTIES, BUT HAVE DIFFERENT TEXT Dim MyLabelText(5) As String Dim ArrayOfLabels(5) As Label Dim i As Int MyLabelText = Array As String ("Klaus","NJDude","Informatix","mc73","Erel") For Each MyLabel As Label In ArrayOfLabels MyLabel.Initialize("") MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT MyLabel.TextColor = Colors.Cyan MyLabel.TextSize = 18 MyLabel.Typeface = Typeface.DEFAULT MyLabel.Text=MyLabelText(i) Activity.AddView(MyLabel,0,i*45dip,50%x,50dip) i=i+1 Next Upvote 0
Here is my code which I think is in line with what mc73 is suggesting. Informatix's (Fred) recommendations are always top notch too. Suit yourself. Code: 'BELOW DISPLAYS A SERIES OF LABELS THAT SHARE COMMON PROPERTIES, BUT HAVE DIFFERENT TEXT Dim MyLabelText(5) As String Dim ArrayOfLabels(5) As Label Dim i As Int MyLabelText = Array As String ("Klaus","NJDude","Informatix","mc73","Erel") For Each MyLabel As Label In ArrayOfLabels MyLabel.Initialize("") MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT MyLabel.TextColor = Colors.Cyan MyLabel.TextSize = 18 MyLabel.Typeface = Typeface.DEFAULT MyLabel.Text=MyLabelText(i) Activity.AddView(MyLabel,0,i*45dip,50%x,50dip) i=i+1 Next
G GuyBooth Active Member Licensed User Longtime User Jan 14, 2013 #5 Thanks for all your very good suggestions...they all help with organizing (as would creating a class). I'm looking specifically for performance improvement (my apologies - I should have stated that up front). Since all of these methods still requires a complete loop through the settings for every label created, am I right in thinking I wouldn't gain performance-wise? Upvote 0
Thanks for all your very good suggestions...they all help with organizing (as would creating a class). I'm looking specifically for performance improvement (my apologies - I should have stated that up front). Since all of these methods still requires a complete loop through the settings for every label created, am I right in thinking I wouldn't gain performance-wise?
M Mahares Expert Licensed User Longtime User Jan 14, 2013 #6 You will not lose any performance by going through the loop. I just created 100 labels using the code I gave you in my previous post. It took 0.29 second to create them and display them all on the screen, which is insignificant. Upvote 0
You will not lose any performance by going through the loop. I just created 100 labels using the code I gave you in my previous post. It took 0.29 second to create them and display them all on the screen, which is insignificant.
Informatix Expert Licensed User Longtime User Jan 15, 2013 #7 As Mahares said, performance is not much affected by this way of doing things. When you create a view, most of the time is devoted to creating an instance of the view and adding it to a container, not filling its properties, so whatever solution you may choose, the performance gain will be marginal. I don't recommend using a loop with an array (except if you have a lot of views to create) because it's easier to maintain a program with explicit variable names than a code full of label(7), panel(5), imgvw(8), etc. Upvote 0
As Mahares said, performance is not much affected by this way of doing things. When you create a view, most of the time is devoted to creating an instance of the view and adding it to a container, not filling its properties, so whatever solution you may choose, the performance gain will be marginal. I don't recommend using a loop with an array (except if you have a lot of views to create) because it's easier to maintain a program with explicit variable names than a code full of label(7), panel(5), imgvw(8), etc.