Results 1 to 16 of 16

Thread: Find out which label caption I have the mouse over?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Find out which label caption I have the mouse over?

    Hi there folks! I have a program where I am teaching kids how to spell simple words, anyway. Basically, they drag letters via mouse move events to labels. What I would to know is how to figure out that if my mouse move event for any letter is started on a specific label caption, a message box will appear with the number it is.

    For example, say the word is 6 letters long (so 6 labels are visible), the kindergarten student drags each letter (the letters are image boxes) to its spot on the label caption. What I would like to do is if the student drags a letter to box number 1 (going from left to right), a msgbox will pop up with a "1".

    If anyone knows how to do that, it would be very much appreciated!

    Thanks!!

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Find out which label caption I have the mouse over?

    Use a control array for the labels. That will give you will one MouseMove event for all of them and the Index parameter of the event will tell you which label the mouse is over.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Find out which label caption I have the mouse over?

    Ok, thank you for your response. Maybe something like this?
    VB Code:
    1. Private Sub LetterSpotLBL_MouseMove(Index As Integer)
    2. With LetterSpotLBL(Index)
    3.  
    4. Select Case Index
    5.  
    6. Case 0
    7. SpotLBL.Caption = "1"
    8.  
    9. Case 1
    10. SpotLBL.Caption = "2"
    11.  
    12. Case 2
    13. SpotLBL.Caption = "3"
    14.  
    15. Case 3
    16. SpotLBL.Caption = "4"
    17.  
    18. Case 4
    19. SpotLBL.Caption = "5"
    20.  
    21. Case 5
    22. SpotLBL.Caption = "6"
    23.  
    24. Case 6
    25. SpotLBL.Caption = "7"
    26.  
    27. End Select
    28. End With
    29. End Sub

  4. #4
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Find out which label caption I have the mouse over?

    Justin

    Did that work?
    If not, I have a less elegant approach in mind.

    Spoo
    Last edited by Spooman; Apr 2nd, 2017 at 03:15 PM.

  5. #5
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Find out which label caption I have the mouse over?

    Quote Originally Posted by Justin M View Post
    Ok, thank you for your response. Maybe something like this?
    VB Code:
    1. Private Sub LetterSpotLBL_MouseMove(Index As Integer)
    2. With LetterSpotLBL(Index)
    3.  
    4. Select Case Index
    5.  
    6. Case 0
    7. SpotLBL.Caption = "1"
    8.  
    9. Case 1
    10. SpotLBL.Caption = "2"
    11.  
    12. Case 2
    13. SpotLBL.Caption = "3"
    14.  
    15. Case 3
    16. SpotLBL.Caption = "4"
    17.  
    18. Case 4
    19. SpotLBL.Caption = "5"
    20.  
    21. Case 5
    22. SpotLBL.Caption = "6"
    23.  
    24. Case 6
    25. SpotLBL.Caption = "7"
    26.  
    27. End Select
    28. End With
    29. End Sub

    Well, something a little less verbose (but equivalent) will suffice

    Code:
    Private Sub LetterSpotLBL_MouseMove(Index As Integer)
      SpotLBL.Caption = Index
    End Sub
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Find out which label caption I have the mouse over?

    I wasn't sure what you were after exactly. This might be overkill, and even then it might be refined a bit.

    Name:  sshot.png
