简体   繁体   English

C#以编程方式添加控件相邻?

[英]c# programmatically add controls adjacently?

When I do this in form load 当我在表单加载中执行此操作时

    TextBox tb1 = new TextBox();
    TextBox tb2 = new TextBox();

    this.Controls.Add(tb1);
    this.Controls.Add(tb2);

It puts one textbox over another (not vertically or horizontally, but covering each other), which is not what I want. 它将一个文本框放在另一个文本框上(不是垂直或水平,而是相互覆盖),这不是我想要的。

I could manually try to position them programmatically, but is there a way where I can have each control appear adjacently when I add them? 我可以手动尝试以编程方式放置它们,但是有没有一种方法可以让我在添加它们时使每个控件相邻出现?

You can use a FlowLayoutPanel . 您可以使用FlowLayoutPanel
Here a short example code that you can test using Linqpad 这是您可以使用Linqpad测试的简短示例代码

Form f = new Form();
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;
flp.FlowDirection = FlowDirection.LeftToRight;
f.Controls.Add(flp);
TextBox t1 = new TextBox();
flp.Controls.Add(t1);
TextBox t2 = new TextBox();
flp.Controls.Add(t2);
f.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM