Consulting

Results 1 to 18 of 18

Thread: VBA- how can i have a userform behaviours just like controltiptext?

  1. #1
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location

    VBA- how can i have a userform behaviours just like controltiptext?

    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
    Last edited by mahhdy; 05-15-2018 at 08:17 AM. Reason: Clearing the question

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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 :
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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.

    Quote Originally Posted by SamT View Post
    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 :

  4. #4
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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.

    Quote Originally Posted by SamT View Post
    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 :

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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?

    Quote Originally Posted by SamT View Post
    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?

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Is there some reason you don't use each control's ControlTipText property?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    Hi,

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

    Regards,
    M
    Quote Originally Posted by p45cal View Post
    Is there some reason you don't use each control's ControlTipText property?

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    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?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location

    Angry My Code so far...

    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

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Quote Originally Posted by mahhdy View Post
    ....also I need rich texting features which that is not giving me.

    Regards,
    M
    Userform controls do not accept rich text formatting.

    Also cross-posted https://www.mrexcel.com/forum/excel-...oltiptext.html

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by mahhdy View Post
    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.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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-...ml#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

    Quote Originally Posted by p45cal View Post
    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.

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I just think you may be interested to what I coded for that purpose.
    What do you mean?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  15. #15
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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 .
    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.

    Demo.xlsm
    Cheers,
    M


    Quote Originally Posted by SamT View Post
    What do you mean?

  16. #16
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    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.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  17. #17
    VBAX Regular
    Joined
    Aug 2017
    Posts
    9
    Location
    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.

    VBA-Express.jpg

    Quote Originally Posted by p45cal View Post
    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.

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    WOW! That's an amazing bit of coding.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •