我們有一個項目來執行一個程序來添加和刪除大學,並在我們的.txt
文件中有五所大學在我們的ComboBox
中顯示它們。
但它不`噸的工作,並顯示此錯誤:C#WinForms:出現'System.IO.IOException'類型的未處理異常
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
注:這是一個Windows窗體。
因此,這裏的代碼:
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.");
}
}
}
不例外發生在哪一行? –
查看下面我更新的答案。 @JustinPop –
我改進了代碼格式和標題。 – zx485