Solved

How to show the other forms with C#

Posted on 2004-08-14
5
222 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
Comment Utility
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
Comment Utility
Oh, forgot, to show the other form:

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

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 64
mapAB Challlenge 35 84
wordcount challenge 11 72
java  and programming certification ? 4 63
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

16 Experts available now in Live!

Get 1:1 Help Now