I need to modify the Form's Clientsize in a static function but I get a compiler error - error C2597: illegal reference to non-static member 'System::Windows::Forms::Form::ClientSize. Are there any workarounds? Thanks.

Dani AI

Generated

As pointed out, you cannot access an instance property from a static method without an instance. ClientSize is an instance property of a Form and cannot be turned static — it belongs to a specific window, not the Form class itself (Form.ClientSize). If you need to change a form's client area from a static context, use one of these approaches.

Pass the Form instance into the static method (safe and simple):

public static void SetClientSize(System.Windows.Forms.Form f, System.Drawing.Size s)
{
    if (f == null) return;
    if (f.InvokeRequired)
        f.Invoke((Action)(() => f.ClientSize = s));
    else
        f.ClientSize = s;
}

Keep a static reference to the form (set it when the form is created) if a global access point is required, but clear it when the form closes to avoid leaks:

public static class AppForms
{
    public static System.Windows.Forms.Form MainForm; // set in MainForm ctor: AppForms.MainForm = this;

    public static void ResizeMain(System.Drawing.Size s)
    {
        var f = MainForm;
        if (f == null) return;
        if (f.InvokeRequired)
            f.Invoke((Action)(() => f.ClientSize = s));
        else
            f.ClientSize = s;
    }
}

You can also locate open forms at runtime via Application.OpenForms (Application.OpenForms). Always ensure UI updates happen on the UI thread — use Invoke/InvokeRequired as shown (Control.InvokeRequired). These options cover the common, safe workarounds without trying to make ClientSize static (which is not possible).

Recommended Answers

All 2 Replies

You probably either need to make the function non-static or the member data static.

There is no way to access non-static data from a static member function without an instance of the class to operate on.

How do I make ClientSize static?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.