您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 1C#实验题目及代码
C#实验第1页共14页实验一实验内容:1.创建一个控制台程序,输出Hello,world.2.创建一个windows窗口应用程序,输出Hello,world.3.创建一个Web应用程序,输出Hello,world.程序代码:2UsingSystem;UsingSystem.Drawing;UsingSystem.Text;UsingSystem.Windows.Forms;NamespaceHelloWin{PublicpartialclassHelloFrm:Form{PublicHelloFrm();{InitializeComnent();}PrivatevoidHelloFrm_Load(objectsender,EventArgse){This.Title=”我的第一个Windows应用程序”;}}}实验二WinForms应用(一)要求:实现一个最简单的登录过程。用户首先进行登录界面(图3-1),当输入正确的用户名和口令及选择相应的用户类型后,登录成功后切换到程序的主界面(图3-3),否则提示输入的用户名和口令不正确(图3-2)。这里假定管理员正确的用户名为admin,口令为123,一般用户正确的用户名为username,口令为password。C#实验第2页共14页(图3-1)(图3-2)(图3-3)1.Form1.csusingSystem;usingSystem.Collections.Generic;C#实验第3页共14页usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer3{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){}privatevoidbutton1_Click(objectsender,EventArgse){stringA=comboBox1.SelectedItem.ToString();stringa=textBox1.Text;stringb=textBox2.Text;if(A==管理员&&a==admin&&b==123){Form2f2=newForm2();f2.Visible=true;this.Visible=false;}elseif(A==普通户&&a==username&&b==password){Form3f3=newForm3();f3.Visible=true;this.Visible=false;}else{MessageBox.Show(你输入的用户名或密码不正确,错误,C#实验第4页共14页MessageBoxButtons.OK);}}privatevoidbutton2_Click(objectsender,EventArgse){this.Visible=false;}privatevoidbutton2_Click_1(objectsender,EventArgse){textBox1.Text=;textBox2.Text=;}}}Form2.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer3{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();label1.Text=登陆成功!这是主程序界面;}privatevoidbutton1_Click(objectsender,EventArgse){this.Visible=false;}}}Form3.csusingSystem;C#实验第5页共14页usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer3{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();label1.Text=登陆成功!这是主程序界面;}privatevoidbutton1_Click(objectsender,EventArgse){this.Visible=false;}}}实验三WinForms应用(二)实验要求:在实验三的基础上对其功能进行扩展:1.给主界面添加菜单(如图3-1)。用户可以进入“管理信息”项的“注册信息”,单击后进入“信息登记表”页面(原主程序界面仍显示)。2.实现“信息登记表”页面的内容。用户填写好“基本信息”(图3-2)和“其它信息”(图3-3)后,单击“完成”按钮后,则输出用户填写的内容(图3-4)。C#实验第6页共14页图3-1图3-2C#实验第7页共14页图3-3图3-4提示:给主界面添加菜单的部分代码如下://1.声明控件成员privateSystem.Windows.Forms.MenuItemmenuInfomaion;privateSystem.Windows.Forms.MenuItemmenuRegister;//2.实例化控件privatevoidInitializeComponent(){this.mainMenu=newSystem.Windows.Forms.MainMenu();//添加主菜单this.menuInfomaion=newSystem.Windows.Forms.MenuItem();//管理信息菜单项this.menuRegister=newSystem.Windows.Forms.MenuItem();//注册信息子菜单项//3.设置各菜单项并进行菜单的组合(最好以可视化的方式实现以下代码)//mainMenu,主菜单中添加“管理信息”菜单项this.mainMenu.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{this.menuInfomaion});//menuInfomaion“管理信息”菜单项中添加“注册信息”项this.menuInfomaion.Index=0;this.menuInfomaion.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{this.menuRegister});C#实验第8页共14页this.menuInfomaion.Text=管理信息(&M);//menuRegister,给“注册信息”项添加事件this.menuRegister.Index=0;this.menuRegister.Text=注册信息(&R);this.menuRegister.Click+=newSystem.EventHandler(this.menuNew_Click);//4.把菜单添加到当前Form中this.Menu=this.mainMenu;}Form1.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer4{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){stringA=comboBox1.SelectedItem.ToString();stringa=textBox1.Text;stringb=textBox2.Text;if(A==管理员&&a==admin&&b==123){Form2f2=newForm2();f2.Visible=true;this.Visible=false;}elseif(A==普通户&&a==username&&b==password){Form3f3=newForm3();f3.Visible=true;C#实验第9页共14页this.Visible=false;}else{MessageBox.Show(你输入的用户名或密码不正确¨,错误”,MessageBoxButtons.OK);}}privatevoidbutton2_Click(objectsender,EventArgse){this.Visible=false;}privatevoidbutton2_Click_1(objectsender,EventArgse){textBox1.Text=;textBox2.Text=;}}}Form2.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer4{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();label1.Text=admin,欢迎进入学生信息管理系统!;}privatevoid注册信息ToolStripMenuItem_Click(objectsender,EventArgse){C#实验第10页共14页Form4frm=newForm4();frm.Visible=true;this.Visible=false;}privatevoidbutton1_Click(objectsender,EventArgse){this.Visible=false;}}}Form3.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceExer4{publicpartialclassForm3:Form{publicForm3(){InitializeComponent();label1.Text=username,这是主界面程序;}privatevoidbutton1_Click(objectsender,EventArgse){this.Visible=false;}}}Form4.csusingSystem;usingSystem.Collections.Generic;usingSystem.Com
本文标题:1C#实验题目及代码
链接地址:https://www.777doc.com/doc-4694275 .html