我在Form1中有这段代码:
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)
{
}
}
我想要发生的是,当选择列表框中的跑步者并单击“处理”按钮时,hasFinished
中的布尔ProcessRunner
将更改为true
和{{ 1}}整数增加1,然后更新Total
也增加1,但我无法使其工作。
我的两个主要问题是:我不确定lblFinished
中的代码是什么,如果ProcessRunner()
将其更改为true,hasFinished == false
将布尔值保留为原样。另一个问题是,当整数递增时,else
会相应地更新。
关于我哪里出错以及将来如何防止这种情况的任何建议都会很棒。
答案 0 :(得分:0)
你可以试试这个:
把这一行
lblFinished.Text = Convert.ToString(gate.Total);
在btnFinish_Click()
事件结束时而非开头。这样,它会在 ProcessRunner()
运行后更新。
另外,请将其添加到ProcessRunner()
课程中的Gate
个活动:
if (selectedRunner.hasFinished == false)
{
selectedRunner.hasFinished = true;
}
//You don't need to do anything if it isn't false.
我希望这对你有用。如果我需要更具体,请告诉我。