2012-02-22 52 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]; 

     } 
    } 

} 
相关问题