Resize Controls with Form Resize
I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
c# winforms resize controls
add a comment |
I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
c# winforms resize controls
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05
add a comment |
I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
c# winforms resize controls
I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.
In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.
Here is some other things I have tried without luck:
private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
scale = this.scaleHeight;
}
else
{
scale = this.scaleWidth;
}
foreach (Control control in this.Controls)
{
control.Height = (int)(control.Height * this.scaleHeight);
control.Width = (int)(control.Width * this.scaleWidth);
this.Refresh();
// control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}
If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.
Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.
Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.
c# winforms resize controls
c# winforms resize controls
edited Apr 27 '15 at 18:58
John Saunders
148k22205365
148k22205365
asked Feb 28 '13 at 9:22
KairanKairan
2,164224688
2,164224688
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05
add a comment |
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05
add a comment |
5 Answers
5
active
oldest
votes
The best option is to use a TableLayoutPanel
. Put TableLayoutPanel
on the form, set the Dock
property to Fill
, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor
on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel
into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan
/ColumnSpan
on the controls.
By using a TableLayoutPanel
, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
add a comment |
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
add a comment |
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);
add a comment |
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
add a comment |
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15131779%2fresize-controls-with-form-resize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
The best option is to use a TableLayoutPanel
. Put TableLayoutPanel
on the form, set the Dock
property to Fill
, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor
on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel
into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan
/ColumnSpan
on the controls.
By using a TableLayoutPanel
, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
add a comment |
The best option is to use a TableLayoutPanel
. Put TableLayoutPanel
on the form, set the Dock
property to Fill
, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor
on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel
into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan
/ColumnSpan
on the controls.
By using a TableLayoutPanel
, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
add a comment |
The best option is to use a TableLayoutPanel
. Put TableLayoutPanel
on the form, set the Dock
property to Fill
, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor
on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel
into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan
/ColumnSpan
on the controls.
By using a TableLayoutPanel
, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
The best option is to use a TableLayoutPanel
. Put TableLayoutPanel
on the form, set the Dock
property to Fill
, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor
on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel
into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan
/ColumnSpan
on the controls.
By using a TableLayoutPanel
, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.
answered Mar 4 '13 at 7:01
Mohammad DehghanMohammad Dehghan
14.2k23857
14.2k23857
add a comment |
add a comment |
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
add a comment |
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
add a comment |
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.
answered Feb 28 '13 at 9:25
Freddie FabregasFreddie Fabregas
8781612
8781612
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
add a comment |
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
1
1
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
– Mohammad Dehghan
Feb 28 '13 at 9:54
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
He might use the anchor / docks. The question is: did he used it correctly?
– Freddie Fabregas
Feb 28 '13 at 10:33
1
1
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls
– Kairan
Mar 3 '13 at 7:14
add a comment |
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);
add a comment |
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);
add a comment |
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);
What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.
textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;
All these are also easily set in the Properties panel when using designer.
But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:
textBox1.Location = new Point(newX, newY);
answered Feb 28 '13 at 9:44
Nikola DavidovicNikola Davidovic
7,60612329
7,60612329
add a comment |
add a comment |
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
add a comment |
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
add a comment |
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.
Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.
I would point out I am aware that some folks online mention split containers use lots of resources.
answered Jan 29 '14 at 21:02
KairanKairan
2,164224688
2,164224688
add a comment |
add a comment |
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
add a comment |
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
add a comment |
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.
answered Mar 19 '14 at 23:36
HillbillyBlueHillbillyBlue
114
114
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15131779%2fresize-controls-with-form-resize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you have used dock and anchor property.....try using them in a better way....they should be your key to success I guess. Also can I ask for a screen shot of your UI?
– Sandy
Feb 28 '13 at 11:05