在静态上下文中更改标签的文本

时间:2014-04-11 14:20:29

标签: c# static label this

我在我的程序中创建了一个帮助部分,并且在尝试使用我拥有的编码知识时尽可能提高效率。如果我按如下方式键入Help.whichHelp(some_int_here);,我已经设置了一个方法。所有这些代码都在一个文件Help.cs中。但是标签没有变化,即使没有其他代码,但这应该改变标签。

public static void whichHelp(int index)
    {
        int allowedCount = 0;
        foreach (string namesX in HWLib.Variables.helpDir)
        {
            allowedCount++;
        }
        if (index > allowedCount)
        {
            MessageBox.Show("Index " + index + " does not exist!");
        }
        else
        {
            if (index == 0) Login();
        }
    }

这是"登录"如果index为0,则上面引用的方法。

public static void Login()
    {
        Help.getContent("Log In", "In order to use this program,  you need to be able to log in. " +
            "This is so we can track how many users we have. It also provides many user benefits to you! " +
            "\n\n To login, use the username and password provided when you signed up on the website. " +
            "if you did not signup,  you should do so by clicking the \"SignUp\" button on the login screen.");
    }

以下是获取内容的方法

static void getContent(String header, String body)
    {
        if (header == null || body == null)
        {
            MessageBox.Show("Error loading contents of program");
            Application.Exit();
        }
        else
        {
            //Since "this" isn't valid for an identifier in static context
            Help thisI = new Help();
            thisI.headerLabel.Text = header;
            thisI.bodyLabel.Text = body;
        }
    }

1 个答案:

答案 0 :(得分:0)

标签永远不会因此而改变

Help thisI = new Help();
thisI.headerLabel.Text = header;
thisI.bodyLabel.Text = body;

这些是仅存在于静态方法中的局部变量。

如何将Help对象传递给方法?

static void getContent(String header, String body, Help thisI)

这部分代码可以从

更改
int allowedCount = 0;
foreach (string namesX in HWLib.Variables.helpDir)
{
    allowedCount++;
}
if (index > allowedCount)
{
    MessageBox.Show("Index " + index + " does not exist!");
}

这样的事情

if (index > HWLib.Variables.helpDir.Count() )
{
    MessageBox.Show("Index " + index + " does not exist!");
}