2012-02-22 4 views
0

ここに何が問題なのかよく分かりません。私のプログラムは「テキスト」を読み込んで問題なくタイトルに入れることができますが、クラッシュしたり、サイズや色を変更したりすることはありません。私はクラスメートにこのことについて助けを求めましたが、すべてのコードが正しいと言います。テキストファイルからリコールしようとするとGUIプログラムがクラッシュする

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace RetrieveCustomizedForm 
{ 
public partial class Form1 : Form 
{ 
    const char DELIM = ','; 
    string recordIn; 
    string[] fields; 


    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     const string FILENAME = "C:\\Exercise5\\Data.txt"; 
     stuff stuff1 = new stuff(); 
     FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read); 
     StreamReader reader = new StreamReader(inFile); 
     recordIn = reader.ReadLine(); 
     reader.Close(); 
     inFile.Close(); 



     while (recordIn != null) 
     { 
      fields = recordIn.Split(DELIM); 
      stuff1.color = fields[0]; 
      stuff1.size = fields[1]; 
      stuff1.text = fields[2]; 

      if (fields[0] == "red") 
      { 
       this.BackColor = System.Drawing.Color.Red; 
      } 
      if (fields[0] == "blue") 
      { 
       this.BackColor = System.Drawing.Color.Blue; 
      } 
      if (fields[0] == "yellow") 
      { 
       this.BackColor = System.Drawing.Color.Yellow; 
      } 
      if (fields[1] == "large") 
      { 
       this.Size = new System.Drawing.Size(500, 500); 
      } 
      if (fields[1] == "small") 
      { 
       this.Size = new System.Drawing.Size(300, 300); 
      } 
      this.Text = fields[2]; 

     } 


    } 

    class stuff 
    { 
     public string color { get; set; } 
     public string size { get; set; } 
     public string text { get; set; } 
    } 
} 
} 
+0

、それを試してみてください? –

+0

Data.txtファイルの内容を入力してください –

+0

赤、小、texttest – Bya413

答えて

0

フィールド:ファイルの末尾が空である可能性がありますか? ファイルオープンエラー?バックグラウンドで既に開かれている間にもう一度開こうとしていますか?

0

さらに詳しい情報が参考になる場合があります。他の人が言ったように、フィールド[0] - [2]の値は何ですか?あなたはどんなエラーを受けていますか?あなたが受け取るエラーは、あなたのインデックスがfields配列の境界の外にあるということです。私たちにいくつかの情報を与えてください。私たちはあなたを助けることができます。

0

は、フィールドの値は、[0] [1]、及び[2]何このよう

using (FileStream infile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read)) 
{ 
    using (StreamReader reader = new StreamReader(infile)) 
    { 
     string recordIn = reader.ReadLine(); 
     while (recordIn != null) 
     { 
      fields = recordIn.Split(DELIM); 
      stuff1.color = fields[0]; 
      stuff1.size = fields[1]; 
      stuff1.text = fields[2]; 

      if (fields[0] == "red") 
      { 
       this.BackColor = System.Drawing.Color.Red; 
      } 
      if (fields[0] == "blue") 
      { 
       this.BackColor = System.Drawing.Color.Blue; 
      } 
      if (fields[0] == "yellow") 
      { 
       this.BackColor = System.Drawing.Color.Yellow; 
      } 
      if (fields[1] == "large") 
      { 
       this.Size = new System.Drawing.Size(500, 500); 
      } 
      if (fields[1] == "small") 
      { 
       this.Size = new System.Drawing.Size(300, 300); 
      } 
      this.Text = fields[2]; 

     } 
    } 

} 
関連する問題