help with a problem with a code

Piero753

Member
Hi all well, I'm new to this and I'm doing a basic program, when I press negative subtract -1 and when i press positive adds +1 just want to know how I can put a notice te when the result is 4 saying "lost" in the label 2 I try to add a timer but nothing does not work, I show a photo

sinttulofbr.jpg



I hope your help thanks :)
 

stevel05

Expert
Licensed User
Longtime User
Can you zip (from the files menu) and post your code (see manage attachments when posting) so we can see your whole project including what you've done in the designer.
 
Upvote 0

Piero753

Member
okey i zip the proyect , another question can you give me your email or something to communicate faster? thanks for your help :)
 

Attachments

  • project.zip
    7.1 KB · Views: 226
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It's better to keep the conversations in the forum so others can see the results, and join in with alternate or more timely solutions.

You don't need a timer to do this, you can call a checking routine after each button click.

B4X:
Sub Button2_Click
   label1.Text = label1.Text + 1 
   CheckScore
End Sub
Sub Button1_Click
    label1.Text = label1.Text - 1
   CheckScore
End Sub
Sub CheckScore
   If label1.Text = "-4.0" Then
        Label2.Text = "lost" 
     End If
End Sub

Also Label1.Text holds a string, so you should compare it with a string "-4.0". It would have worked with a timer too if you had just changed this.
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Piero753 .. the above code that stevel05 supplied should work. Each button click calls 'CheckScore' sub/routine.

Cheers mj
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As in my example, the 'CheckScore' on the line after

B4X:
label1.Text = label1.Text - 1
CheckScore  'here

calls the Sub CheckScore.

Run it and you will see.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Did you create the Sub?

Your entire code should look like this (copy and paste it):

B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.



   Dim Label1 As Label
   Dim Label2 As Label
'   Dim Timer1 As Timer
   Dim Button1 As Button
   Dim Button2 As Button
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")
  Activity.LoadLayout("dada")
'Timer1.Initialize("Timer1",1000)
'Timer1.Enabled = True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Button2_Click
   Label1.Text = Label1.Text + 1 
   CheckScore
End Sub
Sub Button1_Click
    Label1.Text = Label1.Text - 1
   CheckScore
End Sub
Sub CheckScore
   Log(Label1.Text)
   If Label1.Text = "-4.0" Then
        Label2.Text = "lost" 
     End If
End Sub
 
Upvote 0

Piero753

Member
Hello again stevel05 , I just wanted to help me make a function, for example in the image where it says 312 , I want every time you push negative or positive 312 down to 311 so successively , when in 312 I want divides 6 the result to the label 1 and when in 260 divide 5 and when in 208 divide 4 ,for example i push positive two times the number 312 decreases to 310 if i push 52 the number 312 decreases to 260 ,when the number is in 260 divide 5 to label 1
helpm.jpg
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can do this directly in the button click subs, something like:

B4X:
If Label1.Text = "312" Then Label1.Text = 312/6

Although if you put it in both positive and negative button clicks, you will not get to a number larger than 312.

So play with it and you'll find exactly how to do what you want, if you get stuck post your code with the question and we will help.
 
Upvote 0

Piero753

Member
I do not think you understood me very well or I did not express myself well, because I'm using a translator, I speak Spanish, I say that every time you click on positive or negative, 312 diminish 1 this way successively up to coming to 260 and this when in 260 the result of the label1 divides 5 , for example in the label 1 the number is 10 and if the label 2 indicates 260 divide automatic 5
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ok No problem, I'm sure your English is infinitely better than my Spanish I'm ashamed to say.

So you want to set Label1.text and Label2.text before you start, then have something like:

B4X:
 Button2_Click
    Label2.Text = Label2.Text - 1 
    CheckScore
End Sub
Sub Button1_Click
    Label2.Text = Label2.Text - 1
    CheckScore
End Sub
Sub CheckScore
     If Label2.Text = 312 Then Label1.Text = Label1.Text / 6
     If Label2.Text = 260 Then Label1.Text = Label1.Text / 5
     If Label2.Text = 208 Then Label1.Text = Label1.Text / 4
End Sub

Is that more like what you meant?
 
Upvote 0

Piero753

Member
Clear if it works but only it divides a number, I want that I divide for example from 312 to 260 all the numbers of the result divide between 6
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I'm sorry I'm not sure exactly what you are asking, have a look at page 204 of the beginners guide.pdf 14.2.2 Relational expressions. I think you can download that. It'll help you understand what you need to do. If you get stuck just ask a question with some code examples.
 
Upvote 0
Top