What is the most "lightweight" winforms control?
(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?
如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

评论(4)

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.

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.

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.

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.
发布评论
需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。