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.
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.