C#WinForms:类型为'的未处理异常;System.IO.IOException';发生
我们有一个项目要做一个程序来添加和删除大学,在我们的C#WinForms:类型为'的未处理异常;System.IO.IOException';发生,c#,winforms,C#,Winforms,我们有一个项目要做一个程序来添加和删除大学,在我们的.txt文件中有五所大学在我们的组合框中显示它们 但它不起作用,并显示出以下错误: mscorlib.dll中发生类型为“System.IO.IOException”的未处理异常 注意:这是一个Windows窗体。 下面是代码: public partial class Form1 : Form { university[] univ = new university[10]; struct university {
.txt
文件中有五所大学在我们的组合框中显示它们
但它不起作用,并显示出以下错误:
mscorlib.dll中发生类型为“System.IO.IOException”的未处理异常
注意:这是一个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.");
}
}
}
公共部分类表单1:表单
{
大学[]大学=新大学[10];
结构大学
{
公共字符串uni;
公共字符串prov;
公共字符串城市;
公共字符串人口;
公共字符串程序;
公立学校学费;
公共住宅;
}
公共表格1()
{
初始化组件();
}
私有无效图片bx4_单击(对象发送方,事件参数e)
{
显示“(如果你想检查信息,点击组合框,然后选择你选择的大学。\n点击黑按钮,在填好所有东西后将一个大学添加到列表中。\n-点击红色按钮,在选择它之后删除一个大学。”
}
私有void Form1\u加载(对象发送方、事件参数e)
{
StreamReader sr=newstreamreader(“universions.txt”);//对象
弦线;
尝试
{
对于(int i=0;i 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