2012-02-22 77 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]的值,[1],[2]? – 2012-02-22 16:42:37

+0

請提供Data.txt文件的內容 – 2012-02-22 16:45:56

+0

red,small,texttest – Bya413 2012-02-24 17:20:20

回答

0

fields:可能是空的,文件的結尾? 任何文件打開錯誤?試圖在它已經在後臺打開時再次打開它?

0

更多的信息可能會對您有所幫助。像其他人所說的那樣,字段[0] - [2]的值是什麼?你收到什麼錯誤?它看起來像我會像一個錯誤,你會收到的是,你的索引超出了字段數組的界限...給我們一些更多的信息,我們可以更好地幫助你。

0

嘗試這樣

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]; 

     } 
    } 

} 
相關問題