0
Form1() 클래스 만 초기화하면됩니다! 아래 코드로 프로그램을 시작하면 클래스 테스트에서 생성자 테스트가 호출됩니다.델리게이트로 폼로드시 클래스 초기화
시험 테스트는 form1 callbackFunction을 반영하는 기능입니다. 하지만 Form/Program 시작시 아닌 Timer1_Tick에서 호출하고 싶습니다!
아이디어가 있으십니까?
감사합니다.
양식 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++;
}
}
}
}
타이머를 시작 하시겠습니까? 당신은 timer1.Start();를 넣을 수 있습니다. 그 자신의 방법으로. – Smartis
타이머는 폼로드에서 시작해야하고 Timer1_Tick은 callbackFunction ... –
을 호출해야합니다. Tick 이벤트 핸들러에서 메소드를 호출하게하십시오. 타이머를 활성화 시키려면 * 어떤 버튼에 정확히 초점을 맞춰야합니다. 질문에서 명확하지 않지만 "마지막 버튼을 클릭"하는 것이 일반적입니다. 따라서 타이머가 다시 클릭 할 수 있도록 마지막으로 클릭 한 버튼을 저장하는 또 다른 변수가 필요합니다. –