(Newbie) - Cursor.Hide() / MouseEnter problem.

R

Rob Lancaster

Hi. After using Cursor.Hide() and .Show() in a bigger aplication, I found
that if I included a conditional statement in a panels MouseEnter event, it
got ignored. To isolate this and make sure it wasn't something else in my
code causing this, I created the following test application, which just
includes a form, a panel and a button, and the same seems to occur.

My code is...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CursorHideTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

static bool showMouse;

private void Form1_Load(object sender, EventArgs e)
{
showMouse = true;
button1.Text = "Mouse should be visible";
}

private void button1_Click(object sender, EventArgs e)
{
if (showMouse)
{
showMouse = false;
button1.Text = "Mouse should be invisible";
}
else
{
showMouse = true;
button1.Text = "Mouse should be visible";
}
}

private void panel1_MouseEnter(object sender, EventArgs e)
{
if (!showMouse)
{
Cursor.Hide();
}
}

private void panel1_MouseLeave(object sender, EventArgs e)
{
Cursor.Show();
}
}
}

In the panel1_MouseEnter event, if I comment out the IF statement and the 2
brackets, it works as expected, with the cursor invisible when it travels
across the panel, however with the IF uncommented, the cursor is visible
regardless of whether showMouse is true or false. My expectation is that
when showMouse is set to true, the cursor will be visible over the panel,
and when it is false, the cursor will not be visible.

Can anyone give me some idea why I can't get this to work as I expect it to,
or how I can get it to work as I want?

Designer.cs and Program.cs code is just what was generated by the IDE with
no interference from me. If it would be useful to post them, let me know.

Many thanks, Rob.
 
M

Morten Wennevik [C# MVP]

Hi Rob,

After discovering another feature (running over remote desktop never ever disables the cursor ...) I think I found your problem. The documentation states that you should never use Cursor.Hide() without also having aCursor.Show(). However, the opposite also appears to be true. CallingCursor.Show() two times before Cursor.Hide() appears to detect that there is still one command calling Show and the cursor remains visible.

Try putting

if (!showMouse)
{
Cursor.Show();
}

in your MouseLeave event
 
R

Rob Lancaster

Hi Rob,

After discovering another feature (running over remote desktop never ever
disables the cursor ...) I think I found your problem. The documentation
states that you should never use Cursor.Hide() without also having a
Cursor.Show(). However, the opposite also appears to be true. Calling
Cursor.Show() two times before Cursor.Hide() appears to detect that there is
still one command calling Show and the cursor remains visible.

Try putting

if (!showMouse)
{
Cursor.Show();
}

in your MouseLeave event
--
Happy coding!
Morten Wennevik [C# MVP]

Thanks, Morten. That now works as I originally expected.

Cheers, Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top