tags:

views:

201

answers:

4

(This is probably a dumb question AND a WTF combined, but here goes nothing)

I want to 'draw' lines or areas in my interface - typically separators inside a TableLayoutPanel. Currently what I do is just dump a Panel in the row and set it to Dock=Fill and give it the background required colour.

Just before doing the same thing, I wondered if another control (Label?) might be more lightweight. Or maybe I should just roll my own by inheriting from Control?

Any other solutions?

+5  A: 

I would inherit from Control and create your own as all other controls including Label and Panel directly or indirectly inherit from Control. This also allows you to encapsulate the functionality separate from the others.

I also think you could also override the Paint event for your TableLayoutPanel, or inherit from this, make it your own and draw the lines.

Daniel A. White
I went with this one in the end. I use TableLayoutPanel specifically to avoid having to get into the sordid details of pixel positions on size change.
Benjol
A: 

Why do you want to write your own? This seems like premature optimization to me. Also you need to define what you mean by lightweight, memory usage, processor cycles, etc. If you’re really that concerned about performance I’d inherit from control but I wouldn’t go down this route unless testing shows your current solution doesn’t work for some reason.

Jared
I guess you may be right, but having a control called 'LineFiller' should stop future developers (including me) from going "WTF a label 2 pixels high?!"
Benjol
+1  A: 

I would have a look at the ControlPaint class which has several methods to draw lines etc. that are specifically targetted to draw system-alike lines. Override the paint event or inherit it as Daniel says and draw the lines in there.

RobIII
+1  A: 

Here's an out-of-the-box solution from Microsoft.

Check out Visual Basic PowerPack 3.0. Don't worry too much about the name - these are components from the Visual Basic Team, but they work just fine for C# developers too.

One of the components included is LineShape, which does exactly what you'd expect from the name.

If you're targeting earlier versions of the framework, you could include the assembly with your application. But, if you're targeting 3.5 SP1 or higher, the powerpack is included.

Bevan