2012-11-13 4 views
5

私の設定ファイル(HighScoreSavedをhighscoreList配列に入れたもの)からデータを取り出し、その文字列を結合してテキストボックスに書き込む関数を作成しようとしています(highScore Form1のWindowsは異なるクラスを作成してtextbox.textを変更しようとしています

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

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

そして相続人Highscore.Fetch(によって呼び出されることになったクラスのthats):私は、関数を呼び出すときの.text)

は、しかし、何も私のコードであるので、ここで

を起こりません、Highscore.Set( ) が、私はそれらを呼び出すとき何もあなたは変更が表示されない理由は、あなたのSet方法でForm1の新しいインスタンスを作成している私のテキストボックスに

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

答えて

8

を変更しません。代わりに、フォームのインスタンスをメソッドに渡す必要があります。

何か(あなたのクラス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" 
} 
+2

これは動作していたようです。ありがとうございます –

1

より良いアプローチは、一度にそれらのすべてを行うことであろう。

using System.Linq; 

public static void FetchAndSet() 
{ 
    Form1.highscoreList[0] = "\t\t\t" + HighscoreSaved.Default.highscoreKeeper1; 
    . 
    . 

    Form1.highscoreInt[0] = HighscoreSaved.Default.highscoreScore1; 
    . 
    . 

    Form1.highscoreKeeper[0] = HighscoreSaved.Default.highscoreKeeper1; 
    . 
    . 

    //Use LINQ's Take to Pull out the Top 10. 
    string highscores = string.Join("\n", highscoreList.Take(10)); 
    this.highscoreText(highscores);  
} 
関連する問題