Visual C #: customize form size

I am trying to use a button to expand the size of my form. However, for some reason, he will not allow me to do this. I would have thought it would be easy to do, but I get an error message:

"An object reference is required for a non-static field, method or property 'System.Windows.Forms.Control.Width.get'

The code I use causes an error

    private void options_Click(object sender, EventArgs e)
    {
        FileSortForm.Height = 470;
    }

FileSortForm is the name of my form. In addition, on the advice of another site, I added this code to the form loading code.

this.Size = new System.Drawing.Size(693, 603);
+3
source share
3 answers

You need to change the height of a specific instance of your form. Most likely, in your case there thiswill be an instance that you want to change:

private void options_Click(object sender, EventArgs e)
{
    this.Height = 470;
}
+6

, FileSortForm - , . ,

private void options_Click(object sender, EventArgs e)
{
    this.Height = 470; // "this" is your form instance.
}
+2

You are trying to access a static property that does not exist. You need to reference the non-stationary method that exists.

If the options_Click method is inside your FileSortForm.

this.Height = 470;

If the options_Click method is outside of FileSortForm, you need to use the link. Sort of:

subForm.Height = 470

Edit:

Inside the containing class, the qualifier 'this' is not needed (unless you call the overridden method).

+1
source

Source: https://habr.com/ru/post/1753767/


All Articles