gpt4 book ai didi

c# - 从类访问表单控件?

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:18 24 4
gpt4 key购买 nike

我的表单上有一个 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 调用 Runner 类中的 GetStatus 方法以及我想要的方法然后要做的基本上是检查 bool 值 HasFinished 以查看运行者是否已经完成,如果他们已经完成,lblRunnerInfo.Text 有一条消息来反射(reflect)这一点,如果 bool 值为false,那么基本上输出一条信息说“Runner has not yet yet finished/did not finish”

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

最佳答案

我想你要找的是这个:

lblRunnerInfo.Text = selectedRunner.GetStatus();

然后在 runner 类中:

public string GetStatus()
{
if (this.HasFinished == true)
{
return "Runner has already finished!";
}
return "Finished";
}

关于c# - 从类访问表单控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com