Bubo
Bubo

Reputation: 818

Calling a Method in another Form C#

I know that this a very common topic and I have been searching for some time for a solution. Again I am working on a CHIP8 emulator and I am trying to create a separate form to process the graphics.

I now have two forms, Form1 and graphics. What I would like to do is call the "draw" method in graphics from Form1. In Form1 I have the following code...

This is the error that I am getting: Error 4 'System.Windows.Forms.Form' does not contain a definition for 'Draw' and no extension method 'Draw' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)

Form graphicsTest = new graphics(); // This is in the initial declaration of Form1
graphicsTest.Draw(screen); // Screen is a [64,32] boolean array representing pixel states

Here is what I have for "graphics"...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class graphics : Form // The purpose of this form is to handle the graphics
{
    Graphics dc;
    Rectangle ee = new Rectangle(10, 10, 30, 30); // Size of each pixel
    Pen WhitePen = new Pen(Color.White, 10); // Pen to draw the rectangle object
    Pen BlackPen = new Pen(Color.Black, 10);
    bool[] drawMe;

    public graphics()
    {
        InitializeComponent();
        dc = this.CreateGraphics();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    public void Draw(bool[] drawme) // This method recieves the array and draws the appropriate Sprites!
    {
        // This is where I will draw the appropriate pixels...   
    }
}
}

Again, I may be missing something simple, This is all relatively new to me and I apologize if I have not posted enough information. Any input is appreciated!

Upvotes: 1

Views: 968

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Change type of variable to graphics instead of Form

graphics graphicsTest = new graphics(); 
graphicsTest.Draw(screen);

Otherwise only base class members will be available to you.

BTW use PascalCase for class names in C#.

Upvotes: 3

Related Questions