views:

36

answers:

3

I've been trying to use a button to extend the size of my form. However, for some reason, it won't let me do this. I'd think this would be an easy thing to accomplish, but I get the error:

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

The code I'm using that causes that error is

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

FileSortForm is the name of my Form. Also, from the advice of another site, I added this code into the Form Load code.

this.Size = new System.Drawing.Size(693, 603);
+4  A: 

You need to change the height of a specific instance of your form. Most likely in your case this will be the instance you want to modify:

private void options_Click(object sender, EventArgs e)
{
    this.Height = 470;
}
heavyd
Oh. That's different.So how does the "this" work? I thought it just meant whatever object the code was for...
rar
"this" is the current instance of the class cf.: http://msdn.microsoft.com/en-us/library/dk1507sz(VS.80).aspx
tobsen
+2  A: 

It seems that FileSortForm is the name of your class, not your form instance. If this is the case, you can simply write

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

You are trying to access a static property that doesn't exist. You need to reference the non static method that does exist.

If the options_Click method is inside of your FileSortForm.

this.Height = 470;

If the options_Click method is outside of the FileSortForm you have to use the reference. Something like:

subForm.Height = 470

Edit:

Inside of the containing class the 'this' qualify is unnecessary (unless you are calling an overridden method).

Jerod Houghtelling