C#实验综合设计.docx

上传人:b****7 文档编号:11240584 上传时间:2023-02-26 格式:DOCX 页数:29 大小:1.24MB
下载 相关 举报
C#实验综合设计.docx_第1页
第1页 / 共29页
C#实验综合设计.docx_第2页
第2页 / 共29页
C#实验综合设计.docx_第3页
第3页 / 共29页
C#实验综合设计.docx_第4页
第4页 / 共29页
C#实验综合设计.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

C#实验综合设计.docx

《C#实验综合设计.docx》由会员分享,可在线阅读,更多相关《C#实验综合设计.docx(29页珍藏版)》请在冰豆网上搜索。

C#实验综合设计.docx

C#实验综合设计

 

贵州大学计算机科学与信息学院

 

《C#》综合设计

题目

姓名

学号

班级

完成时间

综合设计

学院:

计算机科学与信息工程学院专业:

网络工程班级:

网络091

姓名

学号

设计组

设计时间

2012-6-10

指导教师

成绩

设计项目名称

综合设计

设计目的

1.      了解文件流的概念。

2.      学会文本文件的读写。

3.      学会序列化及二进制随机文件的读写。

设计内容

1.设计如下窗体,A.要求用户点击添加按钮时将当前学生信息添加到指定文本文件中(如源文件中有内容,不覆盖)。

B.输入时,要求采用异常处理,针对输入学号、姓名为空,输入年龄、分数小于0的情况,弹出相应的出错对话框进行提示。

C.点击显示按钮,将当前文件中内容显示出来。

(提示:

异常处理时要自定义异常,具体方法见参考程序)

2.A.封装一个学生类对象,将以上程序学生信息采用序列化方式写入二进制文件,并可输入学号随机查找相应记录信息显示在文本框中,查找记录时,也要求用异常处理处理输入学号为空的情况。

设计环境

Pc机

Visualstudio2005

设计步骤

1.编写程序

2.运行程序

3.得出实验结果

设计程序

1.

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Windows.Forms;

namespaceStudentLast

{

staticclassProgram

{

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.IO;

namespaceStudentLast

{

publicpartialclassForm1:

Form

{

stringpath=@"G:

\C#实验\StudentLast\StudentLast\StudentLast.txt";

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

stringtxt=textBox1.Text+""+textBox2.Text+""+textBox3.Text+""+textBox4.Text+""+textBox5.Text+"\r\n";

File.AppendAllText(path,txt);

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

stringmystr="";

FileStreamfs=File.OpenRead(path);

StreamReadersr=newStreamReader(fs);

while(sr.Peek()>-1)

mystr=mystr+sr.ReadLine()+"\r\n";

sr.Close();

fs.Close();

richTextBox1.Text=mystr;

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

if(textBox1.Text=="")

MessageBox.Show("学号为空,请重新输入!

");

}

privatevoidtextBox2_TextChanged(objectsender,EventArgse)

{

if(textBox2.Text=="")

MessageBox.Show("姓名为空,请重新输入!

");

}

privatevoidtextBox3_TextChanged(objectsender,EventArgse)

{

if(textBox3.Text!

="男"&&textBox3.Text!

="女")

MessageBox.Show("性别输入有误,请重新输入!

");

}

privatevoidtextBox4_TextChanged(objectsender,EventArgse)

{

intage=Convert.ToInt32(textBox4.Text);

if(age<1)

{

MessageBox.Show("年龄输入有误,请重新输入!

");

}

}

privatevoidtextBox5_TextChanged(objectsender,EventArgse)

{

intscore=0;

if(textBox5.Text!

="")

{

if(textBox5.Text!

="-")

{

score=Convert.ToInt32(textBox5.Text);

}

}

if(score<0||score>100)

{

MessageBox.Show("分数输入有误,请重新输入!

");

}

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

}

}

}

2.

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Windows.Forms;

namespacezhsj

{

staticclassProgram

{

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

 

namespacezhsj

{

classNULL1:

Exception{}

classNULL2:

Exception{}

classNOT1:

Exception{}

classNOT2:

Exception{}

classSN:

Exception{}

classNULL3:

Exception{}

classyccl

{

publicstaticboolyc(stringsa,stringsb,stringsc,stringsd)

{

if(sa=="")

thrownewNULL1();

elseif(sb=="")

thrownewNULL2();

elseif(Convert.ToInt32(sc)<0)

thrownewNOT1();

elseif(Convert.ToDouble(sd)<0||Convert.ToDouble(sd)>100)

thrownewNOT2();

else

returntrue;

}

publicstaticboolyd(stringse)

{

if(se=="")

thrownewNULL3();

else

returntrue;

}

}

}

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespacezhsj

{

[Serializable]

classstud

{

publicintsno,sage;

publicstringsname,sex;

publicdoublescore;

publicstud(){}

publicstud(intpsno,stringpsname,stringpsex,intpsage,doublepscore)

{

sno=psno;

sname=psname;

sex=psex;

sage=psage;

score=pscore;

}

publicintno

{

get{returnsno;}

set{sno=value;}

}

publicstringname

{

get{returnsname;}

set{sname=value;}

}

publicstringssex

{

get{returnsex;}

set{sex=value;}

}

publicintage

{

get{returnsage;}

set{sage=value;;}

}

publicdoublesscore

{

get{returnscore;}

set{score=value;}

}

}

}

usingSystem;

usingSystem.Collections;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Runtime.Serialization;

usingSystem.Runtime.Serialization.Formatters.Binary;

usingSystem.IO;

namespacezhsj

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

ArrayListst=newArrayList();

privatevoidForm1_Load(objectsender,EventArgse)

{

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

try

{

yccl.yc(textBox1.Text,textBox2.Text,textBox4.Text,textBox5.Text);

studs=newstud(Convert.ToInt32(textBox1.Text),textBox2.Text,textBox3.Text,Convert.ToInt32(textBox4.Text),Convert.ToDouble(textBox5.Text));

st.Add(s);

FileStreamf=newFileStream(@"I:

\C#\student.dat",FileMode.Create);

BinaryFormatterb=newBinaryFormatter();

b.Serialize(f,st);

f.Close();

MessageBox.Show("学生信息添加成功!

");

if(!

comboBox1.Items.Contains(textBox1.Text)&&!

comboBox1.Items.Contains(s.no))

comboBox1.Items.Add(textBox1.Text);

}

catch(NULL1)

{

MessageBox.Show("请输入的学号不能为空!

");

}

catch(NULL2)

{

MessageBox.Show("请输入的姓名不能为空!

");

}

catch(NOT1)

{

MessageBox.Show("请输入的年龄不能小于0!

");

}

catch(NOT2)

{

MessageBox.Show("请输入的分数不能小于0或大于100!

");

}

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

try

{

if(!

File.Exists(@"I:

\C#\student.dat"))

{

MessageBox.Show("无学生信息记录,请先添加!

");

}

else

{

FileStreamf=newFileStream(@"I:

\C#\student.dat",FileMode.Open);

BinaryFormatterb=newBinaryFormatter();

st.Clear();

st=(ArrayList)b.Deserialize(f);

f.Close();

textBox6.Text="学号\t姓名\t性别\t年龄\t分数"+"\r\n";

foreach(studs1inst)

{

textBox6.Text=textBox6.Text+

string.Format("{0}\t{1}\t{2}\t{3}\t{4}",s1.no,s1.name,s1.ssex,s1.age,s1.sscore)+"\r\n";

if(!

comboBox1.Items.Contains(s1.no)&&!

comboBox1.Items.Contains(textBox1.Text))

comboBox1.Items.Add(s1.no);

}

}

}

catch(Exceptionex)

{

MessageBox.Show(ex.Message);

}

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

try

{

yccl.yd(comboBox1.Text);

ArrayListstu=newArrayList();

studs2=newstud();

FileStreamf=newFileStream(@"I:

\C#\student.dat",FileMode.Open);

BinaryFormatterb=newBinaryFormatter();

stu.Clear();

stu=(ArrayList)b.Deserialize(f);

f.Close();

textBox6.Text="学号\t姓名\t性别\t年龄\t分数"+"\r\n";

foreach(studs6instu)

{

if(s6.no==Convert.ToInt32(comboBox1.Text))

{

textBox6.Text=

textBox6.Text+string.Format("{0}\t{1}\t{2}\t{3}\t{4}",s6.no,s6.name,s6.ssex,s6.age,s6.sscore)+"\r\n";

break;

}

}

}

catch(NULL3)

{

MessageBox.Show("学号为空,请先选择学号或正确添加信息!

");

}

}

}

}

namespacezhsj

{

partialclassForm1

{

///

///必需的设计器变量。

///

privateSystem.ComponentModel.IContainercomponents=null;

///

///清理所有正在使用的资源。

///

///如果应释放托管资源,为true;否则为false。

protectedoverridevoidDispose(booldisposing)

{

if(disposing&&(components!

=null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#regionWindows窗体设计器生成的代码

///

///设计器支持所需的方法-不要

///使用代码编辑器修改此方法的内容。

///

privatevoidInitializeComponent()

{

this.label1=newSystem.Windows.Forms.Label();

this.groupBox1=newSystem.Windows.Forms.GroupBox();

this.button1=newSystem.Windows.Forms.Button();

this.textBox5=newSystem.Windows.Forms.TextBox();

this.textBox4=newSystem.Windows.Forms.TextBox();

this.textBox3=newSystem.Windows.Forms.TextBox();

this.textBox2=newSystem.Windows.Forms.TextBox();

this.textBox1=newSystem.Windows.Forms.TextBox();

this.label5=newSystem.Windows.Forms.Label();

this.label4=newSystem.Windows.Forms.Label();

this.label3=newSystem.Windows.Forms.Label();

this.label2=newSystem.Windows.Forms.Label();

this.groupBox2=newSystem.Windows.Forms.GroupBox();

this.button2=newSystem.Windows.Forms.Button();

this.textBox6=newSystem.Windows.Forms.TextBox();

this.groupBox3=newSystem.Windows.Forms.GroupBox();

this.button3=newSystem.Windows.Forms.Button();

boBox1=newSystem.Windows.Forms.ComboBox();

this.groupBox1.SuspendLayout();

this.groupBox2.SuspendLayout();

this.groupBox3.SuspendLayout();

this.SuspendLayout();

//

//label1

//

this.label1.AutoSize=true;

this.label1.Location=newSystem.Drawing.Point(15,34);

this.label1.Name="label1";

this.label1.Size=newSystem.Drawing.Size(41,12);

this.label1.TabIndex=0;

this.label1.Text="学号:

";

//

//groupBox1

//

this.groupBox1.Controls.Add(this.button1);

this.groupBox1.Controls.Add(this.textBox5);

this.groupBox1.Controls.Add(this.textBox4);

this.groupBox1.Controls.Add(this.textBox3);

this.groupBox1.Controls.Add(this.textBox2);

this.groupBox1.Controls.Add(this.textBox1);

this.groupBox1.Controls.Add(this.label5);

this.groupBox1.Controls.Add(this.label4);

this.groupBox1.Controls.Add(this.label3);

this.groupBox1.Controls.Add(this.label2);

this.groupBox1.Controls.Add(this.label1);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 历史学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1