1

I am drawing a line during runtime when the user clicks on 2 buttons basically linking them.

My code is something like:

Line l = new Line();
l.Size = new Size(#distance from button1 to button2 as width#)
l.Location = button1.Location

The problem is the buttons and other controls between the line overlays the line so it is only visible when there aren't any other controls inbetween.

How can I make the line on the top of other controls?

method
  • 1,369
  • 3
  • 16
  • 29

2 Answers2

1

You can use BringToFront() to bring the Line (or any Control) forward in the z order.

l.BringToFront();
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • It worked but when I hover over the controls that are inbetween it is not in the front anymore, is there an event like losefoucs am i supposed to look over? – method Jun 21 '13 at 19:30
0

Use

l.BringToFront();

Create an event handler for the line like so:

public void Line_LostFocus(object sender, EventArgs e)
{
    Line L = (Line) sender;
    L.focus();
}

And attach it using:

l.LostFocus += Line_LostFocus;

Though, I have to say this seems like a strange way to do things. Reconsider if you should create a custom control instead of drawing over existing ones. That seems sort of silly.

EDIT Since LineShape controls do not support focus, do this instead:

public void Line_ToFront(object sender, EventArgs e)
{
    Line L = (Line) sender;
    L.BringToFront();
}

And attach like so:

Form1.Paint += Line_ToFront;

And if that doesn't work, then iterate through every control on the form and add Line_ToFront to the paint handler. I still recommend looking for other approaches though. This seems too sloppy.

RutledgePaulV
  • 2,568
  • 3
  • 24
  • 47
  • It doesn't work, but is a control supposed to lose focus when you hover over other control? because as I said in the previous comment the line gets goes back when I hover over the overlayed controls – method Jun 21 '13 at 20:07
  • Yes. Control with focus is the control that responds to user input at the time. For example, when hovering over a button you would want the button to respond when you click. Can you explain a little bit more of what you're trying to do? There may be a better solution if you're willing to alter the structure of your application. The reason my solution didn't work is that the LineShape object (I'm assuming that is what you're using?) cannot receive focus. You can check the read-only control property Line.CanFocus, and you'll find that it returns false. – RutledgePaulV Jun 24 '13 at 13:06