PDA

View Full Version : [SOLVED] VBA- how can i have a userform behaviours just like controltiptext?



mahhdy
05-15-2018, 05:48 AM
Hello,

I need a small userform to pops up after mouse goes over my specific controls and disappears after mouse leaves them. Is there any way for that, or any workaround this? now I am using a userform + application.ontime command to close that form after 2 seconds, but this does not look good at all, I assume.
The controls are in the main form and the second form is loaded base on mouse move event. All of my labels (controls), are Runtime generated and are assigned to a class which make them draggable. My problem is when second form is loaded and have the focus. If I make the second form modeless, it will not work at all! So I think there should be something out here for this. For sure some controls like calender date pickers. Thanks a lot for your help.


Regards,
M

SamT
05-15-2018, 07:13 AM
Best I can think of.

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
If x > Me.Width - 7 Then MsgBox "Time to unload the Form"

'If x < ? Then MsgBox "Time to unload the Form"
'If y > ? Then MsgBox "Time to unload the Form"
'If y < ? Then MsgBox "Time to unload the Form"
End Sub
The minimum from the Left edge of the form the mouse will register on is 6.25 points. On my computer. Maybe the Form Border is 6.25 points thick :dunno:

mahhdy
05-15-2018, 08:20 AM
Hi,

I think I was not clear enough. I have twouserform. The second form is loaded base on mouse move on the main form. I know if the user goes over the second form, I can unlod it base on mouse move event on that, but there is lots of labels along each other. user just moves around them, not each time goes over the new userform.

I hope iwas clear enough this time. I edited my OP as well.

Thanks for your attention.


Best I can think of.

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
If x > Me.Width - 7 Then MsgBox "Time to unload the Form"

'If x < ? Then MsgBox "Time to unload the Form"
'If y > ? Then MsgBox "Time to unload the Form"
'If y < ? Then MsgBox "Time to unload the Form"
End Sub
The minimum from the Left edge of the form the mouse will register on is 6.25 points. On my computer. Maybe the Form Border is 6.25 points thick :dunno:

mahhdy
05-15-2018, 09:18 AM
Hi,

I think I was not clear enough. I have two userform. The second form is loaded base on mouse move on the main form. I know if the user goes over the second form, I can unlod it base on mouse move event on that, but there is lots of labels along each other. user just moves around them, not each time goes over the new userform.

I hope iwas clear enough this time. I edited my OP as well.

Thanks for your attention.


Best I can think of.

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
If x > Me.Width - 7 Then MsgBox "Time to unload the Form"

'If x < ? Then MsgBox "Time to unload the Form"
'If y > ? Then MsgBox "Time to unload the Form"
'If y < ? Then MsgBox "Time to unload the Form"
End Sub
The minimum from the Left edge of the form the mouse will register on is 6.25 points. On my computer. Maybe the Form Border is 6.25 points thick :dunno:

SamT
05-15-2018, 10:48 AM
Ideas?

Sub SomeControl_MouseMove(...)
UserForm2.Show
End Sub

UserForm1_MouseMove(...)
Unload UserForm2
End sub


You will still need some delay with Application.EnableEvents = False after Control Mouse Move before USerFOrm1 Mouse move

What is the purpose of the second UserForm? Would Control ToolTips Work?

mahhdy
05-15-2018, 11:42 AM
Have you tried this one?
When second form is loaded, userform1_mousemove is not working. Evenif you click on userform1 area it will beep!,

as a workaround, I think if I just load the second userform when the user keeps mouse over that label for instance for 1 Sec, I think at least I know that this form is what user is needed, so i can assume he/she should close it manually. Do you have any suggestion for this one?


Ideas?

Sub SomeControl_MouseMove(...)
UserForm2.Show
End Sub

UserForm1_MouseMove(...)
Unload UserForm2
End sub


You will still need some delay with Application.EnableEvents = False after Control Mouse Move before USerFOrm1 Mouse move

What is the purpose of the second UserForm? Would Control ToolTips Work?

p45cal
05-16-2018, 01:10 AM
Is there some reason you don't use each control's ControlTipText property?

mahhdy
05-17-2018, 07:54 AM
Hi,

I am using the ControlTipText for another purpose, also I need rich texting features which that is not giving me.

Regards,
M

Is there some reason you don't use each control's ControlTipText property?

p45cal
05-17-2018, 11:10 AM
Well, if you're already adding controls at runtime, instead of a whole new userform, perhaps you could add (and remove/delete) rich text boxes at runtime too. There's no native rich text box control, but there is one that can be added/registered or many people say you can use an InkEdit control, which is native to Excel.

Confirm all you're wanting to do is pop up a message/s as the user hovers around the first useerform? No other type of control?

Perhaps rustle up a workbook with the sort of thing you're doing and attache it here?

mahhdy
05-17-2018, 11:30 AM
Here is what I have so far:

I have something which works, But I know that is not a clean way to do that. Anyway, I also added a hidden frame to my main form which will be visible on mouse move.

