Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to change the size of the form programmatically

What I have tried:

C#
var formSize = new System.Drawing.Size(ucTreatmentScreen1.Size.Width + 500, this.ucTreatmentScreen1.Height + 500);
				
				this.MdiParent.ClientSize = formSize;
				this.MdiParent.MaximumSize = formSize;
				this.MdiParent.Size = formSize;
				this.WindowState = FormWindowState.Normal;
				this.ClientSize = formSize;
				this.MaximumSize = formSize;
				base.Width = formSize.Width;
				base.Height = formSize.Height;
				this.Width = formSize.Width;
				this.Height = formSize.Height;
Posted
Updated 17-Jan-22 23:44pm
Comments
Maciej Los 18-Jan-22 5:22am    
And... What's wrong with your code? What's the problem?
BillWoodruff 18-Jan-22 8:01am    
'base ? what is the context here that makes you use ;base ?

I suggest you set your MDIForm size in its code, then use use the Dock Property to make amy one of child Forms fill it.

1 solution

If I create a new MDI Parent and an MDI Child forms, then copy'n'paste your code into the child form DoubleClick handler (modified for the UserControl I don't have):
private void ChildForm_DoubleClick(object sender, EventArgs e)
    {
    var formSize = new System.Drawing.Size(500, 500);

    this.MdiParent.ClientSize = formSize;
    this.MdiParent.MaximumSize = formSize;
    this.MdiParent.Size = formSize;
    this.WindowState = FormWindowState.Normal;
    this.ClientSize = formSize;
    this.MaximumSize = formSize;
    base.Width = formSize.Width;
    base.Height = formSize.Height;
    this.Width = formSize.Width;
    this.Height = formSize.Height;
    }
Then it works: the size of both forms is changed as I would expect.
(Even though these two lines are irrelevant):
C#
base.Width = formSize.Width;
base.Height = formSize.Height;


So what am I doing that you aren't? Or you doing that I am not?
How do you test it? What does the debugger show you?
 
Share this answer
 
Comments
imran zulfiqar 2022 18-Jan-22 5:56am    
Size still do not change. Debugger shows the old values. Form display is still not changed
OriginalGriff 18-Jan-22 7:03am    
It does for me, so there must be something else in your code that is affecting it.

Try this: Create a new form, call it ParentForm and set isMDIContainer true, set it's size to 400,400. Use Show to display it from your existing app.

Add a second form, call it ChildForm, set it's size to 300,300.

In the Parent Form Shown Event, display the new child:
ChildForm cf = new ChildForm();
cf.MdiParent = this;
cf.Show();


Handle the ChildForm DoubleClick event, and use the code in my original solution.
Run your app, and double click the Child form.

The parent and child forms should both change size to 500,500.

Does that work?
Maciej Los 18-Jan-22 15:26pm    
That 3 questions at the end of your aswer should be the fixed part of any comment/answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900