|
-
Jul 6th, 2021, 01:14 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] 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.
-
Jul 6th, 2021, 01:57 AM
#2
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.
-
Jul 6th, 2021, 12:46 PM
#3
Thread Starter
Hyperactive Member
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?
-
Jul 6th, 2021, 12:55 PM
#4
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.
-
Jul 6th, 2021, 08:32 PM
#5
Re: How to hide cursor?
 Originally Posted by pourkascheff
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.
-
Jul 7th, 2021, 04:46 AM
#6
Thread Starter
Hyperactive Member
Re: How to hide cursor?
 Originally Posted by si_the_geek
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?
-
Jul 7th, 2021, 05:33 AM
#7
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.
-
Jul 7th, 2021, 05:59 AM
#8
Thread Starter
Hyperactive Member
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 =))))
-
Jul 7th, 2021, 06:55 AM
#9
Re: How to hide cursor?
 Originally Posted by pourkascheff
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 7th, 2021, 07:13 AM
#10
Re: How to hide cursor?
 Originally Posted by pourkascheff
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.
-
Jul 7th, 2021, 08:58 AM
#11
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
-
Jul 8th, 2021, 07:23 AM
#12
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 .
-
Jul 8th, 2021, 07:44 AM
#13
Re: How to hide cursor?
 Originally Posted by boops boops
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:
For Each ctrl In GetControls()
AddHandler ctrl.MouseMove, AddressOf Controls_MouseMove
Next
-
Jul 10th, 2021, 04:47 AM
#14
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|