Here is my code:
This Click event on a class module will show the second userform of CurrentJob


Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Dim m, n&
If Button = XlMouseButton.xlPrimaryButton And MainMap.Edit.Caption = "Done" Then
Label1.Left = Label1.Left + X - x_offset
Label1.Top = Label1.Top + Y - y_offset
ElseIf MainMap.Edit.Caption = "Go to Edit Mode" Then
With MainMap.Frame1
.Visible = True
.WorkerN = Label1.Caption
.LBcurr = openJobs
.LClsd = WorksheetFunction.CountIfs(oprecord.Range("e:e"), Label1.Caption, oprecord.Range("f:f"), Date, oprecord.Range("s:s"), "CLOSED")
End With
'if wanted to use the separeate form
If MainMap.TglB1 And Not CurrentJob.Visible Then
With CurrentJob
.Caption = "Current Job of " & Label1.Caption
.LCurr = openJobs
.LLast = LastJob
.LClsd = WorksheetFunction.CountIfs(oprecord.Range("e:e"), Label1.Caption, oprecord.Range("f:f"), Date, oprecord.Range("s:s"), "CLOSED")
n = Right(Label1.Tag, Len(Label1.Tag) - 1)
.LAc = IIf(n > 288, "N/A", Fix((n - 1) / 24) + 70006)
m = WorksheetFunction.VLookup(Label1.Caption, rooster.Range("b:e"), 4, 0)
.LSkill = Right(m, Len(m) - InStr(1, m, " "))
.StartUpPosition = 0
.Top = IIf(Label1.Top > MainMap.Height / 2, Label1.Top - .Height, Label1.Top + Label1.Height)
.Left = IIf(Label1.Left > MainMap.Width / 2, Label1.Left - .Width, Label1.Left + Label1.Width)
.Show
End With
End If
' separate form code ends here
End If
End Sub
Also on the second form:


Private Sub UserForm_Activate()
ontime = True
Application.ontime Now + TimeValue("00:00:02"), "closeee", Now + TimeValue("00:00:07")

End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'On Error Resume Next
ontime = False
x1 = X
y1 = Y
mouseleft Me
End Sub
and also in another module


Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type

Sub closeee()
If ontime And CurrentJob.Visible Then
CurrentJob.Hide
ontime = False
End If
End Sub

Sub mouseleft(Optional ByRef frm As Object)
Dim CurPos As POINTAPI, h1&, w1&
GetCursorPos CurPos
If frm Is Nothing Then
h1 = 0
w1 = 0
Else
With frm
h1 = .Height
w1 = .Width
End With
End If
Do While CurPos.X <= x1 + h1 And CurPos.Y <= y1 + w1 And CurPos.X > x1 And CurPos.Y > y1
frm.Visible = True
DoEvents
Loop
If frm.Visible Then frm.Hide

End Sub
I also used one of the class codes to detect when mouse left these labels in my main form.

Anyway, I think if I can do it by that wait triggered method, that would be better. I didn't work on that, as I am developing other parts of my app now.

Thanks a lot.
M

mikerickson
05-17-2018, 05:55 PM
....also I need rich texting features which that is not giving me.

Regards,
MUserform controls do not accept rich text formatting.

Also cross-posted https://www.mrexcel.com/forum/excel-questions/1055571-vba-how-can-i-have-userform-behaviours-just-controltiptext.html

p45cal
05-18-2018, 01:41 AM
Here is what I have so far:No-one's going to spend the time needed to rebuild (probably guessing wrongly what you have) your workbook for themselves to experiment with.
Put together a simple workbook and attach it here.

mahhdy
05-18-2018, 01:22 PM
My Friend,
I din't get that. I just think you may be interested to what I coded for that purpose. Anyway. Jaafar provided me something very good. I am posting the link for the refrence.
https://www.mrexcel.com/forum/excel-questions/1055571-vba-how-can-i-have-userform-behaviours-just-controltiptext-post5071279.html#post5071279

I also will make a workbook for you as well and attach it in my next post. Thanks to Mike as well.

Regards,
M


No-one's going to spend the time needed to rebuild (probably guessing wrongly what you have) your workbook for themselves to experiment with.
Put together a simple workbook and attach it here.

SamT
05-18-2018, 02:08 PM
I just think you may be interested to what I coded for that purpose.
What do you mean?

mahhdy
05-18-2018, 03:34 PM
Hello Again,
I replied to our friend which asked a sample workbook. I just explained why I post my codes. I agree it was better if I clean that then post it. That was my bad :bow:.
I spend 1 hour, but was not able to do a clean up on my real file! but I am attaching a simple demo one right now. and Will change the thread status to solved. This Sample file have Jaafar Code in it.

22276
Cheers,
M



What do you mean?

p45cal
05-19-2018, 04:57 AM
See attached. Pop-up disappears after 3 secs. Doesn't interfere with buttons being clicked. Less code.
If I get time today I'll add code to deal with multiple buttons and different messages.

mahhdy
05-19-2018, 11:25 AM
Thanks for your effort. Please find below image. My form is already busy enough. Also the labels are draggable between shifts and airplanes. I erased some parts of labels as they was name of people. So your approach is not working. What I meant by Rich text, was at least text wrap.... Now these labels according to their status, absent or present, Have job assigned or not have different appreance.

22283


See attached. Pop-up disappears after 3 secs. Doesn't interfere with buttons being clicked. Less code.
If I get time today I'll add code to deal with multiple buttons and different messages.

SamT
05-19-2018, 11:44 AM
WOW! That's an amazing bit of coding.