sudhara
sudhara

Reputation: 45

c# i want add minimize button my form can't use Form1.WindowState = FormWindowState.Minimized;

i want add minimize button my form

private void button6_Click(object sender, EventArgs e)
    {
        Form1.WindowState = FormWindowState.Minimized;
    }

this is not work i got error An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Form.WindowState.get'

Upvotes: 0

Views: 494

Answers (4)

nurchi
nurchi

Reputation: 800

To add to what everyone said, if you need to minimise the form from another form, use:

...
Form1 f1=new Form1();
...
f1.Show();
...
private void button6_Click(object sender, EventArgs e)
{
    this.f1.WindowState = FormWindowState.Minimized;
    // 'this' is optional
}

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66449

You need to set the value on an instance of your Form, such as the current instance that your button click event is firing in.

Use this instead:

this.WindowState = FormWindowState.Minimized;

(You technically don't need to include "this" either - depends on your preference.)


That being said, your code is actually minimizing the form, not adding a minimize button to the form, which is what your title indicates you're trying to do.

That button should show by default, unless you've made other customizations to your form that you're not including in the original question.

You can try this to show the minimize button if it's been hidden:

this.MinimizeBox = true;

Upvotes: 3

user1968030
user1968030

Reputation:

private void button6_Click(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Minimized;
     }

in button6_Click this means current form.

Upvotes: 1

Marko Juvančič
Marko Juvančič

Reputation: 5890

Yes, you're accessing the class definition (Form1), not the instance of your form. Simpy use this.

private void button6_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;
}

Only static fields and properties can be accessed in class, for everything else you need to create and/or use an instance of that class.

Upvotes: 4

Related Questions