Results 1 to 3 of 3

Thread: [RESOLVED] Help control array change borderstyle

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Resolved [RESOLVED] Help control array change borderstyle

    There are 5 images. When I click on one I want to change it's borderstyle. Only one image can have a borderstyle = 1 at any time. So how I change the previous image borderstyle back to zero.
    Code:
            If imgCar(Index).BorderStyle = 0 Then
                imgCar(Index).BorderStyle = 1
            Else
                imgCar(Index).BorderStyle = 0
            End If

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Help control array change borderstyle

    Loop throu control aray and reset border first, then set it to currently selected (clicked) control. Try something like this:
    Code:
    Option Explicit
    
    Private Sub Image1_Click(Index As Integer)
    Dim i As Integer
    
        For i = Image1.LBound To Image1.UBound
            Image1(i).BorderStyle = 0
        Next i
        Image1(Index).BorderStyle = 1
    
    End Sub
    Edit: add error handler to avoid errors for missing control array member (say, you have 0, 1, 3 but not 2).

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] Help control array change borderstyle

    Another approach would be is to keep track of the last Image control clicked via a Static (or module-level) variable:

    Code:
    Private Sub imgCar_Click(Index As Integer)
        Static OldIndex As Integer              '<-- Assumes there's an Image control with an Index of 0
    
        If Index <> OldIndex Then imgCar(OldIndex).BorderStyle = 0
        imgCar(Index).BorderStyle = 1
        OldIndex = Index
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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