环境:VS-2013
时间:20200402
学生信息管理系统;
控件 | |
---|---|
Form | Form1 ;(text:学生信息管理系统) |
ToolStripMenuItem | ToolStripMenuItem(text:文件(F)) |
label | label1(text:学号) |
label | label2(text:姓名) |
label | label3(text:性别) |
label | label4 (text:系别) |
label | label5(text:年级) |
label | label6(text:学生信息) |
label | label7(text:输入学生信息) |
textBox | textBox1 |
textBox | textBox2 |
textBox | textBox3 |
textBox | textBox4 |
textBox | textBox5 |
radioButton | radioButton1(text:男) |
radioButton | radioButton2(text:女) |
button | button1(text:保存) |
button | button2(text:导入) |
button | button3(text:保存Bw) |
button | button4(text:导入dr) |
button | button5(text:提取字符串中信息) |
richTextBox | richTextBox1 |
statusStrip | statusStrip1 |
menuStrip | menuStrip1 |
toolStripStatusLabel | toolStripStatusLabel2(text:时间进度) |
namespace _20200330
{
public class student
{
private int id = 0; //ID
private string name=" wuning" ;//姓名
private bool gender = true; //性别
private string depart = "vuxi"; //系别
private int grade = 0; //年级
public int Id //封装Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public bool Gender
{
get { return gender; }
set { gender = value; }
}
public string Depart
{
get { return depart; }
set { depart = value; }
}
public int Grade
{
get { return grade; }
set { grade = value; }
}
public student(int id, string name) //构造函数,带参id,name
{
Id = id;
Name=name;
}
public student(int id, string name,bool gender,string depart,int grader)
{
Id = id;
Name=name;
Gender = gender;
Depart = depart;
Grade = grade;
}
}
}
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace _20200330
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
Open();
}
private void Open()
{
OpenFileDialog openfiledia = new OpenFileDialog();
openfiledia.InitialDirectory = "d:\\";
openfiledia.Filter = "文本文件(*. txt) |*. txt |word文件 (*. docx) |*. docx|所有文件(*. *)|*.*";
openfiledia.FilterIndex = 1;
// if (openfiledia.ShowDialog()= DialogResult.OK)展开的写法:
DialogResult dr = openfiledia.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.LoadFile(openfiledia.FileName, RichTextBoxStreamType.PlainText);
}
}
private void button1_Click(object sender, EventArgs e)
{
//保存按钮点击事件: 检测文本框是否有内容?没有的话进行提示:有的话提示文本框文本赋值给学生对象的属性
if (check())
{
Save();
}
}
private void Save() //保存
{
MessageBox.Show("输入已完成,进行保存!");
int sid = Convert.ToInt32(textBox1.Text);//转换为int
int sgrade = Convert.ToInt32(textBox4.Text);
student stu = new student(sid, textBox2.Text, radioButton1.Checked, textBox3.Text, sgrade);
//保存 用保存文件对话框,进行保存
SaveFileDialog sf = new SaveFileDialog();
sf.InitialDirectory = "d:\\";
sf.Filter = "文本文件(*. txt) |*. txt |word文件 (*. docx) |*. docx|所有文件(*. *)|*.*";//筛选器
DialogResult dr = sf.ShowDialog();
if (dr == DialogResult.OK)
{
//对学生信息进行保存,FileStream方式,保存到对话框中的文件地址FileName
//创建一个文件流对象fs 设置参数(文件地址,文件打开方式即操作模式-附加,文件写入权限)
FileStream fs = new FileStream(sf.FileName, FileMode.Append, FileAccess.Write);
StreamWriter sv = new StreamWriter(fs);//对fs文件流对象进行操作的StreamWriter类对象
//进行写入操作
sv.WriteLine("学号:{0},姓名:{1},性别:{2},年级:{3},系别:{4}", stu.Id, stu.Name, stu.Gender ? "男" : "女", stu.Grade, stu.Depart);//将学生信息一个一个写入,格式跟println()一致
//文件写入完毕,要将文件流关闭!!!
sv.Close();
fs.Close();
MessageBox.Show("文件保存成功!!");
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("请确认退出!");
Close();
}
private void toolStripProgressBar1_Click(object sender, EventArgs e)
{
toolStripProgressBar1.Value = 60;
}
//点击 从文件导入信息到文本框中
///
/// 1,打开文件对话框,判断如果点确定
///
///
///
private void button2_Click(object sender, EventArgs e)//导入
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.InitialDirectory = "d:\\";
openfile.Filter = "文本文件(*. txt) |*. txt |word文件 (*. docx) |*. docx|所有文件(*. *)|*.*";
if (openfile.ShowDialog() == DialogResult.OK)
{
String s = File.ReadAllText(openfile.FileName);//用文件类 的读取方法File.ReadAllText,来打开文本框中确认的文件
richTextBox1.Text = s;
}
}
///
/// 二进制保存
///
///
///
private void button3_Click(object sender, EventArgs e)
{
//执行 方法
if (check())
{
Save2();
}
}
private bool check() //检测
{
if (textBox1.Text == string.Empty) //1.学号是否有内容
{
MessageBox.Show("请输入学号!");
return false;
}
else if (textBox2.Text == string.Empty)
{
MessageBox.Show("请输入姓名!");
return false;
}
else if (!radioButton1.Checked & !radioButton2.Checked) //判断两个单选按钮 都没被选择,就弹出提示框
{
MessageBox.Show("请输入性别!");
return false;
}
else if (textBox3.Text == string.Empty)
{
MessageBox.Show("请输入系别!");
return false;
}
else if (textBox4.Text == string.Empty)
{
MessageBox.Show("请输入年级!");
return false;
}
else
{
MessageBox.Show("信息均以填写完整!");
return true;
}
}
private void Save2()
{
MessageBox.Show("输入已完成,进行保存!");
int sid = Convert.ToInt32(textBox1.Text);//转换为int
int sgrade = Convert.ToInt32(textBox4.Text);
student stu = new student(sid, textBox2.Text, radioButton1.Checked, textBox3.Text, sgrade);
SaveFileDialog sf = new SaveFileDialog();
sf.InitialDirectory = "d:\\";
sf.Filter = "文本文件(*. txt) |*. txt |word文件 (*. docx) |*. docx|所有文件(*. *)|*.*";//筛选器
sf.InitialDirectory = "d:\\";
DialogResult dr = sf.ShowDialog();
if (dr == DialogResult.OK)
{
//对学生信息进行保存,FileStream方式,保存到对话框中的文件地址FileName
//创建一个文件流对象fs 设置参数(文件地址,文件打开方式即操作模式-附加,文件写入权限)
FileStream fs = new FileStream(sf.FileName, FileMode.Append, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);//对fs文件流对象进行操作的 二进制的写入方式
//进行写入操作 注意:分开写入
bw.Write(stu.Id);
bw.Write(stu.Name );
bw.Write(stu.Depart);
bw.Write(stu.Gender);
//文件写入完毕,要将文件流关闭!!!
bw.Close();
fs.Close();
MessageBox.Show("文件保存成功!!");
}
}
///
/// 用二进制方式导入
///
///
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.InitialDirectory = "d:\\";
openfile.Filter = "文本文件(*. txt) |*. txt |word文件 (*. docx) |*. docx|所有文件(*. *)|*.*";
FileStream fs;//文件流对象
BinaryReader br;//二进制读取对象
if (openfile.ShowDialog() == DialogResult.OK)
{
fs = new FileStream(openfile.FileName,FileMode.Open,FileAccess.Read);
br = new BinaryReader(fs);
//注意:分开导入
richTextBox1.Text=br.ReadInt32().ToString();//把br对象读取文件流fs,
richTextBox1.Text += br.ReadString();//把br对象读取文件流fs
richTextBox1.Text += br.ReadString();
richTextBox1.Text += br.ReadBoolean();
}
}
}
}