GodsCrimeScene
GodsCrimeScene

Reputation: 1270

Add an outline over a control when mouse hovers over it(sometimes)

I am trying to add help functionality to some of my controls and i am having difficulty in selecting the control for help how i would like to.

This is what i would like to do.

  1. Click Help button
  2. Change cursor to help
  3. When hovering over control show blue outline of the control
  4. When clicked pop up help event.

Currently i have everything implemented excpet #3, i can do a MouseDown event, that way my click event won't fire when selected(works ok, except for the outline). Does anyone have an idea how i can outline the control and what event would be appropriate to fire when clicked?

Upvotes: 0

Views: 1721

Answers (2)

Jake Berger
Jake Berger

Reputation: 5367

Partial Answer. Here is where I'd start.

Assuming: isInHelpMode is a private instance variable within the application that is being set to true when Help button is clicked...

Somewhere in the app:

myControl.MouseHover += (sender,eventArgs) => 
{
    if(isInHelpMode)
    {// draw blue outline
       // insert your code to draw the blue outline
    }
}

Control.MouseHover

Upvotes: 1

Tim Lentine
Tim Lentine

Reputation: 7862

Expanding upon jberger's answer, you can use the Control's MouseHover event to draw a rectangle around the control similar to this:

private void Control_MouseHover ( object sender, EventArgs e ) {
    if ( inHelpMode ) {
        var c = (Control)sender;
        var rect = c.Bounds;
        rect.Inflate(1,1);
        var g = CreateGraphics ();
        ControlPaint.DrawBorder ( g, rect, Color.Blue, ButtonBorderStyle.Solid );
    }
}

This assumes you have a private instance variable inHelpMode that you are setting when the user clicks the Help button.

You likely will also want to remove the blue outline from the control when the user moves the focus of the mouse off of the control. To do that you can use the MouseLeave event:

private void Control_MouseLeave ( object sender, EventArgs e ) {
    Invalidate ();
}

Upvotes: 3

Related Questions