2

我的表单上有一个Get Status按钮,当前的代码如下所示:

private void btnGetStatus_Click(object sender, EventArgs e)
    {
        // Check if a runner has been selected
        if (lstRunners.SelectedIndex > -1)
        {
            // Obtain selected runner
            Runner selectedRunner = (Runner)lstRunners.SelectedItem;

            // Call the method in Runner class to get the runner's status
            selectedRunner.GetStatus(selectedRunner);
        }
    }
}
}

现在在Runner课堂上我有:

public void GetStatus(Runner selectedRunner)
    {
        if (selectedRunner.HasFinished == true)
        {
            lblRunnerInfo.Text = "Runner has already finished!";
        }
    }

我基本上想做的是btnGetStatus调用类中的GetStatus方法,Runner然后我想要该方法做的基本上是检查布尔值HasFinished以查看跑步者是否已完成,如果他们已完成,则lblRunnerInfo.Text有一条消息要反映如果布尔值为假,则基本上输出一条消息“跑步者尚未完成/未完成”

我不太确定从类访问表单控件是否是正确的做法,或者是否可以完成,但我不确定如何按照我想要的方式进行操作(获取GetStatus方法来检查跑步者的状态而不是让btnGetStatus触发代码。)

4

4 回答 4

0

像这样的东西怎么样

private void btnGetStatus_Click(object sender, EventArgs e)
{
    // Check if a runner has been selected
    if (lstRunners.SelectedIndex > -1)
    {
        // Obtain selected runner
        Runner selectedRunner = (Runner)lstRunners.SelectedItem;

        // Call the method in Runner class to get the runner's status
        lblRunnerInfo.Text = selectedRunner.GetStatus(selectedRunner);
    }

}

然后将您的 GetStatus 设置为字符串

public string GetStatus(Runner selectedRunner)
{
    if (selectedRunner.HasFinished == true)
    {
        return "Runner has already finished!";
    }
}
于 2013-12-06T02:06:30.170 回答
0

我认为您正在寻找的是:

lblRunnerInfo.Text = selectedRunner.GetStatus();

然后在跑步者类中:

public string GetStatus()
{
    if (this.HasFinished == true)
    {
        return "Runner has already finished!";
    }
    return "Finished";
}
于 2013-11-01T20:21:49.240 回答
0

您可以按如下方式更改 btnGetStatus_Click:

private void btnGetStatus_Click(object sender, EventArgs e)
{
    // Check if a runner has been selected
    if (lstRunners.SelectedIndex > -1)
    {
        // Obtain selected runner
        Runner selectedRunner = (Runner)lstRunners.SelectedItem;
        // Call the method in Runner class to get the runner's Status
        // CHANGED
        if (selectedRunner.HasFinished)
            lblRunnerInfo.Text = "Runner has already finished";
    }
}

这样,表单处理输出,而 Runner 类负责“运行”。如果稍后您有更复杂的状态情况,那么您仍然可以为状态添加枚举并检索状态,而不仅仅是检查 HasFinished。要检索文本,您需要添加从 Status 枚举到适当文本的映射。

于 2013-11-01T20:26:12.157 回答
0

为什么不直接访问您的 HasFinished() 属性?

    private void btnGetStatus_Click(object sender, EventArgs e)
    {
        // Check if a runner has been selected
        if (lstRunners.SelectedIndex > -1)
        {
            // Obtain selected runner
            Runner selectedRunner = (Runner)lstRunners.SelectedItem;

            // Call the method in Runner class to get the runner's status
            if (selectedRunner.HasFinished)
            {
                lblRunnerInfo.Text = "Runner has already finished!";
            }
            else
            {
                lblRunnerInfo.Text = "Runner has NOT finished yet!";
            }
        }
    }
于 2013-11-01T20:26:38.433 回答