Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:14:28+00:00 2026-06-15T10:14:28+00:00

I want to autosize some generated controls. I create two TextBox types and two

  • 0

I want to autosize some generated controls. I create two TextBox types and two CustomControl types which are a type of UserControl. Each CustomControl has a Label that displays a string which I call a caption. I can only see one of the two textboxes. I can only see one of the two captions. How can I display all the Controls in the Form? I prefer not to manage control position’s myself. Rather stick to Dock settings.

 public partial class SomeForm : Form
{
    public SomeForm()
    {
        InitializeComponent();
        LoadControls();//I can only see the first control caption and textBox2
        //how can I display both textboxes and both captions?
    }

    private void LoadControls()
    {
        TextBox textBox = GenerateTextBox("First textbox");
        TextBox textBox2 = GenerateTextBox("Second textbox");

        CustomControl control = new CustomControl(labelCaption: "First control caption");
        CustomControl control2 = new CustomControl(labelCaption: "second control caption");            

        //add the textboxes to the usercontrols
        control.Controls.Add(textBox);
        control2.Controls.Add(textBox2);            

        //this displays only 1 control (incorrect)
        flowLayoutPanel1.Controls.Add(control);
        flowLayoutPanel1.Controls.Add(control2);
        flowLayoutPanel1.SetFlowBreak(control, true);
        flowLayoutPanel1.SetFlowBreak(control2, true);

        //this displays both controls (correct)
        //flowLayoutPanel1.Controls.Add(textBox);
        //flowLayoutPanel1.Controls.Add(textBox2);
        //flowLayoutPanel1.SetFlowBreak(textBox, true);
        //flowLayoutPanel1.SetFlowBreak(textBox2, true);
    }

    private static TextBox GenerateTextBox(string text)
    {
        TextBox textBox = new TextBox();
        textBox.Text = text;
        textBox.Dock = DockStyle.Top;
        return textBox;
    }
}

CustomControl:

    public CustomControl(string labelCaption)
    {
        InitializeComponent();
        Label label = new Label();
        label.Text = "Rtb..." + labelCaption;
        //label.Dock = DockStyle.Top;
        //contentPanel.Controls.Add(label);//disabled for now
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:14:29+00:00Added an answer on June 15, 2026 at 10:14 am

    The problem is your initialization using DockStyle.Top:

            CustomControl control = new CustomControl(labelCaption: "First control caption");
            control.Dock = DockStyle.Top;        
            CustomControl control2 = new CustomControl(labelCaption: "second control caption");            
            control2.Dock = DockStyle.Top;
    

    They’re going to overlay each other this way. Instead of using DockStyle, use positioning attributes:

           control.Top = 0;
           control2.Top = control.Height;
    

    That should get you where you need to go.

    A great way to learn about how to do this well is to look at the code the designer generates. Do this by hand in the designer, then look at the generated code, and it will help you generalize how to do the positioning in code yourself in the future.

    Edit based on your comment:

    You can use a FlowLayoutPanel and set a FlowBreak after each:

    flowLayoutPanel.Controls.Add(control);
    flowLayoutPanel.Controls.Add(control2);
    flowLayoutPanel.SetFlowBreak(control, true);
    flowLayoutPanel.SetFlowBreak(control2, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
I'm working on a tablix in SSRS 2008 and want my columns to autosize
I want to convert string to other type. The problem is that I know
i want to set the autosize settings for an uilabel programmatically. i want exactly
I have an ASP.NET application which uses JQuery datepicker for picking dates in some
I got a question about the NatTable SWT datagrid widget which I want to
I want what in the good old days would be a two-column table layout.
I don't want to use button controls I just want the data to be
What I want to create is a list view that will resize itself in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.