0

I'm newbie to C#. Trying to call method from inner class and got: An object reference is required for the non-static field, method, or property. Looks like there is something wrong with panel1.Size = new System.Drawing.Size(100, 100);?

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class MainForm : Form
{
    public class SomeClass : PictureBox
    {
        public SomeClass()
        {
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouseDown);
        }
        public void mouseDown(object sender, MouseEventArgs e)
        {
            resizePanel();
        }
    }

    Panel panel1 = new Panel();
    SomeClass someObject = new SomeClass();

    public MainForm()
    {   
        this.Controls.Add(this.panel1);
        this.panel1.Size = new System.Drawing.Size(200, 200);
        this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.draw);
        this.panel1.Controls.Add(this.someObject);
        this.someObject.Location = new System.Drawing.Point(0, 0);
    }

    static void resizePanel() {
      panel1.Size = new System.Drawing.Size(100, 100);
    }

    public void draw(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.Red, 0, 0, 200, 200);
    }

    public static void Main()
    {
        Application.Run(new MainForm());    
    }
}

1 Answers1

1

Before going to use any Object Oriented Language. You first need to fully understand the concept of OOP. A static method of any class is a method that uses static properties of the class. Static properties/methods are those properties whose value is same in all instances of the class. It is logically incorrect to use a non-static property/method in a static function of the class.

Thanks

Amaid Niazi
  • 162
  • 9