1

这是我的代码:

        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;
        }

效果很好,但是,标签与框重叠。这将是在标签之后放置盒子的最佳方法,并且盒子对齐在一起。

结果:在此处输入图像描述

4

1 回答 1

1

Label.AutoSize属性设置为true。(在设计器中添加它们时的默认值是,true但通过代码添加时的默认值是false。)还将您的值设置TextBox.Location为大于x标签的值...您的标签和文本框的起始位置为相同的x12

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

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);
        }
    }
}

上面的代码生成正确放置的TextBox控件:

自动定位

于 2013-10-26T00:37:23.190 回答