Results 1 to 15 of 15

Thread: [RESOLVED] Displaying different sets of controls in the same form ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] Displaying different sets of controls in the same form ?

    I'm just after a little bit of advice.

    I want to write an appication that uses a toolbar to switch views on the main form. The best way to describe this would be something like outlook where when a button (say mail to contacts etc) is clicked the main display changes without apparently opening another form and displays a different set of controls. How is this achieved would I use panels and if so how do I switch them ?

    Thanks in advance for any help.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying different sets of controls in the same form ?

    A approch that you may consider is the usage of UserControl.
    They are just like form but without any border, and the user can't close it.
    They have a Load/Close Event exaclty like Form do.

    And instead of opening them with the .show method you simply have to do
    Me.Controls.Add(New MyUserControl)
    and to close them Me.Controls.Remove(MyUserControl)

    Try it I'm sure you will found them usefull!!
    Using VS 2010 on Fw4.0

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying different sets of controls in the same form ?

    A UserControl is really only useful if you want to encapsulate a group of controls in order to hide some of the inner workings of the group and expose only select members or to reuse the group in several places. If it's a one-off situation and you want access to all members of all controls then a UserControl is probably not worth it. The simplest method would be to simply place more than one Panel on your form and then manipulate the Visible property of each Panel to show only the one you want at any one time. If you're going to do this, you have to be a little bit careful at design time to make sure that you don't put one Panel inside another. Here are the steps that I use:

    1. Click the form in the designer.
    2. Double-click the Panel in the Toolbox to add it to the form.
    3. Repeat steps 1 & 2 until you have the desired number of Panels on the form. Clicking the form between each one makes sure each is added to the form and not the previous Panel.
    4. Edit the Size/Location/Anchor/Dock properties of each Panel manually in the properties window. You can select the correct Panel from the drop-down list at the top of the properties window.
    5. Once all Panels are stacked on top of each other in the z-order you can right-click on the top one and select Send to Back. You would do this repeatedly until the Panel you want to work on is on top.
    6. Set the Visible property of each Panel to False except the one you want to show first. When you want to switch views in code, simply Hide all Panels and then Show the desired one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by jmcilhinney
    A UserControl is really only useful if you want to encapsulate a group of controls in order to hide some of the inner workings of the group and expose only select members or to reuse the group in several places. If it's a one-off situation and you want access to all members of all controls then a UserControl is probably not worth it.
    Why? I meen why a Form can be use to encapulate many controls and not a UserControl. They are really similar.
    And it will made the design quite more easier then drawing them at run time or place all controls for all option directly on the MainFrom at design time in that you may end up, with big time to many controls on the form ... don't you think?
    Using VS 2010 on Fw4.0

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying different sets of controls in the same form ?

    Like I said, a UserControl doesn't offer you any additional functionality over a Panel unless you want to use it to hide certain members of its constituent controls or you want to use it in more than one place. There is no issue with too many controls when using multiple Panels because if only one Panel is visible then only one Panel's worth of controls gets drawn
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by jmcilhinney
    Like I said, a UserControl doesn't offer you any additional functionality over a Panel unless you want to use it to hide certain members of its constituent controls or you want to use it in more than one place. There is no issue with too many controls when using multiple Panels because if only one Panel is visible then only one Panel's worth of controls gets drawn
    Not that I don't believe you, you have all my respect, but I just want to debate my case

    If there is 20+ Panels on is form and on each panels there is 20+ TextBox/Label/DataGrid/else, At design time the IDE will take the eternity long to refresh/open the form

    And the Panel have the same basic functionality has a UserControl since they Inherits the same class's
    Using VS 2010 on Fw4.0

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying different sets of controls in the same form ?

    Either will work. I guess there are pros and cons for each. My argument is more one of style while your's is perhaps more practical. I guess it depends on the individual and the situation. Having said that, if you want to have the benefits of adding event handlers, etc. at design time then you'd have to add each of the UserControls to the form at design time anyway. If you don't then you lose the benefits of having the controls as members of the form at design time, which means your code would need to be more complex.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by jmcilhinney
    Either will work. I guess there are pros and cons for each. My argument is more one of style while your's is perhaps more practical. I guess it depends on the individual and the situation. Having said that, if you want to have the benefits of adding event handlers, etc. at design time then you'd have to add each of the UserControls to the form at design time anyway. If you don't then you lose the benefits of having the controls as members of the form at design time, which means your code would need to be more complex.
    You can still expose any desired Function/Sub/property to the public scope from a Uc like Uc.Save, uc.Load(pId), ...

    P.S.
    Sorry Tinbeard for polluting your thread with all this
    Using VS 2010 on Fw4.0

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Displaying different sets of controls in the same form ?

    Here's the panel approach I use for PDA's, where multiple forms has a huge cost:

    One form with many panels, all controls are on panels.

    On form load, set the left property of all panels to 0, and the top property to something huge (big enough that it is off the screen)

    Have a sub that holds a Select Case that selects based on a form level integer variable. Each case is for a different panel:

    Select Case theState
    Case 0
    Panel1.Top=0
    Case 1
    Panel2.Top=0
    etc.

    When you want to switch panels, set the variable (theState in the above example) for the correct panel, and call the sub.

    I also have something in the sub that sets the top property for all panels to something huge. This may not be strictly necessary, since a panel that is moved into place will probably appear above one that is already there, but you may need to do this so that you can re-show a panel that has been covered up by a second panel.
    My usual boring signature: Nothing

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by Zakary
    If there is 20+ Panels on is form and on each panels there is 20+ TextBox/Label/DataGrid/else, At design time the IDE will take the eternity long to refresh/open the form
    I can testify to the veracity of this! Since the compact framework has a subset of controls, the Panel technique is a good one there. However, once I have two dozen panels, not only are they hard to manage (some have to be moved almost off the screen so that I can work on others), but any change to the underlying code will cause a pause of a few seconds before the designer comes back up. This is annoying, but the pause is only 2-4 seconds, so it isn't all that big a deal.
    My usual boring signature: Nothing

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by Zakary
    You can still expose any desired Function/Sub/property to the public scope from a Uc like Uc.Save, uc.Load(pId),
    Of course, but if this is a one-off situation then why would you bother with the extra work? If the control was to be reused then that would be a different matter.
    Quote Originally Posted by Shaggy Hiker
    some have to be moved almost off the screen so that I can work on others
    Which is exactly why it's a good idea to use the technique I suggested and stack them using the z-order, then just use "Send to Back" and "Bring to Front". You only see one Panel at a time and there is no need to move or resize any of them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Displaying different sets of controls in the same form ?

    Frankly, I only do this for PDA programs, and I have only made one of those so large that I needed more panels than I could comfortably work with.

    I'd have to look, but I'm not sure whether I have z-order in the CF.
    My usual boring signature: Nothing

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by Shaggy Hiker
    I'm not sure whether I have z-order in the CF.
    I can see how that could present a problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Displaying different sets of controls in the same form ?

    Quote Originally Posted by Zakary
    P.S.
    Sorry Tinbeard for polluting your thread with all this
    No problem at all, I learn so much from these forums and have a great respect for all those that take time to contribute so people like me can benefit.

    Back to the discussion I think that I'll go with the panel option as there will only be a few panels and it's more or less what I imagined I'd have to use, but really didn't have the know how

    Thanks again for your help

  15. #15
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: [RESOLVED] Displaying different sets of controls in the same form ?

    Good!

    The important is that you get helped
    Using VS 2010 on Fw4.0

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