Enabling Control from Method
-
I know I am going to be embarassed when I see the answer, but here goes. I have a simple app which looks like this:-
namespace ThisProg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
Refresh();
button1.Enabled= false;
}
public static void FinishOff(int ErrorLevel)
{
button1.Enabled = true; // this line gives error CS0120 An object reference is required for the non-static field, method, or property 'Form1.button1'
}
private void button1_Click(object sender, EventArgs e)
{
System.Environment.Exit(1);
}
}
}I've tried enabling the button using this.button1, Form1.button1, same error. Help much appreciated.
-
I know I am going to be embarassed when I see the answer, but here goes. I have a simple app which looks like this:-
namespace ThisProg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
Refresh();
button1.Enabled= false;
}
public static void FinishOff(int ErrorLevel)
{
button1.Enabled = true; // this line gives error CS0120 An object reference is required for the non-static field, method, or property 'Form1.button1'
}
private void button1_Click(object sender, EventArgs e)
{
System.Environment.Exit(1);
}
}
}I've tried enabling the button using this.button1, Form1.button1, same error. Help much appreciated.
Get rid of the "static" in your
FinishOff
method header.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I know I am going to be embarassed when I see the answer, but here goes. I have a simple app which looks like this:-
namespace ThisProg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
Refresh();
button1.Enabled= false;
}
public static void FinishOff(int ErrorLevel)
{
button1.Enabled = true; // this line gives error CS0120 An object reference is required for the non-static field, method, or property 'Form1.button1'
}
private void button1_Click(object sender, EventArgs e)
{
System.Environment.Exit(1);
}
}
}I've tried enabling the button using this.button1, Form1.button1, same error. Help much appreciated.
There are two types of "elements" that a class can have: static elements, and instance elements (where an element is a field, property, method, event, or delegate) A static element is shared by all instances, and is accessed via the class name. An instance element is unique to each different instance of the class and is accessed via the variable holding the instance reference. Think about cars for a moment: all cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean. But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car) And that's important, because if you want to affect a Car (start the engine for example) you have to be referring to a specific vehicle - and inside a
static
method you are not talking about any particular Car, you are talking about all cars, and it's just not going to work to sat "start the engine" and expect all cars engines to run. So when you declare astatic
method, it can't access anything in your class that isn't alsostatic
. You want to access a instance button, so you need to use a instance method by removing the keywordstatic
from it;'s declaration, as Dave has said. Does that make sense?Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
There are two types of "elements" that a class can have: static elements, and instance elements (where an element is a field, property, method, event, or delegate) A static element is shared by all instances, and is accessed via the class name. An instance element is unique to each different instance of the class and is accessed via the variable holding the instance reference. Think about cars for a moment: all cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean. But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car) And that's important, because if you want to affect a Car (start the engine for example) you have to be referring to a specific vehicle - and inside a
static
method you are not talking about any particular Car, you are talking about all cars, and it's just not going to work to sat "start the engine" and expect all cars engines to run. So when you declare astatic
method, it can't access anything in your class that isn't alsostatic
. You want to access a instance button, so you need to use a instance method by removing the keywordstatic
from it;'s declaration, as Dave has said. Does that make sense?Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
You're welcome!
ormonds wrote:
You should write a book.
Who buys books any more? :laugh:
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
You're welcome!
ormonds wrote:
You should write a book.
Who buys books any more? :laugh:
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!