Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an abstract class "Element" which its instances have to use the function DrawRectangle() from System.Drawing.
The problem is that because the class is abstract it can't inherit from FORM so the program doesn't recognize the classes..
How can I solve this?

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MyMouseGame
{
        public abstract class Element
        {

        //Form1 myForm = new Form1();// not sure I need it
        public enum EnumShape { Square, Circle, Triangle }
        public enum EnumType { Chase, Escape, Random }

            //variables
            private double X { get; set; }
            private double Y { get; set; }
            private int Size { get; }
            private int Speed { get; }
            private EnumShape Shape { get; set; }
            protected EnumType Type { get; set; }

            private static Random R = new Random();

            //methods

            public int generateRandomNum()
            {
                int randomNum = R.Next(20, 100);
                return randomNum;
            }
            public Element()
            {
                X= 0;
                Y = 0;
                Shape = 0;
                Size = generateRandomNum();
                Speed = generateRandomNum();
                Type = 0;
            }

        public void ElementsFactory()
        {

        }
            public void hitTarget(e) { }
            public abstract void Draw(Form1 g); //here I tried to pass a form inside 
            public abstract void Move();

        }
}


//example of one of the instances classes of element


C#
<pre>using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace MyMouseGame
{
    class E_Escape : Element
    {
        int move_circleX = 0;
        int move_circleY = 0;

        bool flag = false;

        Rectangle circle = new Rectangle(10, 70, 35, 35);
        Pen p = new Pen(Color.Black);

        //circle
        public E_Escape()
        {
        }
        public override void Draw(Form1 g) {
            g.DrawEllipse(p, circle);
            g.FillEllipse(new SolidBrush(Color.Red), circle);
        }

        public override void Move() {
            if (!flag)
            {
                if (circle.X >= panel1.Width - 2)
                    flag = true;
                circle.X += 10;
            }
            else
            {
                if (circle.X <= 30)
                    flag = false;
                circle.X -= 10;
            }
        }
    }
}


<pre lang="C#"><pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;




namespace MyMouseGame
{
    public partial class Form1 : Form
    {
        Graphics g;
        Element [] elements= new Element[3];

        public Form1()
        {
            this.DoubleBuffered = true;
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
    
            g = panel1.CreateGraphics();

            //timer set
            t.Interval = 100;
            t.Tick += new EventHandler(timer1_Tick);
            t.Start();

        }

        private void recThreadTracker()
        {
            //move rectangle
            while (true)//here I tried to call element finction draw
            {
                elements[0].Draw(g);
                elements[1].Draw(g);
                elements[2].Draw(g);
            }
        }

        public void threadCounter()
        {
            long counter = 0;
            while (true)
            {
                counter++;
                this.Invoke(new Action(() =>
                {
                    lcountRes.Text = counter.ToString();
                }));
                Thread.Sleep(1000);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
          
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread counterThread = new Thread(threadCounter);
            counterThread.Start();
            Thread recTrackThread = new Thread(recThreadTracker);
            recTrackThread.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Element elements in elements)
            {
                elements.Move();
            }

            //Refresh();
            Invalidate();
        }


    }
}


What I have tried:

I've tried puting a variable of type Form inside Element and didn't work.
Posted
Updated 26-Apr-21 21:57pm
Comments
Richard Deeming 27-Apr-21 4:07am
   
"because the class is abstract it can't inherit from FORM"
That makes no sense - there is nothing to stop an abstract class inheriting from a non-abstract class.

However, you might have problems with the form designer having an abstract base class.
Inheriting a Form from an Abstract Class (and Making it Work in the Designer)[^]

1 solution

If your class is intended to display something, then it should be derived from something that already knows how to draw itself: a Control. (And a Form is derived from Control, but it's a specialised type that you only need when you want it to be independent, with it's own title bar, border, control box, and so on.)

The most likely thing you want to start off with is to create a new Element class but create it as a UserControl from the VS "New item" menu.
It - and everything derived from it - will then support all events you need, including Paint events.
   
Comments
Michal 2021 27-Apr-21 4:31am
   
Ok. I did it. Now how do I connect it to the original Form?
Thanks a lot
OriginalGriff 27-Apr-21 5:03am
   
Create an instance and add it to the Form.Controls collection.
Or ... drag and drop it from the ToolBox like any other control!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900