If/Then/Else Help

Ksmith1192

Member
Licensed User
Longtime User
Ok guys, so heres my scenario:

I have a PriceLabel, PriceIncreasebtn, PriceDecreasebtn to change the label of the PriceLabel +1. I'm new to programming, and I am trying to figure out how to use an If/Then statement for this.

So If I press the PriceIncreasebtn it goes up by 1 continuously until i stop clicking the button. And vice versa for the decrease. Is that possible? Any and all suggestions would be appreciated.
 

stu14t

Active Member
Licensed User
Longtime User
If I understand you correctly there is no need for if/then/else statements.

You just need to put your code in the button click events
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Ksmith,

I have added a quick example of what you want, actually putting the increase and decrease into the button click event for each button would be quicker (margret explained this) but if you create the buttons and then change event name of both buttons to the same text (in this example 'PriceChange') and then set the tag in both to two different strings (this can be numbers or text or a mixture), then the new event name is call each time the button is pressed and the event sub will look at the tag of the sending view (button in this example) and then carry out different actions depending on the tag.

B4X:
'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 PriceLabel As Label
   Dim PriceIncreasebtn As Button
   Dim PriceDecreasebtn 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("main")
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub PriceChange_Click
   Dim btn As Button, price As Int 
   btn = Sender
   price = PriceLabel.Text 
   If btn.Tag ="Increase" Then 
      price = price + 1
   Else
      price = price - 1
   End If
   PriceLabel.Text = price
End Sub

I hope it helps.

Stu
 

Attachments

  • IfThenExample.zip
    7 KB · Views: 169
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Don't forget The price is always positive or zero.
If Price < 0 then price=0
 
Upvote 0

Ksmith1192

Member
Licensed User
Longtime User
Help?



Stu, this is how I have mine coded. When I try and debug and test it. I click the + and - buttons, but they only go to +1 and -1. How do I set it to where it continually goes up as i hold in the + button (and same for - )

here is my code

B4X:
Sub Price_Click
   Dim btn As Button, Price As Int
   btn = Sender
   If btn.Tag ="Increase" Then
      Price = Price + 1
   Else
      Price = Price - 1
   End If
   PriceLabel.Text = Price
   If Price < 0 Then Price = 0
   If Price > 50 Then Price = 50
End Sub
Sub Pounds_Click
   Dim btn As Button, Pounds As Int
   btn = Sender
   If btn.Tag ="Increase" Then
      Pounds = Pounds + 1
   Else
      Pounds = Pounds - 1
   End If
   PoundsLabel.Text = Pounds   
   If Pounds < 0 Then Pounds = 0
   If Pounds > 50 Then Pounds = 50 
End Sub
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Ksmith,

I have changed my example to the code below (just cut and paste over the top of the original code)

B4X:
'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 PriceLabel As Label
   Dim PriceIncreasebtn As Button
   Dim PriceDecreasebtn As Button
   Dim ButtonDown As Boolean 
   Dim Timer1 As Timer
   Dim ButtonPressed As String 
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("main")
   Timer1.Initialize("Timer1",1000)
   Timer1.Enabled = True
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Timer1_Tick
   Dim price As Int 
   price = PriceLabel.Text 
   If ButtonDown Then 
      If ButtonPressed ="Increase" Then 
         price = price + 1
      Else
         price = price - 1
      End If
      PriceLabel.Text = price
   End If
End Sub

Sub PriceChange_Down
   Dim btn As Button
   btn = Sender
   ButtonDown = True
   ButtonPressed = btn.Tag 
End Sub
Sub PriceChange_Up
   ButtonDown = False
End Sub

This is a little drawn out (im not sure if there is a quicker way with a button for doing this), but basically when the button is down (pressed), it sets a Boolean variable (ButtonDown) to equal True and if its not pressed it equals false. I have then Used a timer (Timer1.Initialize("Timer1",1000)) to call the timer_tick sub every second to increase/decrease the price while the button is held. the 1000 is 1000ms = 1s you can change this number so the value increases/decreases faster if you want.

I hope this helps

Stu
 
Upvote 0

Ksmith1192

Member
Licensed User
Longtime User
Well I tried doing all of that, but everytime i hit either the price increase/decrease button, the data value label never changes. I have attached what I have done so far. (Sorry if it looks so sloppy)
I just want to be able to do this for both the price, and the pounds (you'll see in the file)
 

Attachments

  • Price increase.zip
    180.7 KB · Views: 171
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Your PriceIncrease and PriceDecrease buttons had different event names.

i have modified your code and attached it for you to have a look at

Regards

Stu
 

Attachments

  • Price Increase v1_0_1.zip
    180.7 KB · Views: 167
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find another version.
With one event name for all four Buttons.
B4X:
Sub Globals
    '
    Dim ButtonPressed As Button 
    Dim LabelToChange As Label
End Sub

Sub Timer1_Tick
    Dim val As Int 

    val = LabelToChange.Text

    If ButtonPressed.Text = "+" Then
        val = val + 1
    Else
        val = val - 1
    End If

    Select ButtonPressed.Tag
    Case "Price"
        val = Max(0, val)
    Case "Pounds"
        val = Max(0, val)
        val = Min(50, val)
    End Select

    LabelToChange.Text = val
End Sub

Sub Change_Down
    ButtonPressed = Sender
    Timer1.Enabled = True

    Select ButtonPressed.Tag
    Case "Price"
        LabelToChange = PriceLabel
    Case "Pounds"
        LabelToChange = PoundsLabel
    End Select

    Timer1_Tick
End Sub

Sub Change_Up
    Timer1.Enabled = False
End Sub
Best regards.
 

Attachments

  • Price increaseV2.zip
    180.7 KB · Views: 169
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Klaus,

That is really good I had not thought to dimension a button and label in globals, makes it a lot more elegant. Thanks for showing me a new way to handle this.

Stu

Sent from my Nexus 7 using Tapatalk HD
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…