C#如何在同一个单独的类中填充Form1参数

时间:2015-11-24 00:55:25

标签: c# winforms

好的。我正在制作一个Simon Say的游戏,我正在努力让按钮闪烁。我有一个额外的类,它有一个参数为Form1的函数,我可以在其中访问我的Form1按钮。但是,我不知道如何在同一个类中填充参数。如果你看一下代码,你就会理解我的意思。

这是我的单独类代码:

class SimonFunctions : Form1
{

    public static List<int> ColorNums = new List<int>();

    public static void ColorPlay(Form1 form, int x)
    {
        form.ButtonNum = x;
        if (x == 1)
            form.GreenButton.BackColor = Color.Lime;
        else if (x == 2)
            form.RedButton.BackColor = Color.Red;
        else if (x == 3)
            form.YellowButton.BackColor = Color.Yellow;
        else if (x == 4)
            form.BlueButton.BackColor = Color.Blue;
        form.timer1.Start();
    }

    public static void Colorplaff()
    {
        ColorPlay(HOW AM I GOING TO SPECIFY THIS, 1);
    }

}

这是我的Form1代码:

public partial class Form1 : Form
{
    public int ButtonNum = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void StartButton_Click(object sender, EventArgs e)
    {
        SimonFunctions.Colorplaff();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (ButtonNum == 1)
        {
            GreenButton.BackColor = Color.FromArgb(255, 113, 215, 128);
            timer1.Stop();
        }
        else if (ButtonNum == 2)
        {
            RedButton.BackColor = Color.FromArgb(255, 234, 83, 87);
            timer1.Stop();
        }
        else if (ButtonNum == 3)
        {
            YellowButton.BackColor = Color.FromArgb(255, 255, 255, 128);
            timer1.Stop();
        }
        else if (ButtonNum == 4)
        {
            BlueButton.BackColor = Color.FromArgb(255, 125, 150, 206);
            timer1.Stop();
        }
    }

    private void GreenButton_Click(object sender, EventArgs e)
    {
        GreenButton.BackColor = Color.Lime;
        ButtonNum = 1;
        timer1.Start();
    }

    private void RedButton_Click(object sender, EventArgs e)
    {
        RedButton.BackColor = Color.Red;
        ButtonNum = 2;
        timer1.Start();
    }

    private void YellowButton_Click(object sender, EventArgs e)
    {
        YellowButton.BackColor = Color.Yellow;
        ButtonNum = 3;
        timer1.Start();
    }

    private void BlueButton_Click(object sender, EventArgs e)
    {
        BlueButton.BackColor = Color.Blue;
        ButtonNum = 4;
        timer1.Start();
    }
}

0 个答案:

没有答案