views:

52

answers:

2

Is this a stupid question? I am trying to navigate the mystical world of C# Visual Studio. I want to be able to replace a panel at some point in run time, but the designer view only has a static view of a single state of the form.

For instance there could be panel1 on the form, with button1 and picturebox1. Then, if the users presses button1, panel1 will be replaced with panel2. Panel2 has button2 and picturebox2.

I know how to make panel1 on the designer just fine. Isn't there some way I can use the designer to make panel2? Can you only use the designer to make one state of the form and then you have to do any other state by hand?

+1  A: 

I haven't used the C# form designer specifically, but in general you design the initial state of the form, and do the rest (all the dynamic changes) in the code. Now, it might be possible to put panel2 on the form, make it invisible, and just toggle the visibility of panels when button1 is pressed, but most likely this wouldn't be a good solution.

Alex - Aotea Studios
exactly, because it produces a big overhead of the form. Especially if you got a lot of controls
KroaX
+1  A: 

I don't know of any method to display different states of the form in the designer. It does however sound as if what you want do is two different user controls. I would suggest that you create one form that can shift between two user controls in the code. It's not exactly what you asked for but at least now you don't have to code the different panels outside of the designer. This is what you'll need to do:

  1. UserControlPanel1 and UserControlPanel2 where you design the different layouts of your views.
  2. PanelLoaderForm where you leave room for loading the different user controls. Depending on your needs you can implement code to show the different user controls.

This is VB.NET but the code in your PanelLoaderForm could look something like this:

Private Sub LoadUserControl()
   If UsePanel1 Then
      Controls.Add(new UserControlPanel1())        
   Else
      Controls.Add(new UserControlPanel2())        
   End If
End Sub

Then if you want to shift between the two call Controls.Remove(oldControl) or something before adding the new one.

MikaelHalen
Sorry, what does "shift between two user controls" mean?
lala
I edited my answer, I hope it clarifies what I meant. Please come back if a need to elaborate more.
MikaelHalen