Winforms,使用 Dock 属性时创建填充

发布于 2024-08-30 22:40:29 字数 366 浏览 3 评论 0原文

使用dockstyle.top属性时如何在文本框之间添加填充或一些空格?

for(int i =0; i< 10; i++) {
    textboxes[i] = new TextBox();
    textboxes[i].Dock = DockStyle.Top;
    mypanel.Controls.Add(textboxes[i]); 
}

上面的代码将文本框放在彼此的正下方。如果不使用质量面板或固定定位,就无法解决这个问题。下面的事情该怎么办呢?

1)我想在框之间添加大约10-20像素。

2)如何更改文本框的大小(高度,宽度),因为使用dockstyle.top时它会忽略大小命令?

How do I add padding, or some space between the textboxes when using dockstyle.top property?

for(int i =0; i< 10; i++) {
    textboxes[i] = new TextBox();
    textboxes[i].Dock = DockStyle.Top;
    mypanel.Controls.Add(textboxes[i]); 
}

The code above puts textboxes right beneath each other. Can't figure this out without using mass panels or fixed positioning. How to do the following?

1) I would like to add around 10-20pixels between boxes.

2) How to change size (height,width) of the textboxes, since when using dockstyle.top it ignores the size commands ?

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

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

发布评论

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

评论(3

生活了然无味 2024-09-06 22:40:29

使用 DockStype.Top 您无法更改 TextBox 的宽度,因为它们已停靠。您只能更改高度。但要更改 TextBox 的高度,您必须事先设置 Multiline = true

要获得不同框之间的空间,您必须将每个 TextBox 放入面板中,设置 TextBox.Dock = FillPanel.Dock = Top>Panel.Padding = 10。现在每个文本框之间都有一些空间。

示例代码

for (int i = 0; i < 10; i++)
{
    var panelTextBox = CreateBorderedTextBox();

    this.Controls.Add(panelTextBox);
}

private Panel CreateBorderedTextBox()
{
    var panel = CreatePanel();
    var textBox = CreateTextBox();

    panel.Controls.Add(textBox);
    return panel;
}

private Panel CreatePanel()
{
    var panel = new Panel();
    panel.Dock = DockStyle.Top;
    panel.Padding = new Padding(5);

    return panel;
}

private TextBox CreateTextBox()
{
    var textBox = new TextBox();
    textBox.Multiline = true;
    textBox.Dock = DockStyle.Fill;

    return textBox;
}

我忘了什么,您也可以尝试 FlowLayoutPanel 。只需从面板中删除 DockStyle.Top 并将它们放入 FlowLayoutPanel 中即可。此外,您还应该将 FlowDirection 设置为自顶向下。也许这也可以帮助您解决您的问题。

With DockStype.Top you can't change the width of your TextBoxes, cause they are docked. You can only change the height. But to change the height of a TextBox you have to set the Multiline = true beforehand.

To get the space between the different boxes you have to put each TextBox within a panel, set the TextBox.Dock = Fill, the Panel.Dock = Top and the Panel.Padding = 10. Now you have some space between each TextBox.

Sample Code

for (int i = 0; i < 10; i++)
{
    var panelTextBox = CreateBorderedTextBox();

    this.Controls.Add(panelTextBox);
}

private Panel CreateBorderedTextBox()
{
    var panel = CreatePanel();
    var textBox = CreateTextBox();

    panel.Controls.Add(textBox);
    return panel;
}

private Panel CreatePanel()
{
    var panel = new Panel();
    panel.Dock = DockStyle.Top;
    panel.Padding = new Padding(5);

    return panel;
}

private TextBox CreateTextBox()
{
    var textBox = new TextBox();
    textBox.Multiline = true;
    textBox.Dock = DockStyle.Fill;

    return textBox;
}

What i forgot, you can also give a try to the FlowLayoutPanel. Just remove the DockStyle.Top from the panels and put them into the FlowLayoutPanel. Also you should set the FlowDirection to TopDown. Maybe this can also help you to solve your problem, too.

天冷不及心凉 2024-09-06 22:40:29

适合较小布局的另一种解决方法是仅添加一个 Label 控件,该控件随后也停靠在 Top 上,该控件不是 AutoSized,Text=" ", 高度=您的内边距。这对于使用设计器时的奇怪填充非常有用。

Another work around that suits smaller layouts is to just add a Label control afterwards also docked to the Top, which is not AutoSized, Text=" ", Height=your padding. This is quite useful for the odd bit of padding when using the designer.

不顾 2024-09-06 22:40:29

我知道你从哪里来,从 WPF 返回到 WinForms 后这尤其令人沮丧。

我建议使用 TableLayoutPanel,其中每个文本框都会获得自己的单元格,并调整单元格的属性。这应该可以解决您的填充和尺寸问题。

另一种选择是使用一些更复杂的布局控件,例如 DevExpress< /a> 的(不是免费的)。

I know where you're coming from, this is especially frustrating after returning to WinForms from WPF.

I would suggest using a TableLayoutPanel, in which each TextBox would get its own cell, and adjusting the properties of the cells. This should solve both your padding and size problems.

Another alternative would be to use some more complex layout controls, such as the DevExpress ones (not free).

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