Can I use a LOOP?

Discussion in 'Web Design & Coding' started by Sanchez, Jun 15, 2004.

  1. Sanchez

    Sanchez OSNN Junior Addict

    Messages:
    14
    In my program that's connected to a form I have code like:

    Code:
    'Row 1
    If TextExist("A-2-A1") Then
        Me.L1.BackColor = 16776960
        End If
    If TextExist("A-2-A2") Then
        Me.L2.BackColor = 16776960
        End If
    If TextExist("A-2-A3") Then
        Me.L3.BackColor = 16776960
        End If
    If TextExist("A-2-A4") Then
        Me.L4.BackColor = 16776960
        End If
    If TextExist("A-2-A5") Then
        Me.L5.BackColor = 16776960
        End If
    If TextExist("A-2-A6") Then
        Me.L6.BackColor = 16776960
        End If
    If TextExist("A-2-A7") Then
        Me.L7.BackColor = 16776960
        End If
    If TextExist("A-2-A8") Then
        Me.L8.BackColor = 16776960
        End If
    If TextExist("A-2-A9") Then
        Me.L9.BackColor = 16776960
        End If
    If TextExist("A-2-A10") Then
        Me.L10.BackColor = 16776960
        End If
    
    AND
    Code:
    Private Sub L1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Me.L1.BackColor = 16776960 Then
            Update ("A-1-A1")
            Me.L1.BackColor = 65535
        End If
    End Sub
    
    Private Sub L2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Me.L2.BackColor = 16776960 Then
            Update ("A-1-A2")
            Me.L2.BackColor = 65535
        End If
    End Sub
    
    Private Sub L3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Me.L3.BackColor = 16776960 Then
            Update ("A-1-A3")
            Me.L3.BackColor = 65535
        End If
    End Sub
    'etc.
    
    Is there anyway to right this using a loop. I don't know how to stick in number variables in the statements [TextExist("A-2-A1")] and [Me.L10,BackColor=1677960]. I think there must be a way tho. And is there any way of writing all those functions as one function with a variable in the name of the function? THANKS.
     
  2. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    Messages:
    3,763
    Location:
    London, UK
    Maybe java with a persistent data store would be one way to go - 'fraid I don;t know much about forms.... but at least I can bump for you and perhaps someone else does?
     
  3. XPos

    XPos OSNN Addict

    Messages:
    88
    Location:
    Kansas
    I need a little more information on this one. What are you trying to do with the TextExist part and the Update part? The L1, L2 objects you are referring to are what? Combo boxes or lines?

    Some had the answer to adding variables to a string but the posts were lost. I believe it goes something like this...
    Code:
    Update ("A-1-A" & Variable)
    
     
  4. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    Messages:
    3,763
    Location:
    London, UK
    I believe the lost post suggested using a CASE statement maybe? Seemed a good idea to me and maybe Sanchez has the email anyway
     
  5. XPos

    XPos OSNN Addict

    Messages:
    88
    Location:
    Kansas
    That was my original post. But if you look at it a little closer he's setting the background to the same color on all of what ever he's changing. It would be easier just to put it in an array loop.
     
  6. dave holbon

    dave holbon Moderator

    Messages:
    1,014
    Location:
    London England
    This can actually be achieved in one loop be designating your (I assume) controls on forms to variables and incrementing then in a for-next loop assigning the constant 16776906 and then the A1, A2 and L1, L2 to strings and manipulating the numerical part of the string (A[1],L[1]) which is the number part, to an integer and using the for next statement all you have to do is assign the For-Next integer to increment by +1 thus using only three lines of code. You will of course, if you are using Visual Basic have to employ the Eval() function to coerce the result back to a string for evaluation purposes (pun). Never use an array if it can be avoided.

    Using many If – Then – Else or ElseIf statements forces the compiler or pcode engine to test each instance one after the other and is thus cumbersome and slow, as a general rule I always start to re-think if I find myself writing more than one If-Then- End If statement to test for incremental changes either in strings or other data types.


    :) :)