Switching views

  • Thread starter Thread starter Jacques Cooper
  • Start date Start date
J

Jacques Cooper

Hello,

We developed a C# application.
How do you changes views within the same
application window?

With VC++ you changes views within the
same window with the SwitchView method.

TIA,
Jacques
 
Hi Jacques,

Thanks for posting in this group.
Can you show me more detailed what exactly you want to do?
In .Net, there are no concept of view, C#, VB.net all use .Net Framework
library to work.
If you want to switch between 2 windows, I think you can create 2 forms,
and switch between them.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Jacques Cooper" <[email protected]>
| Subject: Switching views
| Date: Tue, 18 Nov 2003 20:23:56 -0800
| Lines: 13
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: omega.jcsoftware.net 66.15.15.165
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:200402
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hello,
|
| We developed a C# application.
| How do you changes views within the same
| application window?
|
| With VC++ you changes views within the
| same window with the SwitchView method.
|
| TIA,
| Jacques
|
|
|
 
Hi Jeffrey,

Thank you for responding to my posting.
If you want to switch between 2 windows, I think you can
create 2 forms, and switch between them.
OK, is there a sample application that demonstrates
how to switch between multiple forms?

TIA,
Jacques
 
Hi Jacques,

In VS.net, it is easy to do this. You can first add some new forms to your
project(File|Add New Item, then select "Windows Form").
Each form is a new class that inherited from System.Windows.Forms.Form.
Your original form is has a main method, so it will be first be ran.
Then when you want to swith forms, you can create an instance of other form
and show it out.
Below is how I do in Form1's button_click event:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2=new Form2();
this.Hide();
f2.Show();
}

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

Thanks again for responding to my posting.
I have a few questions from your response:

//add this code to form1
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2=new Form2();
this.Hide();
f2.Show();
}

OK, how do I switch back to form1 from form2?
Do I create another instance of form1 in form2?

//add this code to form2
private void button1_Click(object sender, System.EventArgs e)
{
Form1 f1=new Form1();
this.Hide();
f1.Show();
}

When do these forms get destroyed?
Aren't all these forms created on the stack?
Won't the stack eventually overrun?

TIA,
Jacques
 
Hi Jacques,

Thanks for your feedback.
In my code, the form1 was just hide not destroyed.
I think you should create an overloaded constructor of Form2, which take
form1's reference as parameter.
Then in class form2 you have got a reference of form1.
In form2, you can do whatever program logic you like, if you want to reshow
the form1, you can show it out while no need to create a new instance.
So I think in one form, you should store an instance of another form. Then
you can manipulate another form freely.
Do like this:
Class Form1:
private System.Windows.Forms.Form f;
private void button1_Click(object sender, System.EventArgs e)
{
if(this.f==null)
{
Form2 f2=new Form2(this);
this.f =f2;
}
this.Hide();
f.Show();
}

Class Form2:
private System.Windows.Forms.Form f;
public Form2(Form1 f1)
{
this.f=f1;
InitializeComponent();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.f.Show();
this.Hide();
}

Then, you can swith between these 2 forms. But you must design your
application model well, for example, when closing the one of the forms, you
should either show or close another form, or the application will not
end(Because another "hide" form is still exist).

For your stack overrun problem:
First, in .Net all the class that inherited from System.Object will be
created on heap, and the class's instance is reference to this class object.
Second, you should not hide one form and then pay no attention to it. Then
it will hide and not die, tie up the system resource. You should manage
these forms well. This is why I suggest each form class manage an instance
of other forms.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top