Results 1 to 6 of 6

Thread: [RESOLVED] Get "column" from selected control array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Resolved [RESOLVED] Get "column" from selected control array

    Hi,

    On my form I have 40 labels in a control array. They are lined up with 10 columns and 4 rows.

    lbl(0) lbl(1) lbl(2) lbl(3)
    lbl(10) lbl(11)
    lbl(20) lbl(21)
    lbl(30)
    etc.

    How would I go about returning the column of the label I clicked on?
    Say I was to click lbl(10) it would return 0. If I would click lbl(21) it would return 1.

    Thanks.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Get "column" from selected control array

    You use Mod and \ operators.

    Index = X + Y * ItemsInRow

    X = Index Mod ItemsInRow
    Y = Index \ ItemsInRow

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Get "column" from selected control array

    This somewhat traditional code does not use Mod arithmatic but it provides both the column number and the row number:
    Code:
    Option Explicit
    Const NumRows = 4, NumCols = 10
    
    Private Sub Label1_Click(Index As Integer)
    Dim RowNum As Integer, ColNum As Integer, Pointer As Integer
    Dim I As Integer, J As Integer
    For I = 1 To NumRows
        For J = 1 To NumCols
            Pointer = Pointer + 1
            If Pointer > Index Then
                ColNum = J
                Exit For
            End If
        Next
        If ColNum Then
            RowNum = I
            Exit For
        End If
    Next
    MsgBox "I clicked on row" & Str$(RowNum) & ", column" & Str$(ColNum)
    End Sub
    Last edited by Code Doc; Dec 13th, 2007 at 02:13 PM. Reason: Swapped Column and Row Counts
    Doctor Ed

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Get "column" from selected control array

    That's not traditional, that's just one way to do it when you don't know about the existence of Mod and \ operators ^_^

    Here is another way to do it without Mod and \:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim X As Long, Y As Long, Index As Long, Columns As Long
        Columns = 10
        Index = 56
        Y = Fix(Index / Columns)
        X = ((Index / Columns) - Y) * Columns
        MsgBox X & " x " & Y
    End Sub
    However, Mod and \ remain superior in terms of performance and in length of code

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: [RESOLVED] Get "column" from selected control array

    Well, I hate to beat a horse to death, but I still cannot get a Mod operator involved with a solution. This code works without looping and may be easier for the OP to use than what I wrote above:
    Code:
    Option Explicit
    Const NumCols = 10, NumRows = 4
    
    Private Sub Label1_Click(Index As Integer)
    Dim ColNum As Integer, RowNum As Integer
    RowNum = Index \ NumCols + 1
    ColNum = Index - ((RowNum - 1) * NumCols) + 1
    MsgBox "I clicked on column" & Str$(ColNum) & ", row" & Str$(RowNum)
    End Sub
    Last edited by Code Doc; Dec 13th, 2007 at 03:16 PM. Reason: Punctuation
    Doctor Ed

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Get "column" from selected control array

    Apparently you're trying to work with one based numbers and arrays: zero base is much more convenient in these cases. Internally you probably want to keep everything in zero base.

    Mod and \ with one based array, returning one based row and column:

    Code:
    Const Index = 12
    Const Cols = 10
    
    X = ((Index - 1) Mod Cols) + 1
    Y = ((Index - 1) \ Cols) + 1
    As you can see, it eventually goes back to zero base, then jumps back to one base.
    Last edited by Merri; Dec 13th, 2007 at 03:46 PM.

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