Views: 229
Size:  3.9 KB

    It is a lot more verbose than the other suggestions though.
    Attached Files Attached Files

  7. #7
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Find out which label caption I have the mouse over?

    I think you might want to consider DragOver instead of MouseMove

    Code:
    Dim nX As Single
    Dim nY As Single
    
    Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     nX = X
     nY = Y
     Image1.Drag vbBeginDrag
    End Sub
    
    Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       With Image1
       
         .Move .Left + ScaleX(X - nX, vbTwips, Me.ScaleMode), .Top + ScaleY(Y - nY, vbTwips, Me.ScaleMode)
       End With
     End If
    End Sub
    
    Private Sub LetterSpotLBL_DragOver(Index As Integer, Source As Control, X As Single, Y As Single, State As Integer)
     Caption = LetterSpotLBL(" & Index & ")"
    End Sub
    Last edited by Ordinary Guy; Apr 2nd, 2017 at 06:12 PM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Find out which label caption I have the mouse over?

    Quote Originally Posted by ColinE66 View Post
    Well, something a little less verbose (but equivalent) will suffice

    Code:
    Private Sub LetterSpotLBL_MouseMove(Index As Integer)
      SpotLBL.Caption = Index
    End Sub
    These are all great ideas, and I appreciate them.

    I tried yours first, but I get this odd error it says

    Compile Error

    Procedural Declaration does not match description of event or procedure having the same name.

    Then it highlights the line...

    Private Sub LetterSpotLBL_MouseMove(Index As Integer)

    But I don't get it, the label array is called LetterSpotLBL, and when I double click on it, the code takes me to that sub..

  9. #9
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Find out which label caption I have the mouse over?

    That's because MouseMove is LetterSpotLBL_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Last edited by Ordinary Guy; Apr 2nd, 2017 at 06:06 PM.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Find out which label caption I have the mouse over?

    I'm a little confused. Do you want to drag a letter (image control) over a label and then have a message box say which label you are over or do you want to drag a letter over a label and have a messagebox say if the letter matches the letter on the label

    Either way I don't suggest using a messagebox

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Find out which label caption I have the mouse over?

    Quote Originally Posted by Ordinary Guy View Post
    I'm a little confused. Do you want to drag a letter (image control) over a label and then have a message box say which label you are over or do you want to drag a letter over a label and have a messagebox say if the letter matches the letter on the label

    Either way I don't suggest using a messagebox
    Sorry for the confusion! Yes I would like to drag a letter (which is an image control) over a label, and then the message box would pop up saying which label it is over. : )

  12. #12
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Find out which label caption I have the mouse over?

    Then don't use MouseMove event. See my post 7. MouseMove is for the object you are moving (images, not labels) and DragOver is for the object you drag the letters over (labels, not images). Also, since you have many letters I suggest you use an array of images representing each letter and if you do and if you use the code in post 7 make sure you indicate which index you are dragging
    Last edited by Ordinary Guy; Apr 2nd, 2017 at 06:39 PM.

  13. #13
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Find out which label caption I have the mouse over?

    Quote Originally Posted by Ordinary Guy View Post
    That's because MouseMove is LetterSpotLBL_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Yes, sorry about that. I just copied OP's code (without paying any attention to the event's signature, it would seem) and made it less verbose...
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  14. #14
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Find out which label caption I have the mouse over?

    Here is a version of my previous example with some tweaks.

    The "letter" Image controls have lowercase letters and a 1-pixel width box drawn around them. The DragXXX events are used here.

    This way you have very limited control over things during drag operations, so I create a cursor from the dragged item's Picture to give some visual feedback. Since cursors and icons have OS-determined sizes there is a little distortion but it still works well enough... at least at 96 DPI.


    Six of one, half a dozen of the other. I still like the first version despite the extra code since you aren't bound to the icon/cursor size for showing feedback to the user.


    Oh: I used a Debug.Print instead of your MsgBox because it doesn't cancel the drag operation.
    Attached Files Attached Files

  15. #15
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Find out which label caption I have the mouse over?

    While the GDI/OLE code to make an opaque icon/cursor from a StdPicture is easy enough, there is a bit more work involved if you want transparency.

    But you can make use of an ImageList control to manufacture either type, and it is pretty easy. Here's another silly demo with transparency:


    Name:  sshot.png
Views: 188
Size:  18.2 KB

    Dragging a second "A" block to drop onto
    the picture of marbles

    Most of the attachment size comes from the marbles picture.


    The control instances were pre-created at design time with various colors. They could also use differing font faces and sizes if desired.

    Code:
    Option Explicit
    
    'All containers have ScaleMode = vbTwips.
    
    Private Sub Form_Load()
        Dim SourceWidth As Single
        Dim SourceHeight As Single
        Dim OnePxX As Single
        Dim OnePxY As Single
        Dim I As Integer
        Dim Char As String
    
        With ilSource
            .ImageWidth = 32
            .ImageHeight = 32
            SourceWidth = ScaleX(.ImageWidth, vbPixels, vbTwips)
            SourceHeight = ScaleY(.ImageWidth, vbPixels, vbTwips)
            .UseMaskColor = True 'Assign False instead for "solid" Dragicons.
        End With
        OnePxX = ScaleX(1, vbPixels, ScaleMode)
        OnePxY = ScaleY(1, vbPixels, ScaleMode)
        With picSource
            For I = .LBound To .UBound
                With .Item(I)
                    .Width = SourceWidth
                    .Height = SourceHeight
                    .DrawWidth = 2
                    picSource(I).Line (OnePxX, OnePxY)- _
                                      (SourceWidth - OnePxX, SourceHeight - OnePxY), _
                                      , _
                                      B
                    .ForeColor = .FillColor 'We are using FillColor to hold the text color
                                            'we assigned at design time.
                    Char = ChrW$(AscW("A") + I) 'Assumes LBound = 0.
                    .CurrentX = (SourceWidth - .TextWidth(Char)) / 2
                    .CurrentY = (SourceHeight - .TextHeight(Char)) / 2
                    picSource(I).Print Char;
                    
                    ilSource.MaskColor = .BackColor
                    ilSource.ListImages.Add 1, , .Image
                    .DragIcon = ilSource.ListImages(1).ExtractIcon
                    ilSource.ListImages.Clear 'We no longer need it.
                End With
            Next
        End With
    End Sub
    
    Private Sub picDest_DragDrop(Source As Control, X As Single, Y As Single)
        With Source
            picDest.PaintPicture .Image, _
                                 X - .Width / 2, _
                                 Y - .Height / 2
        End With
    End Sub
    Attached Files Attached Files

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Find out which label caption I have the mouse over?

    Oh you are marvelous! The kids are going to love this! I hate to both you further, but could you add a command button so when pressed, it would pick another word for the kids to spell. What I did is add a listbox with a bunch of words I would like them to be able to spell, and the command button would randomly pick an item from the list to use that to spell.

    Again thank you so much!!

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