Возникает ошибка "An object reference is required for the non-static field, method, or property" - C#

Узнай цену своей работы

Формулировка задачи:

Есть два класса. Один наследуется от другого. В унаследованном класе пытаюсь перадать в конструктор базового класса ссылку на функцию-обработчик клика мыши, но не пойму почему студия ругается:
Error 1 An object reference is required for the non-static field, method, or property 'Semiconductors.OutputTextArea.clickClose(object, System.EventArgs)' F:\CW\Project\Semiconductors\Semiconductors\Semiconductors\OutputTextArea.cs 15 23 Semiconductors
Что я делаю не так? Класс-родитель:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace Semiconductors
{
    public class OutputArea
    {
        MainWindow form;
 
        public OutputArea(MainWindow f, EventHandler eventHandler)
        {
            form = f;
            Label buttonOK = new Label();
            buttonOK.AutoSize = true;
            buttonOK.Cursor = System.Windows.Forms.Cursors.Hand;
            buttonOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            buttonOK.Image = global::Semiconductors.Properties.Resources.buttonGreen25_1;
            buttonOK.Size = new System.Drawing.Size(90, 25);
            buttonOK.Location = new System.Drawing.Point(310, 370);
            buttonOK.MaximumSize = new System.Drawing.Size(90, 25);
            buttonOK.MinimumSize = new System.Drawing.Size(90, 25);
            buttonOK.Name = "Close";
            buttonOK.Text = "Закрити";
            buttonOK.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            buttonOK.UseCompatibleTextRendering = true;
            buttonOK.BackColor = Color.Transparent;
            buttonOK.MouseLeave += new System.EventHandler(label_MouseLeave);
            buttonOK.MouseHover += new System.EventHandler(label_MouseHover);
            buttonOK.Click += new System.EventHandler(eventHandler);
            form.Controls.Add(buttonOK);
        }
 
        private void label_MouseHover(object sender, EventArgs e)
        {
            ((Label)sender).Image = Semiconductors.Properties.Resources.buttonGreen25_2;
        }
 
        private void label_MouseLeave(object sender, EventArgs e)
        {
            ((Label)sender).Image = Semiconductors.Properties.Resources.buttonGreen25_1;
        }
    }
}
Унаследованный от него класс:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Semiconductors
{
    public class OutputTextArea : OutputArea
    {
        MainWindow form;
        RichTextBox richTextBox;
 
        public OutputTextArea(MainWindow f, bool fromFile, string text)
            : base(f, clickClose)
        {
            form = f;
            richTextBox = new System.Windows.Forms.RichTextBox();
            richTextBox.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(193)))), ((int)(((byte)(255)))), ((int)(((byte)(219)))));
            richTextBox.Cursor = System.Windows.Forms.Cursors.Default;
            richTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            richTextBox.Location = new System.Drawing.Point(72, 82);
            richTextBox.Name = "richTextBox";
            richTextBox.ReadOnly = true;
            richTextBox.Size = new System.Drawing.Size(547, 281);
            LoadText(fromFile, text);
        }
 
        private void LoadText(bool fromFile, string text)
        {
            if (fromFile)
            {
                try
                {
                    richTextBox.LoadFile(MainWindow.currentDirectory + text);
                }
                catch (System.IO.FileNotFoundException)
                {
                    MessBox.Show("Помилка! Не вдалося відкрити файл \"" + text
                        + "\". Перевірте його наявність в папці з даною програмою.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                richTextBox.Text = text;
            }
            form.Controls.Add(richTextBox);
        }
 
        private void clickClose(object sender, EventArgs e)
        {
            Dispose();
        }
 
        public void Dispose()
        {
            form.labelTitle.Text = MainWindow.title;
            try
            {
                form.Controls.Remove(form.Controls["richTextBox"] as RichTextBox);
                (form.Controls["richTextBox"] as RichTextBox).Dispose();
            }
            catch { }
            form.menu.InitializeMenu();
            richTextBox = null;
        }
    }
}
Ошибка происходит в строчке:
public OutputTextArea(MainWindow f, bool fromFile, string text)
            : base(f, clickClose)
В чем проблема?

Решение задачи: «Возникает ошибка "An object reference is required for the non-static field, method, or property"»

textual
Листинг программы
public class OutputArea
{
    MainWindow form;
    public Label buttonOK;
    //...
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.733 из 5
Похожие ответы