0

I have created a text box and a button. When a value is entered and button is clicked, it should generate the same number of text boxes that I have entered in the text box field.

For example if I have entered a value 5 in the text box and clicked the button, then 5 text boxes should be generated automatically.

My question is how to align those text boxes vertically?

4
  • 1
    I think it is better if you change the title to "Align textboxes dynamically using c#". Because you have asked for that instead of how to create components dynamically. Commented Jul 5, 2014 at 7:38
  • 1
    One possibility would be to use TableLayoutPanel in order to layout the new items. Commented Jul 5, 2014 at 7:47
  • You should add that as an answer. Commented Jul 5, 2014 at 7:51
  • How can this question possibly be answered without knowing the technology? WinForms, WPF, etc? And the title is wrong. Please edit the question and fix it. Commented Jul 5, 2014 at 8:36

2 Answers 2

3

If you have a starting Y value, and you know the height of each dynamically generated textbox, then simple arithmetic will give you the value of Y for each textbox:

int y = 24;
for(int i = 0; i < 5; i++) {
    Textbox tb = generateTextbox();
    tb.Top = y + i * tb.Height;
    addToForm(tb);
}

Something like this pseudocode should work.

Edit: This is for Windows Forms, which I assume you're using (at time of this writing you haven't said).

Sign up to request clarification or add additional context in comments.

Comments

0

Hello Try this it will surely help you

for (int i = 1; i <= Grab your text box value here; i++)
        {
            TextBox tb = new TextBox();
            tb.Width = width value;
            tb.Height = hight value;
            tb.TextMode = TextBoxMode.SingleLine;
            tb.ID = "Common Name" + (i).ToString();
            tb.Text = Predefine Text;
            tb.Visible = true;

        }

4 Comments

His ultimate question was how to align the text boxes, not how to generate them. His title is misleading.
It is simple just add one more line in loop tb.TextAlign = VerticalAlignment.Center;
I think - but might be wrong - that he's asking about the controls' positions on the form, not the text alignment.
Ya that's true let me find

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.