Frage

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes,Well the problem is mousehover works just 1 time and does not go back to initial position after i leave my pointer out. Can you help me? Here is the code:

{
     MsgBox = new CustomMsgBox();
     MsgBox.label1.Text = Text;
     MsgBox.button1.Text = btnOK;
     MsgBox.button2.Text = btnCancel;
     MsgBox.Text = Caption;
     result = DialogResult.No;
     MsgBox.ShowDialog();
     return result;
}

private void button2_Click(object sender, EventArgs e)
{
    button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{

    button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{

    button2.Location = new Point(+100, +100);
}
War es hilfreich?

Lösung

You should not use MouseLeave because when you move the button in the MouseHover, the button will move so the mouse will leave the button area. Meaning the No button will flip from the original position to the new position and back again all the time. What you could do is use the MouseMove to see if the user moved away from the area where the Button2 was originally and then move it back. Or include a non-visible control, like an empty label behind the button2. And set the MouseLeave on the label instead.

Oh and don't forget to set button2.TabStop = false, otherwise the user can use tab to get to the button.

Made a quick and dirty proof of concept for this, hope that helps ;)

public partial class Form1 : Form
{
    private Rectangle buttonRectangle;

    private bool checkRectangle = false;

    public Form1()
    {
        InitializeComponent();
        button2.TabStop = false;
        buttonRectangle = button2.ClientRectangle;
        buttonRectangle.Location = button2.Location;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        button2.Location = new Point(25, 25);
    }

    private void button2_MouseHover(object sender, EventArgs e)
    {
        button2.Location = new Point(50, 50);
        checkRectangle = true;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (!checkRectangle)
        {
            return;
        }

        if (!buttonRectangle.Contains(e.X, e.Y))
        {
            checkRectangle = false;
            button2.Location = buttonRectangle.Location;
        }
    }
}

The buttonRectangle is set based on where the button is found during construction of the form. It has a contains method that can be used to check if a certain point (mouse move) is contained in it.

I also set the button2.TabStop to false so it will no longer be active during tab cycles.

When your hover (can change this to mouse enter, but just used your code) event fires, I set checkRectangle to true. I use this in the mouse move event handler to see if anything should be checked (prevents from doing anything when the mouse is not "over" the button).

If the buttonRectangle and the mouse location are not intersected this means we left the area where the button was, so we can move the button back.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top