C# 从另一个类递增整数
我在表格1中有以下代码:C# 从另一个类递增整数,c#,winforms,C#,Winforms,我在表格1中有以下代码: public partial class Form1 : Form { public static int hours; public static int minutes; public static int seconds; FinishGate finishgate = new FinishGate(); public Form1() { InitializeComponent();
public partial class Form1 : Form
{
public static int hours;
public static int minutes;
public static int seconds;
FinishGate finishgate = new FinishGate();
public Form1()
{
InitializeComponent();
txtHours.MaxLength = 2;
txtMinutes.MaxLength = 2;
txtSeconds.MaxLength = 2;
lblFinished.Text = Convert.ToString(gate.Total);
}
private void btnFinish_Click(object sender, EventArgs e)
{
hours = Convert.ToInt32(txtHours.Text);
minutes = Convert.ToInt32(txtMinutes.Text);
seconds = Convert.ToInt32(txtSeconds.Text);
lblFinished.Text = Convert.ToString(gate.Total);
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// If runner hasn't finished
if (selectedRunner.HasFinished == false)
{
// Call the method in FinishGate class to process the runner
FinishGate.ProcessRunner(selectedRunner);
}
else
{
// Runner has finished / been processed so increase the total that have completed the climb by one
finishgate.Total++;
}
}
}
}
以下是FinishGate.cs:
class Gate
{
private int total;
public int Total
{
get { return total; }
set { total = value; }
}
public static void ProcessRunner(Runner selectedRunner)
{
}
}
我想做的是,当选择列表框中的一个运行程序并单击Process按钮时,ProcessRunner
中的布尔值hasFinished
被更改为true
,并且Total
整数增加1,然后更新lblFinished
也增加1,但我不能让它工作
我的两个主要问题是:我不确定ProcessRunner()
中的代码是什么,如果hasFinished==false
将其更改为true,else
保持布尔值不变。另一个问题是当整数递增时,使lblFinished
相应地更新
任何关于我哪里出了问题以及如何防止将来出现这种情况的建议都将非常有用。您可以尝试以下方法:
放这条线
lblFinished.Text=Convert.ToString(gate.Total)代码>
在btnFinish_事件的末尾单击()
而不是开始。这样,它将在运行ProcessRunner()
后更新
另外,将其添加到Gate
类中的ProcessRunner()
事件中:
if (selectedRunner.hasFinished == false)
{
selectedRunner.hasFinished = true;
}
//You don't need to do anything if it isn't false.
我希望这对你有用。如果我需要更具体一些,请告诉我