Changes View in a Windows Form

  • Thread starter Thread starter fernandez.dan
  • Start date Start date
F

fernandez.dan

Hi

My question is how to change the view of my windows form. What I want
to do is in my Windows Form is first ask the user some questions, when
the user is done she pushes a button called next.

Based on the input from the user, I would display the appropriate view
on the form.

I just don't how to do this. Should I create two panels(panel1 and
panel2) and keep one visible(panel1). Then when the user clicks next I
make panel2 visible an d panel1 invisble.

I appreciate any advice or where to get more info.

Danny
 
Danny,

If your views are preset ahead of time, panels would work fine but I prefer creating User Controls (UC). They are just as easy to create and don't clutter up your work space. If you want, you can drop them on your form or any other form (then acts similar or better than a panel.) A UC is almost the same as creating a new form with the main difference being that the UC can't execute by itself, must be used within another container (like a winform).

You can easily activate the UC at runtime.

if (userSelected == option1)

myUC1 View1 = new myUC1(); //assumes visible is set to true.

else

if (userSelected == option2)

myUC2 View2 = new myUC2();

If it makes sense, you could declare an interface and eliminate the need to have multiple View1, View2 variables and replace with just one variable like theView.

or you could try casting your custom user control down to UserControl:

UserControl theView = new (UserControl)myUC1();

UserControl theView = new (UserControl)myUC2();

Probably will still do you want because the objects on myUC1 or 2 are contained within the ControlCollectioin of the UC and will still be available in the base UC rather than only via myUC1 or myUC2

Hope this all makes sense.

Charlie

:-)
 
Cool,

Thanks for the info Charlie. My idea is basically, the user starts up
the application and the first view is a log-in view to enter their
username and password. This would be my first user control this log-in
view.

Then if successful it goes to the second view which would be my second
user control.

I would just need to alternate between the first usercontrol and second
usercontrol. Thanks

Danny
 
Cool,

Thanks for the info Charlie. My idea is basically, the user starts up
the application and the first view is a log-in view to enter their
username and password. This would be my first user control this log-in
view.

Then if successful it goes to the second view which would be my second
user control.

I would just need to alternate between the first usercontrol and second
usercontrol. Thanks

Danny

Your idea with multiple panels is not bad either. That's what they are
for, anyway.

On the other hand, you may also want to ditch the idea of a single
form and just create multiple forms. Otherwise you just facing much
more work while trying to make a single form perform like a wizard.
 
I'm still new to these windows forms thing. Do you of any URL links
that show an example of a multiple form app. Thanks

Danny
 
I'm still new to these windows forms thing. Do you of any URL links
that show an example of a multiple form app. Thanks

Danny

Danny,

I suggest you read the documentation and look into the sample code
provided with Visual Studio .NET 2003; forms are quite straighforward.
Yet this is just too much to explain in a single post here.

But, in general, you can easily instantiate and display any custom
form from any other form like this.

// Create form instance
MyOtherForm form = new MyOtherForm();

// Display form
form.Show();

- or -

// Display form modally
form.ShowDialog();

- or -

// Display form as modal to the current form
form.ShowDialog( this );
 
Back
Top