1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

iOS Question Change color a button

Discussion in 'iOS Questions' started by Hector Alcala, Apr 7, 2019.

  1. Hector Alcala

    Hector Alcala Member Licensed User

    Hi, I would like to optimize my routine that I use to change the color of a particular button.
    This is the routine I use (ChangeColor)

    Sub Process_Globals
    Private btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12 As Button
    Private Colorx As String = Colors.RGB(91,208,18)
    End Sub

    Sub Application_Start (Nav As NavigationController)
    :
    :
    ChangeColor(4)
    End Sub

    Sub ChangeColor(btnx As Int)
    Select btnx
    Case 1
    btn1.Color = Colorx
    Case 2
    btn2.Color = Colorx
    Case 3
    btn3.Color = Colorx
    Case 4
    btn4.Color = Colorx
    Case 5
    btn5.Color = Colorx
    Case 6
    btn6.Color = Colorx
    Case 7
    btn7.Color = Colorx
    Case 8
    btn8.Color = Colorx
    Case 9
    btn9.Color = Colorx
    Case 10
    btn10.Color = Colorx
    Case 11
    btn11.Color = Colorx
    Case 12
    btn12.Color = Colorx
    End Select
    End Sub

    Any suggestions ?
     
  2. Sviluppatori Liceo Giovio

    Sviluppatori Liceo Giovio Member Licensed User

    Why not just do btn4.color = colorx? No need to use a sub
     
  3. Hector Alcala

    Hector Alcala Member Licensed User

    Hi, Excuse me for not explaining myself well. I need to change the color of a button between 100. The example was not very clear.

    ChangeColor(I) where I is a random number from 1 to 100. I currently use a routine using the select statement. In the example I only placed 12 cases to simplify.
     
  4. Sviluppatori Liceo Giovio

    Sviluppatori Liceo Giovio Member Licensed User

    I see. The easiest way is to use an array instead of individually creating each button.

    Dim buttons(100) As Button

    buttons(I).Color = Colorx
     
  5. Hector Alcala

    Hector Alcala Member Licensed User

    My problem is that the buttons must be in very specific places, for which I do not have a mathematical formula to be able to place them.
     
  6. Sviluppatori Liceo Giovio

    Sviluppatori Liceo Giovio Member Licensed User

    What do you mean? Any position you set with the designer can be set by code just as easily.
     
  7. Hector Alcala

    Hector Alcala Member Licensed User

    The routine to place each button in its place would be very large and what I'm looking for is to simplify programming
     
  8. Sviluppatori Liceo Giovio

    Sviluppatori Liceo Giovio Member Licensed User

    I'm going to need more info on what you're trying to achieve in order to help you.
     
  9. Hector Alcala

    Hector Alcala Member Licensed User

    For the location of the buttons you suggest something like this:

    Dim buttons(100) As Button
    For I = 1 To 100
    Select I
    Case 1
    buttons(I).Top = 1
    buttons(I).Left = 1
    Case 2
    buttons(I).Top = 12
    buttons(I).Left = 3
    Case 3
    buttons(I).Top = 5
    buttons(I).Left = 20
    :
    :
    :
    End Select
    Next
     
  10. Sviluppatori Liceo Giovio

    Sviluppatori Liceo Giovio Member Licensed User

    so you're saying that there is no formula at all to the positions? Alright.
    Well you can make this much shorter. Since you're defining each button via code, you'll need to initialize them and then add them to the panel.
    So say you're adding it to the page's root panel. You'll need to do
    For I = 1 to 100
    buttons(I).Initialize("...",buttons(I).Style...)

    Page1.Rootpanel.Addview(buttons(1),1,1,width,height)
    Page1.Rootpanel.Addview(buttons(2),12,3,width,height)

    I still don't understand why you need so many buttons.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice