简体   繁体   English

从类访问表单控件?

[英]Accessing a form control from a class?

I have a Get Status button on my form and the code for that currently looks like this: 我的表单上有一个“ 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);
        }
    }
}
}

Now in the Runner class I have: 现在在Runner类中,我有:

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

What I'm basically trying to do is make the btnGetStatus call the GetStatus method in the Runner class and what I want that method to do is then basically check the boolean HasFinished to see if the runner has finished and if they have finished, the lblRunnerInfo.Text has a message to reflect this and if the boolean is false, then basically output a message saying "Runner has not yet finished / did not finish" 我基本上想做的是让btnGetStatusRunner类中调用GetStatus方法,然后我想要该方法做的就是基本上检查布尔值HasFinished以查看跑步者是否完成,是否已经完成, lblRunnerInfo.Text有一条消息可以反映这一点,如果布尔值是false,则基本上输出一条消息,说“ Runner尚未完成/尚未完成”。

I'm not quite sure if it's proper practice to access form controls from a class or if it even can be done, but I am not sure of how to do it the way I want (Getting the GetStatus method to check the status of the runner rather than getting the btnGetStatus to fire the code.) 我不太确定从类访问表单控件是否是正确的做法,或者甚至可以做到,但是我不确定如何按照自己的方式进行操作(获取GetStatus方法以检查控件的状态)。运行程序,而不是让btnGetStatus触发代码。)

I think what you're looking for is this: 我认为您正在寻找的是:

lblRunnerInfo.Text = selectedRunner.GetStatus();

and then in the runner class: 然后在跑步类中:

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

You can change the btnGetStatus_Click as follows: 您可以如下更改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";
    }
}

This way, the form handles the output and the Runner class is responsible for "running". 这样,表单处理输出,而Runner类负责“运行”。 If you have a more complex Status Situation later on, then you still can add an enumeration for the states and retrieve the Status instead of just checking for HasFinished. 如果以后您的状态状况更为复杂,则仍然可以为状态添加枚举并检索状态,而不仅仅是检查HasFinished。 To retrieve the text, you need to add a mapping from the Status enumeration to the appropriate text. 要检索文本,您需要添加一个从Status枚举到适当文本的映射。

Why not just access your HasFinished() property directly? 为什么不直接访问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!";
            }
        }
    }

how about something like this 这样的事情怎么样

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);
    }

}

then set your GetStatus as string 然后将您的GetStatus设置为字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM