CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2009
    Posts
    62

    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?

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Resizing question.

    My advice would be to not use complex Anchor settings to do this . I would use the FlowLayoutPanel to do most of your control resizing.

  3. #3
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    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?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Resizing question.

    Quote Originally Posted by mattst View Post
    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?

  5. #5
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    Quote Originally Posted by BigEd781 View Post
    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?
    Last edited by mattst; October 20th, 2009 at 07:15 AM.

  6. #6
    Join Date
    Sep 2009
    Posts
    62

    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?

    Cheers.
    Last edited by mattst; October 21st, 2009 at 06:47 AM.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Resizing question.

    Quote Originally Posted by mattst View Post
    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.

  8. #8
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    Quote Originally Posted by BigEd781 View Post
    I know I would not want to maintain it.
    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.

    Cheers.

  9. #9
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    Update...

    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.

  10. #10
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Resizing question.

    Quote 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.

  11. #11
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    Quote Originally Posted by zips View Post
    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.

  12. #12
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Resizing question.

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

  13. #13
    Join Date
    Sep 2009
    Posts
    62

    Re: Resizing question.

    Quote Originally Posted by vcdebugger View Post
    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.

    HTH.
    Last edited by mattst; October 22nd, 2009 at 07:40 AM.

  14. #14
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Resizing question.

    thanks for the info

  15. #15
    Join Date
    Sep 2012
    Location
    Colombia
    Posts
    1

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured