我有点新的编程,我试图重新创建candybox(一些互动网站) 在Windows窗体应用程序中。我有一个名为糖果的变量,以及每秒向变量糖果添加一个的计时器,我试过把label.text =" candy:" + candy.ToString();在那种方法中它似乎没有用,任何人都可以提供帮助吗?
代码:
public partial class Form1 : Form
{
public int candy { get; set; }
public int lollipop { get; set; }
public Form1(int value, int lollipop)
{
this.candy = value;
this.lollipop = lollipop;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "candy: 0";
button2.Enabled = false;
button2.Visible = false;
label2.Visible = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerHandler);
timer.Start();
}
private void button1_Click(object sender, EventArgs e)
{
candy += 2;
UpdateCandy(candy, lollipop);
label2.Text = "lollipops" + lollipop.ToString();
if (candy > 9)
{
button2.Enabled = true;
button2.Visible = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (candy <= 10)
{
button2.Enabled = false;
}
candy -= 10;
lollipop += 1;
UpdateCandy(candy, lollipop);
label2.Text = "lollipops" + lollipop.ToString();
label2.Visible = true;
}
System.Timers.Timer timer = new System.Timers.Timer(1000);
private void TimerHandler(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime start;
TimeSpan elapsed = TimeSpan.MaxValue;
while (elapsed.TotalSeconds > 1.0)
{
start = DateTime.Now;
candy += 1;
elapsed = DateTime.Now - start;
}
timer.Interval = 1000 - elapsed.TotalMilliseconds;
timer.Start();
}
public void Form1_Shown(object sender, EventArgs e)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerHandler);
timer.Start();
}
private void UpdateCandy(int candy, int lollipop)
{
label1.Text = "candy: " + candy.ToString();
label2.Text = "lollipops" + lollipop.ToString();
}
}
我正在使用visual studio 2012(如果这些信息有任何帮助)