我知道这是一个非常常见的话题,我一直在寻找解决方案。我再次在 CHIP8 模拟器上工作,我正在尝试创建一个单独的表单来处理图形。
我现在有两个表单,Form1 和图形。我想做的是在 Form1 的图形中调用“draw”方法。在 Form1 中,我有以下代码...
这是我得到的错误: 错误 4 'System.Windows.Forms.Form' 不包含 'Draw' 的定义,并且找不到接受类型为 'System.Windows.Forms.Form' 的第一个参数的扩展方法 'Draw'(您是否缺少使用指令或程序集引用?)
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
这是我的“图形”……
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...
}
}
}
同样,我可能遗漏了一些简单的东西,这对我来说都是相对较新的,如果我没有发布足够的信息,我深表歉意。感谢任何输入!
最佳答案
将变量类型更改为 graphics
而不是 Form
graphics graphicsTest = new graphics();
graphicsTest.Draw(screen);
否则只有基类成员对您可用。
BTW 在 C# 中使用 PascalCase 作为类名。
关于c# - 在另一个窗体中调用方法 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13442891/