Click to See Complete Forum and Search --> : Replace part of object name


mevasquez
August 10th, 2001, 12:20 PM
I have many buttons on a form (8 across and 8 down). The buttons that are aligned vertically are arranged in a control array and I have named them "cmdOut1(0)...cmdOut1(7)". The next column is "cmdOut2(0)...cmdOut2(8)" ... . I want to be able to use the same sub by replacing the number in cmdOut1(0) with the value of n. Example of the this is:

private Sub ChangeColor(a as Integer, n as integer)
If cmdout1(a).BackColor = &HFF& then 'replace 1 with n
cmdout1(a).BackColor = &HC0C000 'replace 1 with n
else
cmdout1(a).BackColor = &HFF& 'replace 1 with n
End If
end sub



I want to be able to replace the 1 with n above. This way I don't have to create 8 subs that do the same thing.

The button that is clicked

private Sub cmdout1_Click(Index as Integer)
ChangeColor (Index, 1)
End Sub

'The next column would be

private Sub cmdout2_Click(Index as Integer)
ChangeColor (Index, 2)
End Sub




Is this possible?
Any help would be appreciated.

Johnny101
August 10th, 2001, 01:16 PM
You'll have to go through the Me.Controls collection and inspect each one - if it matches your desired name, then perform the action, otherwise, move on. To add extra security to this, test for the type of control. Here's an example that changes the font.bold = true when you click a command button.

private Sub Command1_Click(Index as Integer)
ChangeColor Index, 1
End Sub

private Sub Command2_Click(Index as Integer)
ChangeColor Index, 2
End Sub

private Sub ChangeColor(a as Integer, n as Integer)
Dim ctl as Control

for Each ctl In me.Controls
If LCase(Left(ctl.Name, 7)) = "command" then

If Right(ctl.Name, 1) = n then

If ctl.Index = a then

If ctl.Font.Bold = true then
ctl.Font.Bold = false
else
ctl.Font.Bold = true
End If
End If

End If

End If

next

End Sub




Hope this helps,

john

John Pirkey
MCSD
http://www.ShallowWaterSystems.com
http://www.stlvbug.org

John G Duffy
August 10th, 2001, 02:55 PM
here is a simple program that has three commandbutton columns. Each columns click event passes the name of the button just clicked to the common rutine that can process it just as if there is a single button
[vbcode]
'
option Explicit

'
public Sub CommonProc(ctl as CommandButton)
MsgBox ctl.Caption & " " & ctl.Index
End Sub
'
private Sub Command1_Click(Index as Integer)
CommonProc Command1(Index)
End Sub
'
private Sub Command2_Click(Index as Integer)
CommonProc Command2(Index)

End Sub
'
private Sub Command3_Click(Index as Integer)
CommonProc Command3(Index)

End Sub




John G