C# Winform: How to resize my window as per the size of user control

T.Zacks 3,996 Reputation points
2022-09-27T15:16:43.527+00:00

i have a window and user control. i want that when i will be showing my form then form window's size will be adjusted as per the size of user control.
user control will load into form from the load event of the form. in the load event i am loading user control in form and dock the user control.

so guide me what code i need to add as a result my form size will be based on user control's size. thanks

Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-10-07T09:41:29.073+00:00

    @T.Zacks , you could try to use ClientSize property to resize your window as per the size of user control.

    Here is a code example you could refer to.

     private void Form1_Load(object sender, EventArgs e)  
            {  
                this.ClientSize= userControl11.Size;  
                userControl11.BackColor = Color.Black;  
                userControl11.Location = new Point(0, 0);  
      
            }  
    

    Result:

    248399-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.