当前位置: 首页 > news >正文

安图县建设局网站南阳网站建设南阳

安图县建设局网站,南阳网站建设南阳,网站定制开发加公众号,要如何自己创建一个网站在c#winform中我们在写程序时#xff0c;经常需要进行数据处理#xff0c;那么数据如何保存和读取#xff08;下面我们通过序列化和反序列化的方式来实现#xff09; 第一步: 我们建立一个winform窗体 第二步: 构建一个外部实体类#xff08;Student类#xff09; 第…在c#winform中我们在写程序时经常需要进行数据处理那么数据如何保存和读取下面我们通过序列化和反序列化的方式来实现 第一步:  我们建立一个winform窗体 第二步:  构建一个外部实体类Student类 第三步从图上按钮可以发现现在我定义了两个按钮保存参数和读取参数保存参数对应代码DataSave()读取参数对应DataRead DataSava方法代码如下 DataRead方法如下 最后展示完整代码内容 结果演示 第一步:   按F5启动应用程序: 第二步修改控件中的数据并且点击保存参数 第三步:保存成功后点击读取参数在图片的右边会产生一个与原始窗体一样的窗体 1.首先创建窗体: 2.再新建一个实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace SlandDs {//允许本类可以被序列化[Serializable]public class Student{//定义了五个属性分别是姓名年龄性别班级爱好public string Name { get; set; }public int Age { get; set; }public string Six { get; set; }public string ClaslRoom { get; set; }public string Hoppy { get; set; }} } 3.第三步从图上按钮可以发现现在我定义了两个按钮保存参数和读取参数保存参数对应代码DataSave()读取参数对应DataRead DataSava方法代码如下 private bool DataSava(string fileName){try{if (student null){student new Student();}//首先把控件的值赋值给对象也就是序列化student.Name txt_name.Text;student.Age Convert.ToInt16(num_age.Value);student.Six txt_six.Text;student.ClaslRoom txt_classroom.Text;student.Hoppy txt_hobby.Text;//第二步就是要把这些内容保存在一个数据文件中后缀名为.datstring FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){Directory.CreateDirectory(FileDir);}//将学生数据保存在数据文件中mSerialize.FilePath fileName;string Ren;mSerialize.SaveObj(student, out Ren);MessageBox.Show(学生数据保存成功, 提示);return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}} DataRead方法如下 private bool DataRead(string fileName){try{if(studentnull){student new Student();}else if (mSerialize null){mSerialize new MSerialize(null);}string FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){MessageBox.Show(student文件不存在);return false;}mSerialize.FilePath fileName;string Ren;if (mSerialize.LoadObj(out student,out Ren)true){txt_name.Text student.Name;num_age.Value student.Age;txt_six.Text student.Six;txt_classroom.Text student.ClaslRoom;txt_hobby.Text student.Hoppy;MessageBox.Show(读取学生数据成功,提示);}elseMessageBox.Show(读取学生文件失败,提示);Form1 form new Form1();form.dataRead(ref student);form.ShowDialog();return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}} 最后展示完整代码内容 using System; using System.IO; using System.Windows.Forms; using WRO;namespace SlandDs {public partial class Form1 : Form{static string FileName System.Windows.Forms.Application.StartupPath \\studnet\\ studnet.dat;//必须保证在同一个命名控件下Student student;MSerialize mSerialize;public Form1(){mSerialize new MSerialize(FileName);InitializeComponent();}public void dataRead(ref Student _student){try{if (_studentnull){_student new Student();}txt_name.Text _student.Name;num_age.Value _student.Age;txt_six.Text _student.Six;txt_classroom.Text _student.ClaslRoom;txt_hobby.Text _student.Hoppy;}catch (System.Exception ex){MessageBox.Show(ex.Message);}}//保存private void button_保存_Click(object sender, EventArgs e){DataSava(FileName);}private bool DataSava(string fileName){try{if (student null){student new Student();}//首先把控件的值赋值给对象也就是序列化student.Name txt_name.Text;student.Age Convert.ToInt16(num_age.Value);student.Six txt_six.Text;student.ClaslRoom txt_classroom.Text;student.Hoppy txt_hobby.Text;//第二步就是要把这些内容保存在一个数据文件中后缀名为.datstring FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){Directory.CreateDirectory(FileDir);}//将学生数据保存在数据文件中mSerialize.FilePath fileName;string Ren;mSerialize.SaveObj(student, out Ren);MessageBox.Show(学生数据保存成功, 提示);return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}//读取private void button_读取_Click(object sender, EventArgs e){DataRead(FileName);}private bool DataRead(string fileName){try{if(studentnull){student new Student();}else if (mSerialize null){mSerialize new MSerialize(null);}string FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){MessageBox.Show(student文件不存在);return false;}mSerialize.FilePath fileName;string Ren;if (mSerialize.LoadObj(out student,out Ren)true){txt_name.Text student.Name;num_age.Value student.Age;txt_six.Text student.Six;txt_classroom.Text student.ClaslRoom;txt_hobby.Text student.Hoppy;MessageBox.Show(读取学生数据成功,提示);}elseMessageBox.Show(读取学生文件失败,提示);Form1 form new Form1();form.dataRead(ref student);form.ShowDialog();return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}} } 结果演示 按F5启动应用程序: 第二步修改控件中的数据并且点击保存参数 第三步:保存成功后点击读取参数在图片的右边会产生一个与原始窗体一样的窗体
http://www.hkea.cn/news/14560486/

相关文章:

  • .net 网站域账号自动验证码淘宝美工做兼职的网站
  • 罗湖商城网站建设哪家技术好wordpress的安装步骤
  • 和县网站开发郑州高端网站建设公司
  • 查询网站是哪家公司做的寿光网站建设价格
  • 快速整站排名seo教程石家庄seo
  • 网站主页制作教程苏州网站建设方案策划
  • 百度广告联盟网站网站域名有了 网站如何建设
  • 昆明网站定制绿色建筑设计
  • 厦门电子商务网站建设东营市建设工程管理信息网
  • 怎么建设一个漫画网站刷赞网站推广免费链接
  • 企业网站的维护工作要怎么做室内设计师培训哪里好
  • 网站有必要备案吗简约创意logo设计免费生成
  • 手机在线做ppt模板下载网站苏州建设工程合同备案网站
  • 小说关键词提取软件信息流优化师工作总结
  • 400电话单页网站wordpress nginx配置伪静态
  • 云浮市哪有做网站的青岛外贸建设网站
  • 手机网站素材网苏州网站建设系统哪家好
  • 哈尔滨建站软件温州seo方法
  • 网站开发内容wordpress 公式编辑器
  • 拖式网站建设电商运营怎么学
  • 牛商网做的网站如何tint wordpress
  • 建站快车帮助怎么用flashfxp上传网站
  • 温州旅游 网站建设扬州立扬计算机培训网站建设怎么样
  • 做论坛app网站有哪些贸易公司网站建设
  • 网站建设是 口号黄江镇网站建设
  • php购物网站开发怎么把网站设置为信任网站
  • wordpress动漫博客主题昆明seocn整站优化
  • wordpress优秀移动站点前台网站开发技术
  • 网站开发答辩知识点wordpress修改固定链接后页面404
  • 面试网站建设需要的简历zenmeyong wordpress