Hide cursor in my form

I need help on how to hide the cursor when the mouse is over one of my application forms. I know that the Cursor.Hide () and Cursor.Show () code for the MouseEnter and MouseLeave events respectively. Applying this to the form itself does not work. So I inserted a panel (set its Dock property to Fill) to contain other controls. The code worked for the panel, but when I started adding controls, it wasn’t. I assume that I should apply code for each control. About 25 controls on my form, this seems pretty awkward.
My idea was this (I'm sorry about formatting, I'm typing this from the nightstand):

foreach(Control control in this.Controls) { control.MouseEnter += control_MouseEnter; control.MouseLeave += control_MouseLeave; } 

The code was inside FrmScreen_Load. But that still doesn't work. Did I miss something?

I work in C #, but the solutions in VB.NET are acceptable.

+6
source share
2 answers

The constructor of the form suggests using Cursor.Hide(); .

Applying this to the form itself does not work

I don’t think you will ever have to take care of the cursor when you exit the application area. At the same time that the form initializes, enter the code there.

 public Form1() { InitializeComponent(); Cursor.Hide(); } 

Another recommended way is to iterate through the ie control collection

 foreach(Control c in this.Controls) { c.Cursor.Hide(); } 
+5
source

You said the following: @msarchet I think the problem is that in the loop, when the mouse is on the control, control_MouseEnter (which hides the cursor) is called, but for a moment before that, the mouse just left another control that displays the cursor. In general, perhaps two events are fired simultaneously, so it seems that the cursor is always displayed. Just what I think

If you think this is a problem, do not do cursor.show to leave all the controls, you want it to show when you exit the panel. Set the .show cursor and cursor.hide in the panel, and all other controls will just get cursor.hide.

0
source

Source: https://habr.com/ru/post/923292/


All Articles