c# - Windows形成不同的类,试图改变textbox.text

标签 c# .net arrays forms class

我试图创建一个函数,从我的设置文件中获取数据(HighscoreSaved 被放入 highscoreList 数组),然后加入字符串并将它们写入文本框(highScore.Text)

但是当我调用函数时没有任何反应

所以这是我的代码: 表格1

private void button4_Click_1(object sender, EventArgs e)
    {
        Highscore.Fetch();
        Highscore.Set();
    }

    public void highscoreText (string value)
    {
            this.highScore.Text = value;
    }

这是应该由 Highscore.Fetch() 和 Highscore.Set() 调用的类 但是当我调用它们时,我的文本框中没有任何变化

public static class Highscore
    {
        public static void  Fetch()
        {
            Form1.highscoreList[0] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper1 + "\t\t" + HighscoreSaved.Default.highscoreScore1;
            Form1.highscoreList[1] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper2 + "\t\t" + HighscoreSaved.Default.highscoreScore2;
            Form1.highscoreList[2] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper3 + "\t\t" + HighscoreSaved.Default.highscoreScore3;
            Form1.highscoreList[3] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper4 + "\t\t" + HighscoreSaved.Default.highscoreScore4;
            Form1.highscoreList[4] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper5 + "\t\t" + HighscoreSaved.Default.highscoreScore5;
            Form1.highscoreList[5] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper6 + "\t\t" + HighscoreSaved.Default.highscoreScore6;
            Form1.highscoreList[6] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper7 + "\t\t" + HighscoreSaved.Default.highscoreScore7;
            Form1.highscoreList[7] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper8 + "\t\t" + HighscoreSaved.Default.highscoreScore8;
            Form1.highscoreList[8] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper9 + "\t\t" + HighscoreSaved.Default.highscoreScore9;
            Form1.highscoreList[9] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper10 + "\t\t" + HighscoreSaved.Default.highscoreScore10;

            Form1.highscoreInt[0] = HighscoreSaved.Default.highscoreScore1;
            Form1.highscoreInt[1] = HighscoreSaved.Default.highscoreScore2;
            Form1.highscoreInt[2] = HighscoreSaved.Default.highscoreScore3;
            Form1.highscoreInt[3] = HighscoreSaved.Default.highscoreScore4;
            Form1.highscoreInt[4] = HighscoreSaved.Default.highscoreScore5;
            Form1.highscoreInt[5] = HighscoreSaved.Default.highscoreScore6;
            Form1.highscoreInt[6] = HighscoreSaved.Default.highscoreScore7;
            Form1.highscoreInt[7] = HighscoreSaved.Default.highscoreScore8;
            Form1.highscoreInt[8] = HighscoreSaved.Default.highscoreScore9;
            Form1.highscoreInt[9] = HighscoreSaved.Default.highscoreScore10;

            Form1.highscoreKeeper[0] = HighscoreSaved.Default.highscoreKeeper1;
            Form1.highscoreKeeper[1] = HighscoreSaved.Default.highscoreKeeper2;
            Form1.highscoreKeeper[2] = HighscoreSaved.Default.highscoreKeeper3;
            Form1.highscoreKeeper[3] = HighscoreSaved.Default.highscoreKeeper4;
            Form1.highscoreKeeper[4] = HighscoreSaved.Default.highscoreKeeper5;
            Form1.highscoreKeeper[5] = HighscoreSaved.Default.highscoreKeeper6;
            Form1.highscoreKeeper[6] = HighscoreSaved.Default.highscoreKeeper7;
            Form1.highscoreKeeper[7] = HighscoreSaved.Default.highscoreKeeper8;
            Form1.highscoreKeeper[8] = HighscoreSaved.Default.highscoreKeeper9;
            Form1.highscoreKeeper[9] = HighscoreSaved.Default.highscoreKeeper10;                
        }
        public static void Set()
        {
            Form1 mainForm = new Form1();
            string[] highscoreImported = new string[10];
            Array.Copy(Form1.highscoreList, highscoreImported, 10);
            string highscores = string.Join("\n", highscoreImported);
            mainForm.highscoreText(highscores);
        }

最佳答案

您正在 Set 方法中创建 Form1 的新实例,这就是您看不到更改的原因。您应该将 Form 的实例传递给该方法。

类似的东西(在你的类(class) Highscore 中):

public static void Set(Form mainForm)
{
    string[] highscoreImported = new string[10];
    Array.Copy(Form1.highscoreList, highscoreImported, 10);
    string highscores = string.Join("\n", highscoreImported);
    mainForm.highscoreText(highscores);
}

以后你可以这样调用它:

private void button4_Click_1(object sender, EventArgs e)
{
    Highscore.Fetch();
    Highscore.Set(this); //notice "this"
}

关于c# - Windows形成不同的类,试图改变textbox.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356832/

相关文章:

java - 如何将 XML 转换为 MS Doc?

javascript - 我可以将数组传递给 fromCharCode 吗

c - C 是否隐式且奇怪地将数组中的这个 char 转换为 int?

c# - 用一个衬里替换c#中字符串中的多个字符

c# - ado.net 的 Microsoft Sync 框架可以与 Java 一起使用吗?

c# - Visual Studio 2017 c# 将数据导出到 Excel

c# - 如何确定 WPF 窗口是否打开?

asp.net - 开始使用 Microsoft.Net 2008/2010 的源代码控制、TDD 和 CI 的最简单工具集是什么?

c# - 在 .NET 中初始化空变量

c - 在 C 程序中传递数组作为参数并打印数组项