2017-04-06 1 views
0

우리는 .txt 파일에 대학을 추가 및 제거하는 프로그램을 만들고 ComboBox에 5 개의 대학이있는 프로젝트가 있습니다.
그러나 일을 어쩌려 구`t이 오류 보여줍니다C# WinForms : 'System.IO.IOException'유형의 처리되지 않은 예외가 발생했습니다.

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

참고 : Windows Form에의합니다.

public partial class Form1 : Form 
{ 
    university[] univ = new university[10]; 
    struct university 
    { 
     public string uni; 
     public string prov; 
     public string city; 
     public string population; 
     public string programs; 
     public string tuition; 
     public string residence; 
    } 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void pictureBox4_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("If you want to check the infomation, click on the combo box and then choose the University of your choice.\n- Click the black button to add a university to the list after filling in everything. \n- Click the red button to remove a university after selecting it."); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     StreamReader sr = new StreamReader("Universities.txt"); // object 
     String line; 
     try 
     { 
      for (int i = 0; i < univ.Length; i++) 
      { 
       line = sr.ReadLine();     
       string[] sPlit = line.Split(','); 
       univ[i].uni = sPlit[0]; 
       univ[i].prov = sPlit[1]; 
       univ[i].city = sPlit[2]; 
       univ[i].population = sPlit[3]; 
       univ[i].programs = sPlit[4]; 
       univ[i].tuition = sPlit[5]; 
       univ[i].residence = sPlit[6]; 
       comboBox1.Items.Add(univ[i].uni);     
      } 
      sr.Close();     
     } 
     catch (Exception p) //catches errors 
     { 
      Console.WriteLine("Exception: " + p.Message); 
     } 
     finally    //final statement before closing 
     { 
      Console.WriteLine("Executing finally block."); 
     } 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedIndex == 0) 
     { 
      listBox1.Items.Clear(); 
      pictureBox1.Image = Properties.Resources._1; 
      listBox1.Items.Add("Province: " + univ[0].prov); 
      listBox1.Items.Add("City: " + univ[0].city); 
      listBox1.Items.Add("Population: " + univ[0].population); 
      listBox1.Items.Add("Programs: " + univ[0].programs); 
      listBox1.Items.Add("Tuition: $" + univ[0].tuition); 
      listBox1.Items.Add("Residence: $" + univ[0].residence); 
     } 
     else if (comboBox1.SelectedIndex == 1) 
     { 
      listBox1.Items.Clear(); 
      pictureBox1.Image = Properties.Resources._2; 
      listBox1.Items.Add("Province: " + univ[1].prov); 
      listBox1.Items.Add("City: " + univ[1].city); 
      listBox1.Items.Add("Population: " + univ[1].population); 
      listBox1.Items.Add("Programs: " + univ[1].programs); 
      listBox1.Items.Add("Tuition: $" + univ[1].tuition); 
      listBox1.Items.Add("Residence: $" + univ[1].residence); 
     } 
     else if (comboBox1.SelectedIndex == 2) 
     { 
      listBox1.Items.Clear(); 
      pictureBox1.Image = Properties.Resources._3; 
      listBox1.Items.Add("Province: " + univ[2].prov); 
      listBox1.Items.Add("City: " + univ[2].city); 
      listBox1.Items.Add("Population: " + univ[2].population); 
      listBox1.Items.Add("Programs: " + univ[2].programs); 
      listBox1.Items.Add("Tuition: $" + univ[2].tuition); 
      listBox1.Items.Add("Residence: $" + univ[2].residence); 
     } 
     else if (comboBox1.SelectedIndex == 3) 
     { 
      listBox1.Items.Clear(); 
      pictureBox1.Image = Properties.Resources._4; 
      listBox1.Items.Add("Province: " + univ[3].prov); 
      listBox1.Items.Add("City: " + univ[3].city); 
      listBox1.Items.Add("Population: " + univ[3].population); 
      listBox1.Items.Add("Programs: " + univ[3].programs); 
      listBox1.Items.Add("Tuition: $" + univ[3].tuition); 
      listBox1.Items.Add("Residence: $" + univ[3].residence); 
     } 
     else if (comboBox1.SelectedIndex == 4) 
     { 
      listBox1.Items.Clear(); 
      pictureBox1.Image = Properties.Resources._5; 
      listBox1.Items.Add("Province: " + univ[4].prov); 
      listBox1.Items.Add("City: " + univ[4].city); 
      listBox1.Items.Add("Population: " + univ[4].population); 
      listBox1.Items.Add("Programs: " + univ[4].programs); 
      listBox1.Items.Add("Tuition: $" + univ[4].tuition); 
      listBox1.Items.Add("Residence: $" + univ[4].residence); 
     } 
     else 
     { 
      pictureBox1.Image = Properties.Resources.noimage; 
      listBox1.Items.Clear(); 
     } 
    } 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 

    private void label9_Click(object sender, EventArgs e) 
    { 
    } 

    private void label8_Click(object sender, EventArgs e) 
    { 
    } 

    private void label7_Click(object sender, EventArgs e) 
    { 
    } 

    private void label6_Click(object sender, EventArgs e) 
    { 
    } 

    private void label5_Click(object sender, EventArgs e) 
    { 
    } 

    private void label4_Click(object sender, EventArgs e) 
    { 
    } 

    private void label3_Click(object sender, EventArgs e) 
    { 
    } 

    private void textBox8_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox7_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox6_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox3_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
    } 

    private void AddButton_Click(object sender, EventArgs e) 
    { 
     int u = 4; 
     int p = 4; 
     int c = 4; 
     int pop = 4; 
     int pro = 4; 
     int t = 4; 
     int r = 4; 
     univ[u+1].uni = textBox1.Text; 
     univ[p + 1].prov = textBox2.Text ; 
     univ[c + 1].city = textBox3.Text ; 
     univ[pop + 1].population = textBox4.Text ; 
     univ[pro + 1].programs = textBox5.Text; 
     univ[t + 1].tuition = textBox6.Text; 
     univ[r + 1].residence = textBox7.Text ; 
     StreamWriter sw = new StreamWriter("Universities.txt", true); 
     String line; 
     line = Console.ReadLine(); 

     sw.WriteLine(univ[u + 1].uni + ", " + univ[p + 1].prov + ", " + univ[c + 1].city + ", " + univ[pop + 1].population + "," + univ[pro + 1].programs 
      + ", " + univ[t + 1].tuition + "," + univ[r + 1].residence + ","); 
     MessageBox.Show("A University has been added."); 
     sw.Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("There are " + comboBox1.Items.Count + " universities."); 

    } 
} 
} 
+0

어떤 줄에 예외가 발생 않습니다
그래서 여기에 코드입니까? –

+0

아래의 업데이트 된 답변을 확인하십시오. @JustinPop –

+0

코드 서식과 제목이 개선되었습니다. – zx485

답변

0
try 
      { 
       // Create an instance of StreamReader to read from a file. 
       // The using statement also closes the StreamReader. 
       using (StreamReader sr = new StreamReader("TestFile.txt")) 
       { 
        string line; 
        // Read and display lines from the file until the end of 
        // the file is reached. 
        while ((line = sr.ReadLine()) != null) 
        { 
         //your code goes here 
       string[] sPlit = line.Split(','); 
       univ[i].uni = sPlit[0]; 
       univ[i].prov = sPlit[1]; 
       univ[i].city = sPlit[2]; 
       univ[i].population = sPlit[3]; 
       univ[i].programs = sPlit[4]; 
       univ[i].tuition = sPlit[5]; 
       univ[i].residence = sPlit[6]; 
       comboBox1.Items.Add(univ[i].uni); 
        } 
       } 
      } 
      catch (Exception p) //catches errors 
     { 
      Console.WriteLine("Exception: " + p.Message); 
     } 
     finally //final statement before closing 
     { 
      Console.WriteLine("Executing finally block."); 
     } 

Hope Helps 
+0

잘 작동하지만, 대학을 추가 할 때 콤보 상자를 다시로드하는 방법과 –

+0

감사 응답이 작동 하는지를 알려주실 수 있습니까? 받아 들였습니까? upvoted> –

+0

내가 나머지 코드를 확인하자 ok –

관련 문제