Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Enabling Control from Method

Enabling Control from Method

Scheduled Pinned Locked Moved C#
helpworkspace
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ormonds
    wrote on last edited by
    #1

    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.

    D OriginalGriffO 2 Replies Last reply
    0
    • O ormonds

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • O ormonds

        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.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 a static method, it can't access anything in your class that isn't also static. You want to access a instance button, so you need to use a instance method by removing the keyword static 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!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        O 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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 a static method, it can't access anything in your class that isn't also static. You want to access a instance button, so you need to use a instance method by removing the keyword static 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!

          O Offline
          O Offline
          ormonds
          wrote on last edited by
          #4

          Thank you both, excellent explanation. You should write a book.

          OriginalGriffO 1 Reply Last reply
          0
          • O ormonds

            Thank you both, excellent explanation. You should write a book.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            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!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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!

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Have you not read "The Cruel C"? ;P

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups