method
method

Reputation: 1389

How to select which control overlays other in Windows form application

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?

Upvotes: 1

Views: 1616

Answers (2)

RutledgePaulV
RutledgePaulV

Reputation: 2606

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.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

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

l.BringToFront();

Upvotes: 1

Related Questions