yonan2236
yonan2236

Reputation: 13649

What is wrong with my code? (C# Winforms)

Is there anything wrong in my code?

using System;
using System.Windows.Forms;

    public class MyProgram 
    { 
        Form Form1 = new Form();

        public static void Main(string[] args) 
        {      
            Form1.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_Closing);
            Form1 .Show();        
        } 

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
             e.Cancel = true;
        }
    }

I got this error:

Error Id: CS0120, Error: An object reference is required for the non-static field, method, or property 'MyProgram.Form1_Closing(object, System.ComponentModel.CancelEventArgs)', Line: 10, Column: 30 Error Id: CS0120, Error: An object reference is required for the non-static field, method, or property

Upvotes: 2

Views: 961

Answers (6)

SwDevMan81
SwDevMan81

Reputation: 49978

You need to put Form Form1 = new Form () in main.

Upvotes: 1

Ani
Ani

Reputation: 113402

As the error message points out, the Form1 field and the Form1_Closing method are tied to instances of the MyProgram class, not to the class itself. You could make them static to fix the problem:

 static Form Form1 = new Form();

 static void Form1_Closing(object sender, CancelEventArgs e) { ... }

Alternatively, create an instance in the main method:

// really bad code; only a demonstration
var myProgram = new MyProgram();
myProgram.Form1.FormClosing += myProgram.Form1_Closing;
myProgram.Form1.Show();       

Of course, there's no need for any of this since you don't appear to use the field anywhere else - you could just make it a local variable instead.

var form = new Form();
form.FormClosing += Form1_Closing; // make Form1_Closing a static method
form.Show();

You could also choose to write the trivial event-handler method as a lambda instead:

form.FormClosing += (sender, e) => e.Cancel = true;

Upvotes: 0

BoltClock
BoltClock

Reputation: 723428

Your Form instance variable cannot access your main class's Form1_Closing() callback because it's an instance method, but you're adding the event handler in a static context (your Main() method).

You have to either add that event handler using an instance of MyProgram, not through the Main() method:

MyProgram main = new MyProgram();
main.Form1.FormClosing += new FormClosingEventHandler(main.Form1_Closing);
main.Form1.Show();

Or declare your Form field a static field, and your Form1_Closing() event handler a static method so Main() can use them:

static Form Form1 = new Form();

private static void Form1_Closing(...)

Upvotes: 6

Demian Brecht
Demian Brecht

Reputation: 21368

I'd assume that because you're attempting to access a member variable from a static function, the member variable itself needs to be static.

Upvotes: 0

Phil Wright
Phil Wright

Reputation: 22906

You have several problems here. The main one is that you are trying to show a Form inside a console application and that is bound to fail. To have a user interface you need a user interface thread that is processing windows messages. I recommend that you use the Visual Studio templates to create a new blank WinForms application that will show you the way to create a starting Form that shows.

Upvotes: -1

JasCav
JasCav

Reputation: 34632

Basically, you are trying to use a non-static property in a static context. Calling FormClosing from the static context of Main is what's giving you the problem.

Read more about it at Microsoft's page for this error.

Upvotes: 0

Related Questions