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

本文关键字:Form1 参数 同一个 单独 | 更新日期: 2023-09-27 18:24:13

好的。我正在制作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();
    }
}

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

我认为这是更正确的方法

public partial class Form1 : Form
{
    SimonFunctions sf = new SimonFunctions(this); //Added
    public Form1()
    {
        InitializeComponent();
    }
    private void StartButton_Click(object sender, EventArgs e)
    {
        sf.Colorplaff(); //call Colorplaff from Form1;
    }
}
class SimonFunctions 
{
    public static List<int> ColorNums = new List<int>();
    Form1 mainForm; //added
    public SimonFunctions (Form1 form) //Constructor added
    {
        mainForm = form;
    }
    public void ColorPlay(int x) //remove static and Form1 argument
    {
        mainForm.ButtonNum = x; //use mainForm
        if (x == 1)
            mainForm.GreenButton.BackColor = Color.Lime;
        else if (x == 2)
            mainForm.RedButton.BackColor = Color.Red;
        else if (x == 3)
            mainForm.YellowButton.BackColor = Color.Yellow;
        else if (x == 4)
            mainForm.BlueButton.BackColor = Color.Blue;
        mainForm.timer1.Start();
    }
    public void Colorplaff()
    {
        ColorPlay(1);
    }
}