动态标签后对齐文本框

时间:2013-10-26 00:22:03

标签: 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.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;
        }

哪种方法很好,但是,标签与方框重叠。哪个是将标签放在标签后面的最佳方法,以及方框对齐在一起。

结果:enter image description here

1 个答案:

答案 0 :(得分:1)

Label.AutoSize属性设置为true。 (在设计器中添加它们时的默认值为true,但按代码添加时的默认值为false。)还要将TextBox.Location设置为比x更大的值x。您的标签...您的标签和文本框的起始位置的12值为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

以上代码生成正确放置的{{1}}控件:

auto-positioned