Reference view in code

jeffnooch

Member
Licensed User
Longtime User
i have code similar to the following...i want to set it up to reference via sender rather than having the code repeated multiple times (NOTE: there would be more than just the 2 sections below)...but what i'm not sure of how to do would be to reference the view via the code similar to what i did in VB...eg frmMain.controls("lblBalP" & lbl.tag).text

any thoughts would be great

B4X:
Sub lblBalP1_Click
    If lblCurScore.Text = "" Then Exit Sub
   lblBalP1.Text = Round(lblBalP1.Text - lblCurScore.Text)
   lblScoresP1.Text = lblCurScore.Text & CRLF & lblScoresP1.Text
   lblCurScore.Text = ""
   btn00.Visible = False
End Sub
Sub lblBalP2_Click
   If lblCurScore.Text = "" Then Exit Sub
   lblBalP2.Text = Round(lblBalP2.Text - lblCurScore.Text)
   lblScoresP2.Text = lblCurScore.Text & CRLF & lblScoresP2.Text
   lblCurScore.Text = ""
   btn00.Visible = False
End Sub
 

Informatix

Expert
Licensed User
Longtime User
i have code similar to the following...i want to set it up to reference via sender rather than having the code repeated multiple times (NOTE: there would be more than just the 2 sections below)...but what i'm not sure of how to do would be to reference the view via the code similar to what i did in VB...eg frmMain.controls("lblBalP" & lbl.tag).text

any thoughts would be great

B4X:
Sub lblBalP1_Click
    If lblCurScore.Text = "" Then Exit Sub
   lblBalP1.Text = Round(lblBalP1.Text - lblCurScore.Text)
   lblScoresP1.Text = lblCurScore.Text & CRLF & lblScoresP1.Text
   lblCurScore.Text = ""
   btn00.Visible = False
End Sub
Sub lblBalP2_Click
   If lblCurScore.Text = "" Then Exit Sub
   lblBalP2.Text = Round(lblBalP2.Text - lblCurScore.Text)
   lblScoresP2.Text = lblCurScore.Text & CRLF & lblScoresP2.Text
   lblCurScore.Text = ""
   btn00.Visible = False
End Sub

Not tested but that should work:

B4X:
Sub lblBalP_Click
   Dim lbl as label
   lbl = Sender 'You get the object that triggered the event
   If lblCurScore.Text = "" Then Exit Sub
   lbl.Text = Round(lbl.Text - lblCurScore.Text)
   if lbl = lblBalP2 then
      lblScoresP2.Text = lblCurScore.Text & CRLF & lblScoresP2.Text
   else
      lblScoresP1.Text = lblCurScore.Text & CRLF & lblScoresP1.Text
   end if
   lblCurScore.Text = ""
   btn00.Visible = False
End Sub
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
informatix...thanks for the response...

that's essentially what i did to combine everything but this is the part that i'm trying to find better way to handle...

B4X:
if lbl = lblBalP2 then
        lblScoresP2.Text = lblCurScore.Text & CRLF & lblScoresP2.Text
    else
        lblScoresP1.Text = lblCurScore.Text & CRLF & lblScoresP1.Text
    end if

because there are going to be more than 2 ...

in vb you could reference a control like
frmMain.controls("lblScoresP" & lbl.tag).text
can you do anything similar in b4a?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
in vb you could reference a control like
frmMain.controls("lblScoresP" & lbl.tag).text
can you do anything similar in b4a?
Two options:
You put the lblScoresP to change in the Tag property of your lblBal (lblBal1.Tag = lblScoresP1). Tag is an object, so it stores a pointer to the view.
You can declare all your labels as arrays of labels, e.g. Dim lblScoresP(10) as Label.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can do it like this:
B4X:
Dim lblScoresP() As Label
Dim lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4 As Label
lblScoresP = Array As Label(lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4)
For i = 0 To 4
  lblScoresP(i).Text = "Test" & i
