user1667191
user1667191

Reputation: 2537

Align textbox after dynamic labels

This is my code:

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

Which works fine, however, the labels overlap the boxes. Which would be the best method to place the boxes after the labels, and that the boxes are aligned together.

Result:enter image description here

Upvotes: 1

Views: 1424

Answers (1)

jltrem
jltrem

Reputation: 12524

Set the Label.AutoSize property to true. (The default value when adding them in the designer is true but the default when adding by code is false.) Also set your TextBox.Location to have a larger x value than your label... you have the starting location of label and textbox at the same x value of 12.

You can also use the AutoSize property to determine how wide the labels are and then place the textboxes accordingly. Add your labels with AutoSize = true. Arrange the text boxes by determining the widest label and resetting the TextBox.Location just to the right of them. Here is an example:

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

The above code generates properly placed TextBox controls:

auto-positioned

Upvotes: 1

Related Questions