Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# : Spring Panels?

Options
  • 21-10-2004 2:41pm
    #1
    Registered Users Posts: 15,443 ✭✭✭✭


    Guys,

    I'm tooling around with a number of forms at the moment where I have a situation with a listbox o the left and right, and a set of buttons in between.

    (Yes, its the good old "add/add all/remove/remove all" buttons).

    Anyway...the form this set of controls is on is resizeable. I want to set the two listboxes to "spring" the horizontal width - i.e. I want them *both* to resize, each taking up half the width-change of the form. Thus, the buttons will always stay centered, and the two listboxes will always resize equally....if y'see what I mean.

    Does anyone know of a way to do this *without* writing code in the resize event ? Basically, I'm looking for the equivalent functionality of a Spring Panel, but MS didn't see fit to give us one of those :(

    jc


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    .Net actually handles this really well using my 2 favourite windowsforms features, docking and anchoring.

    Anchoring basically keeps the distance of an edge of a component constant to an edge of it's parent. For example if your right hand listbox is right-anchored and you move the right border of your window, then the right hand side of your listbox will move with it. If your listbox isn't also left-anchored then the whole listbox will move, if it is then the distance of the left side of your listbox will stay constant to the left of the window, i.e. the listbox wil expand horizontally.

    Docking basically just sticks a component to the top/bottom/left/right/middle of it's parent. By using several panels in a window each docked and anchored and having components within them docked and anchored it's easy to create a complex gui that resize flawlessly. That won't be nessecary for your example but it's worth playing around with to get a feel for what you can do. For your example there's 2 obvious choices, try both and see which suits better.

    1. Dock the left listbox to the left of the window and anchor it to the right, also dock the right listbox to the right and anchor it to the left.

    2. Anchor both listboxes to the left and the right.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Thanks for the suggestions Steve, but I'm afraid you're wrong.... .Net doesn't handle this well at all.

    I'm well used to docking and anchoring, but they are not sufficient for this task, or at least, there is no obvious way of making them sufficient.

    Dealing with your two solutions in order...here's why they won't do what I want :

    1) Not possible - Docking and anchoring are exclusive.

    Dock a control in the designer, and then change its anchoring, and you'll see that the dock property is set back to None. Anchor, then dock, and you'll see the anchor reset accordingly as well. You can try modifying the code outside the Visual Studio environment so that both are set the way you want, and it doesn't do anything.....the compiler and/or run-time gives docking instructions precedence over anchoring.

    2) Doesn't work.

    Anchoring to both sides causes each control anchored thusly to resize by the same amount as the form. So if the form gets 20 points wider, each listbox will get 20 points wider. What I want is that each gets 10 points wider....that they split the change equally.

    In short, I can use this situation to resize one listbox, and leave the rest static, but I can't get it to do both.

    jc


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Sorry, I was just replying from memory, I don't have VS handy so I never checked. Now that you mention it, I remember what you're saying about the dock turning off when you change the anchor.

    I have done something similar to this before but I can't remember exactly how. If I can find how I did it I'll post it up.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    No problem. I can solve it using code....I'd just prefer to have a more flexible design solution, so that if/when I need to do this with more complex forms, or greater numbers of controls, it will be easy...

    Cheers,

    jc


Advertisement