Tjazz
Tjazz

Reputation: 57

Open existing form with button

I have a login form, from which i want to open a "Home form", which i obviously made myself, when login is success. You can look at the part where it should open a form below. It should make a reference to the existing form right? I tried everything but i cant figure it out.

But it opens a new form instead of the form i called "HomeFRM".

How can i open that form?

this.Hide();
Form _HomeFRM = new Form();
_HomeFRM.Show();



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DELETE
{
public partial class HomeFRM : Form
{
    public HomeFRM()
    {
        InitializeComponent();
    }
 }
}

Upvotes: 1

Views: 1092

Answers (2)

Steve
Steve

Reputation: 216253

I suppose that your _HomeFrm form is defined in a file with this content (or like this)

namespace MyForms
{
     public class HomeFrm : Form
     {
          public HomeFrm()
          {
               InitializeComponent();
               .....
          }
          .... other methods and event handlers for the HomeFrm class
     }
}

Now, if you want to create an instance of this class and show it your code should create the correct class, not the base class

// This is required to get access to all classes included
// in the MyForms namespace unless the following code is
// itself inside the same namespace....
using MyForms;   

.....

this.Hide();
HomeFrm myHomeFrm = new HomeFrm();
myHomeFrm.Show();

As a side note, I suggest to not start your application with the Login form and the keep it hidden for the lifetime of your application. Insted start with the HomeFrm and inside the constructor start the Login form, save the result of the login in a global variable and in the Form_Load event handler decide if you want to continue or stop the application

Upvotes: 1

lyrqs
lyrqs

Reputation: 78

Change to:

 this.Hide();
 _HomeFRM newForm = new _HomeFRM();
 newForm.Show();

Your custom form is a new Class: "_HomeFRM"

You are creating a new instance of the Form Class (wich is the default empty one) called like your custom one.

Instead, you need to use your form as a Class.

Upvotes: 0

Related Questions