Results 1 to 14 of 14

Thread: How to hide cursor?

  1. #1

    Thread Starter
    Lively Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    Earth
    Posts
    64

    How to hide cursor?

    Hi all.

    Is there any specific code to hide cursor in middle of running VB.NET project? The reason is just for AFK and make UI readable and being more user-friendly.

    And if so, How to re-show the cursor (for instance by moving mouse).
    Thanks in advanced.
    𝕬𝖒𝖎𝖗 𝕽. 𝕻𝖔𝖚𝖗𝖐𝖆𝖘𝖈𝖍𝖊𝖋𝖋

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,468

    Re: How to hide cursor?

    You really ought to make an effort to find information for yourself before asking others to provide it. There is a Cursor class in the System.Windows.Forms namespace and it has Hide and Show methods. I haven't actually checked but I strongly suspect that, had you searched for information on hiding the cursor in WinForms, you would have found that pretty quickly. Let me test that...

    What do you know? I just searched for "how to hide cursor in windows forms" and the documentation for the Cursor.Hide method was the second result. The first result was a StackOverflow question on the subject with an answer that suggested calling Cursor.Hide. That's how easy it can be to answer your own question if you try. It won't always be that easy, but you can always post a question here when you actually need to.

  3. #3

    Thread Starter
    Lively Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    Earth
    Posts
    64

    Re: How to hide cursor?

    For two main excuses:
    - Github and Stackoverflow are commonly C# and C++ based communities with high talented contributors in mentioned languages.
    - I'd like VBForums to have this question inside of it (and fortunately answered) here in VB.NET format, easy to use.

    Yes it worked! Surprisingly, I didn't add System.Windows.Forms namespace (maybe it was inserted via project references, attached to the solution, I'm not sure) but Cursor.Hide() worked very well. But I wanted to revert it (Cursor.Show()) in Form1_MouseMove event but it does not follow. I even added a test msgbox in this event to see it happened or not, doesn't respond. I also deleted the code and renewed (Cause I saw sometimes compiler bypasses Form1_MouseMove and generates Form1_MouseMove1 as the new correct one.)

    I'm suspected, I am using a wrong event to re-show the cursor. What do you think?
    𝕬𝖒𝖎𝖗 𝕽. 𝕻𝖔𝖚𝖗𝖐𝖆𝖘𝖈𝖍𝖊𝖋𝖋

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,714

    Re: How to hide cursor?

    The MouseMove event of the form will only fire if the mouse is directly over the form itself - and wont fire if the mouse is in front of one of the controls that is on the form (such as textboxes and panels etc); in cases like that the MouseMove event of the relevant control(s) will fire.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,468

    Re: How to hide cursor?

    Quote Originally Posted by pourkascheff View Post
    Surprisingly, I didn't add System.Windows.Forms namespace (maybe it was inserted via project references, attached to the solution, I'm not sure)
    Check the References page of the project properties. There you will find the namespace imports with project scope. Not surprisingly, System.Windows.Forms is imported by default in WinForms projects. You really should know that because you should have had a thorough look through the project property pages to see what is available long before now. When using something new, you should ALWAYS look through the settings/options very early on, to see what's there and what might need to be changed at the outset.

  6. #6

    Thread Starter
    Lively Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    Earth
    Posts
    64

    Re: How to hide cursor?

    Quote Originally Posted by si_the_geek View Post
    in cases like that the MouseMove event of the relevant control(s) will fire.
    I see, cause I used table layout panels and split containers all docked in fill position and there's no way to touch main form in the background.

    Is there any "e", "ex", "handler" or something to take action while mouse is moving?
    𝕬𝖒𝖎𝖗 𝕽. 𝕻𝖔𝖚𝖗𝖐𝖆𝖘𝖈𝖍𝖊𝖋𝖋

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,468

    Re: How to hide cursor?

    If you want to detect the mouse moving then you handle the MouseMove event. If you want to detect it over every control then you handle the event of every control. You can select every control in the designer and then create a single event handler using the Properties window or you can write code in the Load event handler to visit every control and use AddHandler to attach an event handler. Events work the way they work and there's no magic solution to make them work differently when it would suit.

  8. #8

    Thread Starter
    Lively Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    Earth
    Posts
    64

    Re: How to hide cursor?

    No magics @ all. There are always some new secret sophisticated features which I am not mostly aware of. I just thought to myself there is a single-line code to take mouse movement from machine itself directly. So there will be almost 100 events to code =))))
    𝕬𝖒𝖎𝖗 𝕽. 𝕻𝖔𝖚𝖗𝖐𝖆𝖘𝖈𝖍𝖊𝖋𝖋

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: How to hide cursor?

    Quote Originally Posted by pourkascheff View Post
    No magics @ all. There are always some new secret sophisticated features which I am not mostly aware of. I just thought to myself there is a single-line code to take mouse movement from machine itself directly. So there will be almost 100 events to code =))))
    You use one event to handle the mousemove event of multiple controls...

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,468

    Re: How to hide cursor?

    Quote Originally Posted by pourkascheff View Post
    So there will be almost 100 events to code =))))
    Given that I specifically stated that you could use a single event handler, why would you suggest that there would be almost a hundred? Seems like you didn't really read what I wrote.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,440

    Re: How to hide cursor?

    You can loop over every control and use the AddHandler statement. Something along the lines of this:
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim everyControl = GetControls(Me)
        For Each child In everyControl
            AddHandler child.MouseMove, AddressOf Controls_MouseMove
        Next
    End Sub
    
    Private Function GetControls(parent As Control) As IEnumerable(Of Control)
        Dim controls = New List(Of Control)()
    
        For Each child In parent.Controls
            controls.Add(child)
            controls.AddRange(GetControls(child))
        Next
    
        Return controls
    End Function
    
    Private Sub Controls_MouseMove(sender As Object, e As MouseEventArgs)
        Cursor.Show()
    End Sub
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,175

    Re: How to hide cursor?

    The Control.GetNextControl function provides a simpler way to iterate through all the child controls and their descendents. Here's an example based on DDay's code above:
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim ctrl As Control = Me.Controls(0)
            Do Until ctrl Is Nothing
                AddHandler ctrl.MouseMove, AddressOf Controls_MouseMove
                ctrl = GetNextControl(ctrl, False)
            Loop
        End Sub
    
        Private Sub Controls_MouseMove(sender As Object, e As MouseEventArgs)
            Cursor.Show()
        End Sub
    I can't forget GetNextControl, I learned it from jmc in my first posting to this forum back in 2008.

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,468

    Re: How to hide cursor?

    Quote Originally Posted by boops boops View Post
    The Control.GetNextControl function provides a simpler way to iterate through all the child controls and their descendents. Here's an example based on DDay's code above:
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim ctrl As Control = Me.Controls(0)
            Do Until ctrl Is Nothing
                AddHandler ctrl.MouseMove, AddressOf Controls_MouseMove
                ctrl = GetNextControl(ctrl, False)
            Loop
        End Sub
    
        Private Sub Controls_MouseMove(sender As Object, e As MouseEventArgs)
            Cursor.Show()
        End Sub
    I can't forget GetNextControl, I learned it from jmc in my first posting to this forum back in 2008.
    You may have learned about it from one of my CodeBank threads, which I just added a useful extension method to a matter of days ago.

    https://www.vbforums.com/showthread....=1#post5527385

    Using that method, your code would become this:
    vb.net Code:
    1. For Each ctrl In GetControls()
    2.     AddHandler ctrl.MouseMove, AddressOf Controls_MouseMove
    3. Next

  14. #14

    Thread Starter
    Lively Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    Earth
    Posts
    64

    Re: How to hide cursor?

    Best regards to all you dear contributors. ♥ Let's consider that first basic Form1_MouseMove event. The following code which expected to run repeatedly, only runs once. Can someone give me a clue which part I went wrong? Commented timers that I thought will rectify the matter, will cause not to run afk-cursor-auto-hide function even once!

    Code:
    Public Class Form1
        Dim var As Integer = 0
    
        Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            Cursor.Show()
            var = 0
            'Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            var = var + 1
            If var = 2 Then
                Cursor.Hide()
                var = 0
                'Timer1.Enabled = False
            End If
    
        End Sub
    End Class
    /Update/
    Timer1 is 500ms and enabled=true.
    𝕬𝖒𝖎𝖗 𝕽. 𝕻𝖔𝖚𝖗𝖐𝖆𝖘𝖈𝖍𝖊𝖋𝖋

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
  •  



Click Here to Expand Forum to Full Width