簡體   English   中英

用另一種形式調用方法C#

[英]Calling a Method in another Form C#

我知道這是一個非常常見的話題,我一直在尋找解決方案。 我再次在CHIP8模擬器上工作,我正在嘗試創建一個單獨的表單來處理圖形。

我現在有兩種形式,Form1和圖形。 我想做的是在Form1的圖形中調用“draw”方法。 在Form1中,我有以下代碼......

這是我得到的錯誤:錯誤4'System.Windows.Forms.Form'不包含'Draw'的定義,也沒有擴展方法'Draw'接受類型'System.Windows.Forms.Form的第一個參數'可以找到(你錯過了使用指令或程序集引用嗎?)

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作為類名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM