C# 动态标签后对齐文本框

C# 动态标签后对齐文本框,c#,winforms,C#,Winforms,这是我的代码: for (int i = 0; i < gBoxes.Length; i++) { var LabelID = new Label(); gLabels[i] = LabelID; LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Dra

这是我的代码:

        for (int i = 0; i < gBoxes.Length; i++)
        {
            var LabelID = new Label();
            gLabels[i] = LabelID;
            LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            LabelID.Name = "label" + i;
            LabelID.Text = gColumns[i];
            LabelID.Location = new System.Drawing.Point(12, StartLoc);
            this.Controls.Add(LabelID);
            iPanel.Controls.Add(LabelID);

            var BoxID = new TextBox();
            gBoxes[i] = BoxID;
            BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            BoxID.Name = "textbox" + i;
            BoxID.Text = gContent[i];
            BoxID.Location = new System.Drawing.Point(12, StartLoc);
            BoxID.Size = new System.Drawing.Size(240, 19);
            this.Controls.Add(BoxID);
            iPanel.Controls.Add(BoxID);

            StartLoc += 25;
        }
for(int i=0;i
这很好,但是,标签与框重叠。哪种方法是将框放置在标签之后,并将框对齐在一起的最佳方法


结果:

标签设置为“自动调整大小”
属性为
true
。(在设计器中添加它们时的默认值为
true
,但按代码添加时的默认值为
false
)还可以设置
文本框。位置
比标签的
x
值大。。。标签和文本框的起始位置与
12
x
值相同

您还可以使用
AutoSize
属性确定标签的宽度,然后相应地放置文本框。使用
AutoSize=true
添加标签。通过确定最宽的标签并将
文本框重新设置到文本框右侧的位置来排列文本框。以下是一个例子:

public partial class Form1 : Form
{
    public int YPos { get; set; }
    List<string> Labels = new List<string>();
    List<Label> LabelControls = new List<Label>();
    List<TextBox> TextBoxControls = new List<TextBox>();


    public Form1()
    {
        InitializeComponent();

        AddRow("medium string", "medium");
        AddRow("This is a longer string", "long");
        AddRow("l", "little");

        Arrange();
    }


    void AddRow(string label, string value)
    {
        Labels.Add(label);

        var LabelID = new Label();
        LabelID.AutoSize = true; // make sure to enable AutoSize
        LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        LabelID.Name = "label" + Labels.Count;
        LabelID.Text = label;
        LabelID.Location = new System.Drawing.Point(12, YPos);
        this.Controls.Add(LabelID);
        panel1.Controls.Add(LabelID);
        LabelControls.Add(LabelID);

        var BoxID = new TextBox();
        BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        BoxID.Name = "textbox" + Labels.Count;
        BoxID.Text = value;
        BoxID.Location = new System.Drawing.Point(12, YPos);
        BoxID.Size = new System.Drawing.Size(240, 19);
        this.Controls.Add(BoxID);
        panel1.Controls.Add(BoxID);
        TextBoxControls.Add(BoxID);

        // both controls have the same Y location
        // and initially will have the same X location
        YPos += 25;
    }

    void Arrange()
    {
        // determine the widest label sized by the AutoSize 
        int maxLabelX = 0;
        for (int i = 0; i < Labels.Count; i++)
        {
            maxLabelX = Math.Max(maxLabelX, LabelControls[i].Location.X + LabelControls[i].Size.Width);
        }

        // move all the text boxes a little to the right of the widest label
        for (int i = 0; i < Labels.Count; i++)
        {
            TextBoxControls[i].Location = new Point(maxLabelX + 10, TextBoxControls[i].Location.Y);
        }
    }
}
公共部分类表单1:表单
{
公共int YPos{get;set;}
列表标签=新列表();
列表标签控件=新列表();
List TextBoxControls=新列表();
公共表格1()
{
初始化组件();
AddRow(“中等字符串”、“中等”);
AddRow(“这是一个较长的字符串”,“long”);
阿德罗(“l”,“小”);
排列();
}
void AddRow(字符串标签、字符串值)
{
标签。添加(标签);
var LabelID=新标签();
LabelID.AutoSize=true;//确保启用自动大小
LabelID.Font=new System.Drawing.Font(“Arial”,7.25F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((字节)(0));
LabelID.Name=“label”+Labels.Count;
LabelID.Text=标签;
LabelID.Location=新系统图纸点(12,YPos);
this.Controls.Add(LabelID);
panel1.控件。添加(LabelID);
LabelControls.Add(LabelID);
var-BoxID=新文本框();
BoxID.Font=new System.Drawing.Font(“Arial”,7.25F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((字节)(0));
BoxID.Name=“textbox”+Labels.Count;
文本=值;
BoxID.Location=新系统图纸点(12,YPos);
BoxID.Size=新系统图纸尺寸(240,19);
this.Controls.Add(BoxID);
panel1.控件。添加(BoxID);
TextBoxControls.Add(BoxID);
//两个控件具有相同的Y位置
//最初将具有相同的X位置
YPos+=25;
}
无效排列()
{
//通过自动调整大小确定最宽的标签大小
int-maxLabelX=0;
对于(int i=0;i
上述代码生成正确放置的
TextBox
控件:


设置
标签。自动将
属性大小设置为
true
。(在设计器中添加它们时的默认值为
true
,但按代码添加时的默认值为
false
)还可以设置
文本框。位置
比标签的
x
值大。。。标签和文本框的起始位置与
12
x
值相同

您还可以使用
AutoSize
属性确定标签的宽度,然后相应地放置文本框。使用
AutoSize=true
添加标签。通过确定最宽的标签并将
文本框重新设置到文本框右侧的位置来排列文本框。以下是一个例子:

public partial class Form1 : Form
{
    public int YPos { get; set; }
    List<string> Labels = new List<string>();
    List<Label> LabelControls = new List<Label>();
    List<TextBox> TextBoxControls = new List<TextBox>();


    public Form1()
    {
        InitializeComponent();

        AddRow("medium string", "medium");
        AddRow("This is a longer string", "long");
        AddRow("l", "little");

        Arrange();
    }


    void AddRow(string label, string value)
    {
        Labels.Add(label);

        var LabelID = new Label();
        LabelID.AutoSize = true; // make sure to enable AutoSize
        LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        LabelID.Name = "label" + Labels.Count;
        LabelID.Text = label;
        LabelID.Location = new System.Drawing.Point(12, YPos);
        this.Controls.Add(LabelID);
        panel1.Controls.Add(LabelID);
        LabelControls.Add(LabelID);

        var BoxID = new TextBox();
        BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        BoxID.Name = "textbox" + Labels.Count;
        BoxID.Text = value;
        BoxID.Location = new System.Drawing.Point(12, YPos);
        BoxID.Size = new System.Drawing.Size(240, 19);
        this.Controls.Add(BoxID);
        panel1.Controls.Add(BoxID);
        TextBoxControls.Add(BoxID);

        // both controls have the same Y location
        // and initially will have the same X location
        YPos += 25;
    }

    void Arrange()
    {
        // determine the widest label sized by the AutoSize 
        int maxLabelX = 0;
        for (int i = 0; i < Labels.Count; i++)
        {
            maxLabelX = Math.Max(maxLabelX, LabelControls[i].Location.X + LabelControls[i].Size.Width);
        }

        // move all the text boxes a little to the right of the widest label
        for (int i = 0; i < Labels.Count; i++)
        {
            TextBoxControls[i].Location = new Point(maxLabelX + 10, TextBoxControls[i].Location.Y);
        }
    }
}
公共部分类表单1:表单
{
公共int YPos{get;set;}
列表标签=新列表();
列表标签控件=新列表();
List TextBoxControls=新列表();
公共表格1()
{
初始化组件();
AddRow(“中等字符串”、“中等”);
AddRow(“这是一个较长的字符串”,“long”);
阿德罗(“l”,“小”);
排列();
}
void AddRow(字符串标签、字符串值)
{
标签。添加(标签);
var LabelID=新标签();
LabelID.AutoSize=true;//确保启用自动大小
LabelID.Font=new System.Drawing.Font(“Arial”,7.25F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((字节)(0));
LabelID.Name=“label”+Labels.Count;
LabelID.Text=标签;
LabelID.Location=新系统图纸点(12,YPos);
this.Controls.Add(LabelID);
panel1.控件。添加(LabelID);
LabelControls.Add(LabelID);
var-BoxID=新文本