Stuart Den
Stuart Den

Reputation: 51

How do you make a (Windows) Form a parameter?

This might be a fairly easy question to most people, but I am just a beginner and genuinely curious. I am trying to make Form1 (A Windows Form..) into a parameter for a function, as well as this other form (Say, Form2).

I have a class in which has the codes for the most common code for buttons for all forms.

The code I'm trying to currently do is for opening a new form and hiding the previous one:

public static void navigationButton(Form currentForm, Form nextForm)
{
    currentForm.Hide();
    nextForm form = new nextForm();
    form.Show();
}

The error I get is on nextForm form = new nextForm() which says 'nextForm is a variable but is used like a type.'

I'm trying to do this so I do not have to constantly do the three lines of code over and over again and I can just write something like navigationButton(Form1, Form2) .

What is the proper way of making a Windows Form a parameter? Or is this idea inefficent?

Upvotes: 0

Views: 243

Answers (3)

Dmitriy
Dmitriy

Reputation: 939

Let me give an answer (I can't get 100% of correctness, but...)

So, let's look your code.

public static void navigationButton(Form currentForm, Form nextForm)
{
    currentForm.Hide();
    nextForm form = new nextForm();
    form.Show();
}

First of all, you have an error in nextForm form = new nextForm(), because nextForm is an object of class Form. Second, generated form object will be marked for gc and destroyed in undetermined time/way (so, If I'm not mistaken), because you won't give back any reference on it. Third, It's better to create "syntax sugar" for your code. So, let's rewrite it in possible way:

public static void NavigationButton(this Form currentForm, out Form nextForm)
{
    currentForm.Hide();
    nextForm = new Form();
    nextForm.Show();
}

You could call your code in the same manner:

Form testForm;
myForm.NavigationButton(out testForm);

Update 1: In C# 7.0 you could write last part of code in the same manner:

myForm.NavigationButton(out Form testForm);

Update 2: If you want to create "widespread" syntax sugar, use code like this:

    public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
    currentForm.Hide();
    nextForm = (T)Activator.CreateInstance(typeof(T));
    nextForm.Show();
}

with myForm.NavigationButton<Form2>(out Form2 testForm); (this is for C#7.0) or

Form2 testForm;
myForm.NavigationButton<Form2>(out testForm);

in general.

Upvotes: 1

Peter B
Peter B

Reputation: 24136

You should simply use the second form parameter, like this:

public static void Navigate(Form currentForm, Form nextForm)
{
    currentForm.Hide();
    nextForm.Show();
}

And you should then call it something like this:

Navigate(activeForm, new Form2());

Upvotes: 1

SimonC
SimonC

Reputation: 1618

Let's assume you have two forms; we'll call them formIndex and formNext.

If I understood you correctly, you have a button in formIndex, which is clicked on and then a new window opens. We'll call the button button1.

private void button1_Click(object sender, EventArgs e) {
    // User clicks on button, close this form, open next
    NavigateToNextForm(this, new FormNext(this)); 
    // Call next method with instance of current object and new form
    // Passing current instance to next form, so we can navigate back
}

internal void NavigateToNextForm(Form formIndex, Form formNext) {
    // Assuming you want to return back to this form,
    // we'll just hide the window
    formIndex.Hide();

    // Now we'll show the next one
    formNext.Show();
}

Of course, depending on what exactly you want to achieve, you may want to wait until the next form has closed (and possibly returned a DialogResult).

In which case, you could use the async-await model and Task.Run(Action).

Upvotes: 1

Related Questions