If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Resizing question.

I've a problem resizing a form in 'design' mode of Visual C# 2008 Express.

There are loads of controls in my form, style 'sizable', which have a horribly complex set of anchoring settings (which took me ages to set up) so that the form resizes appropriately.

Now I want to add a menu bar to the top. I can't extend the size of the form downwards so that I can then select all the controls and move them all down a bit to give room at the top for the menu. Instead when I extend the size of the form downwards, all the controls resize and move.

Is there a quick way I can temporarily stop this from happening which won't kill all my anchoring (and control positions) settings but will allow me to resize the dialog to fit the menu in?

Re: Resizing question.

Originally Posted by mattst

FlowLayoutPanel does not allow you much control over how a form looks, it limits your design and was not an option for this particular application.

Anyone?

Well, if you are relying on the Anchor property alone to resize controls, the designer is going to account for that. So, unless someone else knows a way around this (which would be nice, because I have had the same problem before), you may be SOL. Have you thought about creating user controls instead of filling your form with many single controls?

Re: Resizing question.

Originally Posted by BigEd781

Well, if you are relying on the Anchor property alone to resize controls, the designer is going to account for that. So, unless someone else knows a way around this (which would be nice, because I have had the same problem before), you may be SOL. Have you thought about creating user controls instead of filling your form with many single controls?

No I hadn't thought of that. How would creating user controls effect this problem? (EDIT. Less anchoring to turn off and on again??)

My form has 6 group boxes and 24 controls - every one has anchoring settings. It took me quite a while to get them all set. There are 2 ways I can think of to sort this out...

1. Go through each control in the designer, noting down what the anchor settings are and then removing them. Changing the size of the form, adding the menu, then changing the anchor settings back.

2. Manually cutting or commenting out the anchoring settings from TheMainForm.designer.cs file. Then changing the size of the form, adding menu, then pasting or uncommenting out the anchor settings again.

It looks like a really tedious task, which will be quite time consuming, just achieve something so straight forward as increasing the size of a form in the designer.

Are there any other options open to me? Surely there must be a way around this?

Re: Resizing question.

Here's an update with a working solution to get around the form design problem: I can't resize a form without anchored controls also resizing.

In the absence of solutions here I posted the same question on the newsgroup "microsoft.public.dotnet.languages.csharp". Someone came up with a solution.

Close Visual C# and then open the xxx.Designer.cs file, which corresponds to the form, in a text editor. [Edit. Visual C# does not need to be closed, xxx.Designer.cs file can be edited manually in Visual C# and the form size will change when you switch back to the designer.]

I was advised to manually change the "this.Size = new Size(xxx, yyy);" line to a new size. In fact there was no such line. However instead I changed the ClientSize line.

"this.ClientSize = new System.Drawing.Size(xxx, yyy);"

It worked - resizing the form but without the anchored controls also resizing. Then I simply highlighted all the groupboxes and controls, moved them down a bit and added the menu. Of course I had to close and reopen Visual C# 2008 Express several times while I fine tuned the exact amount to make the form bigger by - but it has worked perfectly.

Hope this helps anyone with the same problem. I think the options pertaining to the Windows Form Designer should include an option to disable anchoring when in design mode, so that it can be ticked on and off while a user is designing their form. Maybe in the next version.

PS. Anyone know of any problems that might occur from reiszing a form like this?

Re: Resizing question.

Originally Posted by mattst

PS. Anyone know of any problems that might occur from reiszing a form like this?

Cheers.

Just that it is a maintenance headache. I mean, of course you can always hand edit the generated code, but you shouldn't have to; that's what the designer is for. When you need to move things around again (or if someone else has to maintain this code) it will be a real pain. If at all possible I would really try to find a way to accomplish this so that it can work in the designer, but I don't know anything about your project. If it is a one-off kind of thing, perhaps your method is fine. If it is production code... I know I would not want to maintain it.

Re: Resizing question.

I wouldn't want to maintain it either, I didn't even want to do it. And it's just little old me to have this as a headache, no one else's problem.

Still the manually editing method is a lot quicker than switching all the anchoring off, changing the size, and then reentering the anchoring.

A form property 'bool EnableAnchoring' would solve the problem.

If anyone knows of an IDE solution, and is coming to this thread late, please do reply. I know that I'm going to need to do this again, probably several times, and it'd be more convenient not to have to solve the problem by this method.

Re: Resizing question.

Visual C# does not need to be closed, xxx.Designer.cs file can be edited manually in Visual C# and the form size will change when you switch back to the designer.

Another solution from the newsgroup.

"Another option is to select all the controls that you want to preserve, click cut, resize the form, and then click paste."

That also works, though I must admit it's against every instinct of mine to cut everything like that and put my faith in paste (and undo) - so much so that I backed up my project before even testing that method.

Re: Resizing question.

Originally Posted by mattst

Another option is to select all the controls that you want to preserve, click cut, resize the form, and then click paste.

Note that when you do this Visual Studio loses all of your event handler assignments for those controls. The methods are still there in your cs file, but have to be reassigned - you can do this in the designer events page.

Re: Resizing question.

Originally Posted by zips

Note that when you do this Visual Studio loses all of your event handler assignments for those controls. The methods are still there in your cs file, but have to be reassigned - you can do this in the designer events page.

Errr no. Maybe different versions behave differently but in Visual C# 2008 Express Edition the event handlers are not lost when you do that. I've now done it several times and the control's events are maintained and function perfectly with a compilation just moments later.

Re: Resizing question.

Originally Posted by vcdebugger

can some one explain what do you mean by anchoring of user controls here in some simple words and make life easier for me ?

When designing a form in Visual C# you can set 'anchors' for controls in their properties. In so doing you can set how that control behaves when a user resizes a form at run time. Buttons can stay alligned to a particular edge for instance, listboxes can grow bigger (or smaller), etc. Controls can have left, top, right, and bottom anchoring, none, any or all. A control with all 4 directions set will grow or shrink in exact proportion to the form growing or shrinking. It's a useful mechanism to control resizing behaviour without having to code it all yourself in response to a form's resize event.

There are lots of tutorials on the web, though the ones I looked at were somewhat confusing (however I didn't look hard for a good guide). I suggest playing around with it, you'll soon see how it works. The only downside is the inconvenient side effect discussed in this thread.

Re: Resizing question.

Re: Resizing question.

Put all your controls on a panel with size same to form size.
And then, manipulate the property Anchor of Panel.
The anchor of contained controls is relative to the panel.
The anchor of panel is relative to the form.