C# 中哪个占用空间更小

发布于 2024-08-05 13:11:01 字数 55 浏览 11 评论 0原文

标签大小类似于没有文本但有边框且不可见的矩形(对于窗体上围绕控件但不包含控件的可视矩形)或面板?

A lable sized like a rectangle with no text but has a border and is invisible (for a visual rectangle on the form around controls but not to contain the controls) or a panel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-08-12 13:11:01

您要使用的是 GroupBox。这很可能并不是很重要,但标签应该比面板便宜。

What you want to use is a GroupBox. Not that it really matters, most likely, but a label should be cheaper than a panel.

又爬满兰若 2024-08-12 13:11:01

答案是;哪个占用空间较小并不重要,如果确实如此,则说明您存在设计问题(即,表单上的控件方式太多)。无论如何,您应该只使用适合该工作的控件,在本例中为面板或组框。

The answer is; it doesn't matter which has the smaller footprint, and if it does you have a design problem (i.e., you have way too many controls on your form). Anyhow, you should just use the control that fits the job, in this case, a Panel or a GroupBox.

雨后彩虹 2024-08-12 13:11:01

如果这确实是一个问题,那么在控件之间提供视觉分隔的最佳方法是处理每个选项卡页的 Paint 事件,并使用 e.Graphics.FillRectangle(...) 绘制分隔符。这样你就可以摆脱大量的控件。

如果您不能做一些简单的事情,例如在每个选项卡页上的每个控件下方绘制一个矩形,您可以编写一个代码生成例程,在表单上运行一次,然后为每个选项卡页生成如下所示的内容(通过迭代页面上的所有分隔符控件):

List<Rectangle> rects = new List<Rectangle>();
rects.Add(new Rectangle(10, 40, 200, 5)); // position of first separator
rects.Add(new Rectangle(10, 80, 200, 5)); // position of second separator
// etc.

然后将这些生成的代码例程复制并粘贴到应用程序中,并将它们用于每个页面的 Paint 事件,如下所示:

SolidBrush brush = new SolidBrush(Color.PeachPuff);
foreach (Rectangle rect in rects)
{
    e.Graphics.FillRectangle(brush, rect);
}

然后从选项卡控件中删除所有分隔符。您最终应该得到一个 List 类型的数组(每个页面一个列表),您可以实例化并填充表单的 Load 事件或其构造函数(使用生成的代码)。

不过,我必须重申埃德所说的话。 .Net 表单上可以有很多控件,而不会出现任何实际问题,因此,如果您遇到由于表单上控件过多而导致的问题,您最好重新设计整个事物。

If this is really a problem, then the best way to provide a visual separation between controls is to handle each tab page's Paint event, and use e.Graphics.FillRectangle(...) to draw the separator. You would get rid of a very large number of controls that way.

If you can't do something as simple as just drawing a rectangle underneath each control on each tab page, you can write a code-generating routine that you run one time on the form, and for each tab page you generate something like this (by iterating through all the separator controls on the page):

List<Rectangle> rects = new List<Rectangle>();
rects.Add(new Rectangle(10, 40, 200, 5)); // position of first separator
rects.Add(new Rectangle(10, 80, 200, 5)); // position of second separator
// etc.

Then you copy-and-paste these generated code routines into your application, and use them for each page's Paint event, like so:

SolidBrush brush = new SolidBrush(Color.PeachPuff);
foreach (Rectangle rect in rects)
{
    e.Graphics.FillRectangle(brush, rect);
}

Then you delete all the separators from your tab control. What you should end up with is an array of type List<Rectangle> (one list for each page) that you instantiate and fill in the form's Load event or its constructor (using the generated code).

I have to reiterate what Ed said, though. .Net forms can have a lot of controls on them without any real problems, so if you're running into problems stemming from having too many controls on the form, you might be better off redesigning the whole thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文