Next
Best regards.
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
thanks for the responses...
when i run i get a "Object should be initialized (label)" error...how do you handle that?

thanks
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
thanks for the responses...
when i run i get a "Object should be initialized (label)" error...how do you handle that?

thanks
You have to initialize every label, using
B4X:
somelabel.initialize("nameOfEventHandler")
If you need not an event handler, you can use a blank string.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
At the sub you are creating them. For e.g. when you need to create a label with some text, firstly you dim it, then you initialize it, then you add the text. Keep in mind however, that if you place the label using the designer, you should not initialize it.
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
You can do it like this:
B4X:
Dim lblScoresP() As Label
Dim lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4 As Label
lblScoresP = Array As Label(lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4)
For i = 0 To 4
  lblScoresP(i).Text = "Test" & i
Next
Best regards.

klaus,
tried this and isn't working for me...

here is code that i have...in the message box the correct data shows up but it doesn't change any of my labels....any thoughts?
thanks

B4X:
   Dim lblScoresP() As Label
   Dim lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4 As Label
   lblScoresP0.Initialize("lblScoresP0"):lblScoresP1.Initialize("lblScoresP1"):lblScoresP2.Initialize("lblScoresP2"):lblScoresP3.Initialize("lblScoresP3"):lblScoresP4.Initialize("lblScoresP4")
   lblScoresP = Array As Label(lblScoresP0,lblScoresP1,lblScoresP2,lblScoresP3,lblScoresP4)
.....
      lblScoresP(i).Text = vScore
      Msgbox(vScore,i & " scores")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Jeff: Perhaps you forgot to add the labels to the activity. That is why you do not see them. Here is possibly a code that can help you. If it does not, then your best bet is to post your project. In the IDE, click on 'File' , then 'Export as zip'.. Next, attach the file to your post. But check out the below code first:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
  Dim lblScoresP() As Label
  Dim lblScoresP0, lblScoresP1, lblScoresP2, lblScoresP3, lblScoresP4 As Label
  Dim vscore As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
  lblScoresP = Array As Label(lblScoresP0,lblScoresP1,lblScoresP2,lblScoresP3,lblScoresP4)
  vscore=10
  For i=0 To 4
      lblScoresP(i).initialize("lblScoresP" & i)
      lblScoresP(i).Text=vscore*i
      lblScoresP(i).Color=Colors.Red
      lblScoresP(i).TextColor=Colors.Yellow
      Activity.AddView(lblScoresP(i),0,i*35dip+5dip,30%x,30dip)
  Next
End Sub
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
Mahares,
i did have the labels added via the designer...all except the P0 (because i'm not really using them)...so i did add them from the designer to see if it made a difference...but unfortunately it didn't

i'm new to B4A so i'm just learning...i'm trying to replicate something i did in vb6 (and .net) about 8 years ago...so i'm way rusty...plus trying to learn to get it working for my nexus7...

i got the basic flow working but with very cludgy (ie repeating) code...i'll try to clean it up with your suggestions...i think i remember reading somewhere that you don't have to initialize if you added via designer...so i think i'm getting myself a little confused...thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately it's difficult to help without knowing what you are doing and where.
If you add views in a layout file you must not initialize them.
If you add views in code you must initialize them before adding them to the activity or a panel.
My proposal was supposing that you added the labels with the Designer in a layout file.

The best way to help you were if you posted your project as a zip file (IDE menu Files / Export As Zip) so we could see what you have done and how and test in the same conditions as you do.

Best regards.
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
thanks klaus,
i will play around with it a little tonight...if i can't figure it out i'll post my zip...thanks for your help...
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
Mahares...your example helped me figure things out...now i was able to get rid of a lot of my duplicate code and i think i understand at least at a basic level...

this is a great forum...thanks for everyone's help...

i have new questions on the same project but a completely different topic...should i post them in this thread or start a new thread since a new topic?
Thanks
 
Upvote 0
Top