JeroenM
JeroenM

Reputation: 117

Windows form opening other forms

What I want
I am creating an application which has two functionalities. Both functionalities have their own form (called FactuurForm and VerhuurForm). I have another Form called Home, which has, among others, two buttons. Depending on which button is clicked, I wish to open one of the two forms, and complete close the Home-form.

What I have
Currently, I have the following code:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Home home = new Home();
        home.ShowDialog();
        if (home.kiesFactuur)
        {
            FactuurForm factuur = new FactuurForm();
            home.Close();
            factuur.ShowDialog();
        }
        else if (home.kiesVerhuur)
        {
            VerhuurForm verhuur = new VerhuurForm();
            home.Close();
            verhuur.ShowDialog();
        }
    }
}

kiesFactuur and kiesVerhuur are booleans which in my Home class, initialized as false. As soon as I click on of the buttons, the corresponding boolean will flip to true, triggering the if-statements to close the home-form and open the new form.

My question
Altough my current codes works, it seems a bit much for such a simple functionality. I feel like I wouldn't need the booleans and this go all be done easier. So is there an easier/better way to do this?
I've also thought about creating multiple Main functions. Clicking a button would activate the corresponding new Main function and terminate the current Main. Is this even possible and if so, is it a good solution?

Upvotes: 4

Views: 183

Answers (3)

Lamloumi Afif
Lamloumi Afif

Reputation: 9081

You should replace your code like this:

if (home.kiesFactuur)
{
    FactuurForm factuur = new FactuurForm();
    factuur.Show();
    this.Hide();
}
else if (home.kiesVerhuur)
{
    VerhuurForm verhuur = new VerhuurForm();
    verhuur .Show();
    this.Hide();
}

In the VerhuurForm and FactuurForm you may ovveride the event of closure like this :

public VerhuurForm()
{
    InitializeComponent();
    this.FormClosed += new FormClosedEventHandler(VerhuurForm_FormClosed);
}

void FormClosedEventHandler(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

To be sure that your application is closed if you close the form because the Home still active but hidden.

Upvotes: 0

Marwie
Marwie

Reputation: 3317

I don't exactly understand the need to completely close the home form. I'd just place 2 eventhandlers for each of the buttons and call the following code on them. The first form will be hidden and closed when you close your subform.

    private void ShowSubDialog(Form form)
    {
        this.Hide();   //makes your main form invisible before showing the subform
        form.ShowDialog();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ShowSubDialog(new FactuurForm());
        Dispose();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ShowSubDialog(new VerhuurForm());
        Dispose();
    }

Upvotes: 3

AWinkle
AWinkle

Reputation: 673

    private void Factuur_Click(object sender, EventArgs e) {
        LoadForm(new FactuurForm());
    }
    private void Verhuur_Click(object sender, EventArgs e) {
        LoadForm(new VerhuurForm());
    }
    private void LoadForm(Form f) {
        this.Hide();
        f.ShowDialog();
        this.Show();
    }

Add this to your Home form, remove everything after home.ShowDialog() from Main, and make Facturr_Click and Verhurr_Click handle their respective button's click events. This will allow Home to hide/show automatically.

Upvotes: 1

Related Questions