VB.NET mouseover button move

holemania
holemania used Ask the Experts™
on
Anyone know if it's possible to create a button where if you mouseover it, the button moves away?  I know it can be done in Java and have seen the code, but is it possible to create something like this in VB.NET?  If so does anyone have sample code I can look and see how it's done?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
Yes. Do you mean like however hard you try, the button cannot be clicked because it moves?
IT Consultant
Top Expert 2010
Commented:
You can handle the MouseEnter event of the button, e.g.
	Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
		If Button1.Left <= 0 Then
			Button1.Left += Button1.Width
		Else
			Button1.Left -= Button1.Width
		End If
	End Sub

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
As abel says, of course it can be done...but is this a WinForms (Desktop) or WebForms (Browser) application?

...and do you want the button to move away as the mouse comes NEAR it?

...or make the button simply jump somewhere else when the mouse ENTERS the it?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Top Expert 2009

Commented:
Here's a simple example on WinForms where the cursor stays in the middle of the button and drags it around on the form. Important things to note are PostToClient/PointToScreen translations and Application.DoEvents/is_moving static boolean to give the application the time to draw the button.

static bool is_moving = false;
private void btnMovable_MouseMove(object sender, MouseEventArgs e)
{
    if (is_moving)
        return;
    else
    {
        is_moving = true;
        Point locMouse = this.PointToClient(btnMovable.PointToScreen(e.Location));
        btnMovable.Location = new Point(locMouse.X - btnStartTransform.Width / 2, locMouse.Y - btnMovable.Height / 2);
        Application.DoEvents();
        is_moving = false;
    }
}

Open in new window

Top Expert 2009
Commented:
That's what you get if you try code yourself and you rename variables on the fly, apologies, try this. Name of the button on your form: btnMovable

static bool is_moving = false;
private void btnMovable_MouseMove(object sender, MouseEventArgs e)
{
    if (is_moving)
        return;
    else
    {
        is_moving = true;
        Point locMouse = this.PointToClient(btnMovable.PointToScreen(e.Location));
        btnMovable.Location = new Point(locMouse.X - btnMovable.Width / 2, locMouse.Y - btnMovable.Height / 2);
        Application.DoEvents();
        is_moving = false;
    }
}

Open in new window

Author

Commented:
Yeah wanted to move as you try to click.  Thanks i'll take a look.

Author

Commented:
Thanks that's what I was looking.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial