Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to show the other forms with C#

Posted on 2004-08-14
5
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

722 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