VB6 - How to create a Label Array at runtime and trigger its events?

Hi Experts,

I have the following code that is trying to fill the values of the recordset into labels that should appear under eachother neatly. And then I want the label to be underlined (like hyperlinks) with a pointer cursor when a mouse comes over it. And upon clicking on it, I want it to perform an action.

However, I failed to Dim WithEvents for unknown reason!

Please correct my code, or recommend otherwise!
....
    If Not (RS.BOF And RS.EOF) Then
       RS.MoveFirst
       
       Dim LBL() As Label
       
       For i = 1 To RS.RecordCount
           ReDim Preserve LBL(i)
           With LBL1(i)
                  .Caption = RS("FormCaption")
                  .Left = 0
                  .Top = LBL(i - 1).Top + LBL(i - 1).Height
                  .Visible = True
           End With
           RS.MoveNext
       Next
    End If
....
 
Private Sub LBL_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
        Me.LBL(Index).FontUnderline = True
        Screen.MouseIcon = vbArrow
End Sub

Open in new window

feesuAsked:
Who is Participating?
 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
There are some incorrections in your code...

In VB6, you cannot create controls "from nothing" (as you can do it in .NET, from example). What you have to do is do create first element in design mode, so create element 0 of you label array in design mode (by setting its Index property to 0 value). To create new elements of the array at run time, don't use ReDim Preserve; you must use Load. For example:

If Not (RS.BOF And RS.EOF) Then
       RS.MoveFirst
       
       For i = 1 To RS.RecordCount
           Load LBL(i) 'Load a new element of you array. Element 0 has been created at design time
           With LBL1(i)
                  .Caption = RS("FormCaption")
                  .Left = 0
                  .Top = LBL(i - 1).Top + LBL(i - 1).Height
                  .Visible = True
           End With
           RS.MoveNext
       Next
End If

The event(s) will work perfectly, as you can define it at design time for element 0 (the one created at design time, and it will work too for all elements created at run-time).

Hope that helps.
0
 
feesuAuthor Commented:
Hello,

I get the following error on this line:

Subscript out of range >>>  Load LBL(i) 'Load a new element of you array. Element 0 has been created at design time
0
 
feesuAuthor Commented:
Roland,

I'm sorry, it was my mistake. I had defined the label twice. It works fine now.

Left is how to make the label caption underlined on mouse over with a pointer cursor and with no decoration on mouse out. I can't seem to have my code working for that!
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
Mmmmm.... for do that, the only thing you can do is something like this:

On LBL_MouseMove event, set up the "wait" cursor and underline decoration for your label (and do it only once for each time that user moves cursor over 1 label):

Private Sub LBL_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If LBL(Index).MousePointer <> vbCustom Then
        LBL(Index).MousePointer = vbCustom
        LBL(Index).MouseIcon = LoadPicture("c:\windows\cursors\harrow.cur") 'Or the icon or cursor that you want
        LBL(Index).FontUnderline = True
    End If
End Sub

On the Form_MouseMove event, check if one of the labels is text-underlined and, if found, undo the changes:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim k As Integer
   
    For k = LBL.LBound To LBL.UBound
        If LBL(k).MousePointer = vbCustom Then
            LBL(k).MousePointer = vbDefault
            LBL(k).FontUnderline = False
        End If
    Next k
End Sub

Hope that helps.
0
 
feesuAuthor Commented:
Hi Roland,

This mouseMove code, unfortunately didn't work! It doesn't change neither the font nor the cursor.
0
 
Luis PérezSoftware Architect in .NetCommented:
Check the code again. It must work. It the label array name is "LBL", the it must work.
0
 
feesuAuthor Commented:
The label name is now lbl_FormCaption, and I have changed it in my code but it didn't work. See below plz.


Private Sub lbl_FormCaption_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If lbl_FormCaption(Index).MousePointer <> vbCustom Then
        lbl_FormCaption(Index).MousePointer = vbCustom
        lbl_FormCaption(Index).MouseIcon = LoadPicture("c:\windows\cursors\harrow.cur") 'Or the icon or cursor that you want
        lbl_FormCaption(Index).FontUnderline = True
    End If
    
    Dim k As Integer
   
    For k = lbl_FormCaption.LBound To lbl_FormCaption.UBound
        If lbl_FormCaption(k).MousePointer = vbCustom Then
            lbl_FormCaption(k).MousePointer = vbDefault
            lbl_FormCaption(k).FontUnderline = False
        End If
    Next k
End Sub

Open in new window

0
 
Luis PérezSoftware Architect in .NetCommented:
The second part of the routine (starting with the line "Dim k as integer") must not be in lbl_FormCaption_MouseMove, but in Form_MouseMove, as I mentioned in the original message. Try that.
0
 
feesuAuthor Commented:
Hi Roland,

It works fine now. Thanks alot.
0
 
feesuAuthor Commented:
Roland!

Why is it that I'm not able to change the cursor image to another one in the same C:\Windows\Cursors folder??

I have tried aero_link.cur and many others, but it always gived me "invalid picture" !!
0
 
Luis PérezSoftware Architect in .NetCommented:
None of the aero* will work. You'll probably using Windows Vista (as Aero is the codebase name of the graphical subsystem of Vista), and Vista cursors are in more advanced format than VB6 (and ancient program) can support. Only old cursor formats will work in VB6. It's a casuality than harrow.cur has keeped the same format in all versions of Windows. If you wish to use other cursor, try searching the Internet.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.