Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

How to show the other forms with C#

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
drtoto82
Asked:
drtoto82
  • 3
  • 2
1 Solution
 
drichardsCommented:
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
 
drichardsCommented:
Oh, forgot, to show the other form:

    frmFullScreen f2 = new frmFullScreen();
    f2.Show();
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
drichardsCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now