0

Can someone suggest the best way to achieve my goal?

So, I have a form with three buttons. What I want is, depending on what button is pressed on panel should be shown different controls (user control). I made this in a simple way: all are added from the beginning, I just make change to the visibility. But what would be nice is, if someone can suggest a more appropriate way, because there is no need to have objects created from beginning.

5
  • Try using asp:UpdatePanel: ajax.net-tutorials.com/controls/updatepanel-control Commented Mar 4, 2013 at 16:36
  • 7
    @markzzz For Windows Forms? Commented Mar 4, 2013 at 16:37
  • 1
    What have you tried? You really should post your code; it's hard to suggest changes without seeing what you have already done. Commented Mar 4, 2013 at 16:37
  • 1
    pfff... I read WebForms sorry ;) It is late! Commented Mar 4, 2013 at 16:38
  • in your case it's better to have them "from beginning" Commented Mar 4, 2013 at 16:39

2 Answers 2

6

You can always create the appropriate UserControl, and add it to the Panel.Controls at runtime. This will allow you to create the control(s) as needed, instead of on initialization of your Form.

Sign up to request clarification or add additional context in comments.

4 Comments

It's certainly possible, however I would suggest that it makes it harder to design and a little more complicated. Better to have them all there and simply show/hide based on input.
@Liath It really depends - if the user controls have long initialization, creating 3 up front can create UI hiccups. Sometimes lazy loading IS very appropriate - but it really depends on the scenario.
@Liath That is a very bad idea as the controls would overlap with each other at design time and every control would be loaded in memory which don't need to be.
@Reed Copsey - I'd go with that. Depends on the situation I guess.
0

I would indeed create the controls at design time - if there's advantage no to dynamically create them. Why complicate matters?

If there are a number of controls I would put them all in a panel (within the panel you've already mentioned) so you're only changing the visibility of a single control (the panel) rather than each one within it.

When you press the appropriate button show the appropriate panel (and remember to hide the others in case you've previously shown them)

1 Comment

I agree - unless the controls have long initialization times, in which case creating 3 may be slower than necessary (ie: creating a UI hiccup)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.