Can I use a LOOP?

Sanchez

OSNN Junior Addict
#1
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.
 

Mainframeguy

Debiant by way of Ubuntu
#2
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?
 

XPos

OSNN Addict
#3
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)
 

Mainframeguy

Debiant by way of Ubuntu
#4
I believe the lost post suggested using a CASE statement maybe? Seemed a good idea to me and maybe Sanchez has the email anyway
 

XPos

OSNN Addict
#5
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.
 

dave holbon

OSNN Veteran Addict
#6
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.


:) :)
 

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,970
Messages
673,297
Members
89,017
Latest member
Poseeut