CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Posts
    51

    Replace part of object name

    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.


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Replace part of object name

    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 Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Replace part of object name

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured