Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to show the other forms with C#

Posted on 2004-08-14
5
Medium Priority
?
284 Views
Last Modified: 2010-04-17
HI
I already made a simple program, then added a button to the first form .
I added another form to my project and named it frmFullScreen (with file name frmFullScreen.cs)
in the button I added to Form1 I wrote this code :

Form1.Hide();
frmFullScreen.Show();

the methods didn't appear to the help type in the code viewer and on compiling it didn't work

I then made a change and wrote this :
this.Hide();
frmFullScreen.Show();

the line this.Hide() worked only when it was alone, !! but when I added the other line the second one gave me a compilation error. I don't know where the bug is , It is getting me mad . I made nothing else , just what I wrote here. If there were something to be modified in settings or some other coded to be added ... Plz tell me how to solve that program to proceed on my project.
0
Comment
Question by:drtoto82
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11800505
It doesn't work because you haven't created an instance of teh form.  You are trying to call the method on the class, but Show is not s static method of the Form class.  That's why using 'this.Hide();' worked to hide the main form.

A word of caution, though, if your code is as simple as "this.Hide(); otherForm.Show();" you're going to have trouble when you want to get back to the main form or exit the app.  I assume you've built in a way to do one of those two things?
0
 
LVL 19

Accepted Solution

by:
drichards earned 200 total points
ID: 11800513
Oh, forgot, to show the other form:

    frmFullScreen f2 = new frmFullScreen();
    f2.Show();
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11800539
You need to create an instance of frmFullScreen before you can attempt to show it.  Here is how you would do it in VB.Net:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Dim f2 As Form2 = New Form2
        f2.Show()

        AddHandler f2.Closed, AddressOf Me.f2_Closed
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Show()
    End Sub

The AddHandler() line and the f2_Closed() sub are optional.  They make the original form reappear automatically when f2 is closed.

Regards,

Idle_Mind
0
 
LVL 19

Expert Comment

by:drichards
ID: 11800693
I don't think the handler is optional - othewise you can't get back to main form.  That was the caution in the original post.

In C# it looks like this - both methods of  Form1:
---------------------------------------------------------
            private void button1_Click(object sender, System.EventArgs e)
            {
                  // Create instance of new form
                  frmFullScreen f2 = new frmFullScreen();
                  // Add handler to new form Closed event - will be called when new form has closed
                  f2.Closed += new EventHandler(this.f2_Closed);
                  // Hide current form and show new one
                  this.Hide();
                  f2.Show();
            }

            private void f2_Closed(Object sender, System.EventArgs e)
            {
                  // Re-show this form
                  this.Show();
            }
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11800870
I said it was optional because I don't know the intent of the application.  I was just answering the question asked and threw the extra code in there because he would probably need it.


Idle_Mind
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
Introduction to Processes
Loops Section Overview

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question