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!!
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.
Re: Find out which label caption I have the mouse over?
Ok, thank you for your response. Maybe something like this?
VB Code:
Private Sub LetterSpotLBL_MouseMove(Index As Integer)
With LetterSpotLBL(Index)
Select Case Index
Case 0
SpotLBL.Caption = "1"
Case 1
SpotLBL.Caption = "2"
Case 2
SpotLBL.Caption = "3"
Case 3
SpotLBL.Caption = "4"
Case 4
SpotLBL.Caption = "5"
Case 5
SpotLBL.Caption = "6"
Case 6
SpotLBL.Caption = "7"
End Select
End With
End Sub
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
Re: Find out which label caption I have the mouse over?
Quote:
Originally Posted by
Justin M
Ok, thank you for your response. Maybe something like this?
VB Code:
Private Sub LetterSpotLBL_MouseMove(Index As Integer)
With LetterSpotLBL(Index)
Select Case Index
Case 0
SpotLBL.Caption = "1"
Case 1
SpotLBL.Caption = "2"
Case 2
SpotLBL.Caption = "3"
Case 3
SpotLBL.Caption = "4"
Case 4
SpotLBL.Caption = "5"
Case 5
SpotLBL.Caption = "6"
Case 6
SpotLBL.Caption = "7"
End Select
End With
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
2 Attachment(s)
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.
Attachment 146351
It is a lot more verbose than the other suggestions though.
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
Re: Find out which label caption I have the mouse over?
Quote:
Originally Posted by
ColinE66
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..
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)
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
Re: Find out which label caption I have the mouse over?
Quote:
Originally Posted by
Ordinary Guy
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. : )
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
Re: Find out which label caption I have the mouse over?
Quote:
Originally Posted by
Ordinary Guy
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...
1 Attachment(s)
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.
2 Attachment(s)
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:
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
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!!