0

我的 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)
    {

    }
}

What I want to happen is that when a runner in the listbox is selected and the Process button is clicked, the boolean hasFinishedin ProcessRunneris changed to trueand the Totalinteger is increased by one, which then updates the lblFinishedto also increase by one, but I can'让它工作。

我的两个主要问题是:如果将其更改为 true,我不确定代码会ProcessRunner()说什么,让布尔值保持原样。另一个问题是当整数增加时相应地更新。hasFinished == falseelselblFinished

关于我哪里出错以及将来如何防止这种情况的任何建议都会很棒。

4

1 回答 1

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.

我希望这对你有用。让我知道是否需要更具体。

于 2013-10-31T20:29:46.667 回答