C# 使用委托加载表单中的类初始化

C# 使用委托加载表单中的类初始化,c#,forms,delegates,initialization,C#,Forms,Delegates,Initialization,我只想初始化Form1()中的类测试! 如果我用下面的代码启动程序,则调用类测试中的构造函数测试 类测试中的测试是一个反映回form1 callbackFunction的函数。 但我想在Timer1中调用它,而不是在表单/程序开始上 有什么想法吗 谢谢 表格1代码: namespace Klasse { public partial class Form1 : Form { public System.Windows.Forms.Timer timer1;

我只想初始化Form1()中的类测试! 如果我用下面的代码启动程序,则调用类测试中的构造函数测试

类测试中的测试是一个反映回form1 callbackFunction的函数。 但我想在Timer1中调用它,而不是在表单/程序开始上

有什么想法吗

谢谢

表格1代码:

namespace Klasse
{
    public partial class Form1 : Form
    {

        public System.Windows.Forms.Timer timer1;
        private Button[] button;
        Random rand = new Random();
        int getroffen = 0;

        public Form1()
        {

            Test Object = new Test(rand,callbackFunction);

            InitializeComponent();
            button = new Button[5];
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(Timer1_Tick);
            timer1.Interval = rand.Next(500, 1001);
            timer1.Start();         
            button[0] = button1;
            button[1] = button2;
            button[2] = button3;
            button[3] = button4;
            button[4] = button5;
        }

        void Timer1_Tick(object sender,EventArgs e)
        {
            timer1.Stop();   
        }

        void button_Click(object sender, EventArgs e)
        {
           getroffen = Convert.ToInt16(((Button)sender).Name);    
        }

        void callbackFunction(int buttonNr, bool aktiv)
        {
            if (aktiv == true)
            {
                button[buttonNr-1].BackColor = Color.Red;

            }
            if (aktiv == false)
            {
                button[buttonNr-1].BackColor = SystemColors.Control;
            }
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(Timer1_Tick);
            timer1.Interval = rand.Next(500, 1001);
            timer1.Start();
        }
    }
}
类别代码:

namespace Klasse
{


    class Test

    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }

        public delegate void ButtonAktivieren(int button ,bool passiv);

        private int anzahlGezeigt = 0;

        private int anzahlTreffer = 0;

        private int button = 0;

        private bool passiv;
        public bool Passiv
        {
            get { return passiv; }
        }

        private Random zufall;

        private ButtonAktivieren aktivierenCallback;

        public Test (Random zufall, ButtonAktivieren aktivierenCallback)
        {
            anzahlGezeigt++;
            if (anzahlGezeigt % 2 == 0)
            {
                aktivierenCallback(button, passiv);
            }
            if (anzahlGezeigt %2 > 0)
            {
            button = zufall.Next(1, 6);
            aktivierenCallback(button, passiv);    
            }

        }

        public void Aktivieren()
        {
            passiv = true;
        }

        public void Passivieren()
        {
            passiv = false;
        }

        private void CheckGameOver()
        {
            if (anzahlGezeigt > anzahlTreffer)
            {
                MessageBox.Show("Game Over!");
                Application.Exit();
            }
        }

        public void Getroffen(int getroffenerButton)
        {
            if (getroffenerButton == button)
            {
                anzahlTreffer++;
            }
        }
    }
}
名称空间Klasse
{
课堂测试
{
/// 
///这是一个很好的例子。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
公共代表无效按钮(int按钮,bool被动按钮);
私有内部anzahlGezeigt=0;
私有内部ANZAHLTREFER=0;
私有int按钮=0;
私有布尔被动式;
公共图书馆被动式
{
获取{return passive;}
}
私人随机落差;
私用钮扣扣扣;
公共测试(随机zufall、Buttonnaktivieren aktivierenCallback)
{
Anzahlgezigt++;
如果(anzahlGezeigt%2==0)
{
aktivierenCallback(按钮,被动);
}
如果(anzahlGezeigt%2>0)
{
按钮=zufall.Next(1,6);
aktivierenCallback(按钮,被动);
}
}
公共无效Aktivieren()
{
被动=真;
}
公共空间被动者()
{
被动=错误;
}
私有void CheckGameOver()
{
如果(anzahlGezeigt>ANZAHLTREFER)
{
MessageBox.Show(“游戏结束!”);
Application.Exit();
}
}
公共无效Getroffen(内部getroffenerButton)
{
如果(getroffenerButton==按钮)
{
anzahlTreffer++;
}
}
}
}

使
测试
方法可调用,而不是构造函数,如下面的
开始测试

    public void StartTest (Random zufall, ButtonAktivieren aktivierenCallback)
    {
        anzahlGezeigt++;
        if (anzahlGezeigt % 2 == 0)
        {
            aktivierenCallback(button, passiv);
        }
        if (anzahlGezeigt %2 > 0)
        {
        button = zufall.Next(1, 6);
        aktivierenCallback(button, passiv);    
        }

    }

    public void Aktivieren(Random zufall, ButtonAktivieren aktivierenCallback)

    {
        passiv = true;
        StartTest(Random zufall, ButtonAktivieren aktivierenCallback);
    }

表格代码调整:

    Test Object;

    public Form1()
    {
        //some initializing code

        Object = new Test();

        //the rest of initializing code
    }




    void Timer1_Tick(object sender,EventArgs e)
    {
        Object.StartTest(rand, callbackFunction)
        timer1.Stop();   
    }

或者另一种方法:将
随机zufall
按钮Naktivieren-aktivierenCallback
存储在构造函数中的
测试类中,并在激活时使用它们:

    Random _zufall;
    ButtonAktivieren _aktivierenCallback;


    public void Test (Random zufall, ButtonAktivieren aktivierenCallback)
    {
        _zufall = zufall;
        _aktivierenCallback = aktivierenCallback;
    }


    public void Aktivieren()
    {
        passiv = true;

        anzahlGezeigt++;
        if (anzahlGezeigt % 2 == 0)
        {
            _aktivierenCallback(button, passiv);
        }
        if (anzahlGezeigt %2 > 0)
        {
        button = _zufall.Next(1, 6);
        _aktivierenCallback(button, passiv);    
        }
    }

你想什么时候开始计时?您可以将计时器设置为1.Start();在他自己的方法中。计时器应该在窗体加载时启动,计时器1\u Tick应该调用callbackFunction…只要让Tick事件处理程序调用该方法即可。你需要把注意力集中在你要让定时器激活的按钮上。这个问题并不清楚,但通常是“最后一次点击”按钮。因此,您需要另一个变量来存储上次单击的按钮,以便计时器可以再次单击它。