0

I want to determine if the mouse cursor is above a Windows Form and if so, change its color, but I'm kind of new to this and C# also. I was thinking of something like this:

 public Form1()
 {
     InitializeComponent();

     if (Control.MousePosition.Y >= 0 && Control.MousePosition.Y <= 499
         && Control.MousePosition.X >= 0 && Control.MousePosition.X <= 297)
            this.BackColor = Control.DefaultForeColor;
 }

but it does not work. Can anyone give me a hand? Thanks.

Edited by tanatos.daniel

4
Contributors
7
Replies
30
Views
3 Years
Discussion Span
Last Post by tanatos.daniel
Featured Replies
  • It's because you're making the buttons appear and disappear. When you move your mouse over them, your mouse "leaves" the form window and enters the button window, causing your `Form2_MouseLeave` method to execute, causing your buttons to become invisible. This causes your mouse to "enter" the form window, causing `Form2_MouseEnter` … Read More

0

Consider the MouseMove or MouseEnter events. More info here.
The MouseEventArgs parameter will give you the X and Y coordinates.

0

Assuming you have a cursor with a different colour already made, you can simply change the Cursor property of the form to your new cursor. Now every time the cursor is over the form, it will change.

0

I don't know how should I make the example there work, 'cause it does nothing. The values for X and Y are the same all the time: {X=0,Y=0}

public Form1()
{
    InitializeComponent();

    this.Text += " " + this.Location.ToString();
}
0

Try something like this:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    this.Text = "X=" + e.X.ToString() + "   " + "Y=" + e.Y.ToString();
}

this.Location is the location of the form.

Edited by ddanbe: addition

0

I managed to do the swicth of background colors and works fine, but then i added two buttons. I want the buttons to be visible or not, also conditioned by the mouse move. When i go with the mouse over the buttons, the colors of the background start changing over and over.

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.BackColor = Color.Black;           
        }

       private void Form2_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;
            this.button1.Visible = true;
            this.button2.Visible = true;
        }

        private void Form2_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = Color.Black;
            this.button1.Visible = false;
            this.button2.Visible = false;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;

        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;

        }
    }

With button1(2)_MouseEnter functions I tried to repair the problem, but they do nothing.

Edited by tanatos.daniel: I had to add something more.

1

It's because you're making the buttons appear and disappear.

When you move your mouse over them, your mouse "leaves" the form window and enters the button window, causing your Form2_MouseLeave method to execute, causing your buttons to become invisible. This causes your mouse to "enter" the form window, causing Form2_MouseEnter to execute. This makes your button reappear, causing your mouse to "leave" the form window...An infinite cycle now starts of the button appearing and disappearing.

0

I put a panel over the form, and used these functions to do the switch:

private void Form2_MouseLeave(object sender, EventArgs e)
{
    flowLayoutPanel1.Visible = true; 
}

private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e)
{
    flowLayoutPanel1.Visible = false;
}

Works now. :)

Edited by tanatos.daniel

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.