Solved

How to show the other forms with C#

Posted on 2004-08-14
5
234 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 50 total points
ID: 11800513
Oh, forgot, to show the other form:

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

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now