C#winform练习

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//1.添加两个按钮一个文本框如下图:当用户点击爱时弹出“我也爱你哟”,点击不爱时弹出“还是被你给点到了”并退出程序usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceLove_and_notLove{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidLovebtn_Click(objectsender,EventArgse){MessageBox.Show(我也爱你哟!);this.Close();}//触发点击事件privatevoidNotLovebtn_Click(objectsender,EventArgse){MessageBox.Show(还是被你给点到了);//关闭主程序this.Close();}//针对鼠标移动至按钮所属区域时触发事件privatevoidNotLovebtn_MouseEnter(objectsender,EventArgse){//声明一个变量用于存放按钮的X轴坐标intx=this.ClientSize.Width-NotLovebtn.Width;//声明一个变量用于存放按钮的Y轴坐标inty=this.ClientSize.Height-NotLovebtn.Height;//声明一个随机数实例Randomr=newRandom();//声明按钮的移动范围NotLovebtn.Location=newPoint(r.Next(0,x+1),r.Next(0,y+1));}}}//2.在窗口中放置两个控件如下图:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceLable控件和TextBox控件{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidtxtBox_TextChanged(objectsender,EventArgse){lblTxt.Text=txtBox.Text;//使lable控件的值等于textbox的值}}}//3.做一个跑马灯的练习控件如下图:在窗体中放入label和Timer两个控件并分别设置两个控件的属性Label控件:Text:☆★Timer控件:Enable:trueTick:事件usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace跑马灯{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidtimer1_Tick(objectsender,EventArgse){label1.Text=label1.Text.Substring(1)+label1.Text.Substring(0,1);}}}//4.小闹钟在窗体中放一个label和timer两个控件并修改其属性Timer控件:Interval:1000Enable:TrueLabel控件:Name:labtimeusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Media;namespace小闹钟{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidtimer2_Tick(objectsender,EventArgse){labTime.Text=DateTime.Now.ToString();if(DateTime.Now.Hour==12&&DateTime.Now.Minute==02&&DateTime.Now.Second==00){SoundPlayersp=newSoundPlayer();sp.SoundLocation=@C:\ProgramFiles\MicrosoftOffice\OFFICE11\MEDIA\DRUMROLL.WAV;sp.Play();}}privatevoidForm1_Load(objectsender,EventArgse){labTime.Text=DateTime.Now.ToString();}}}//5.需要输入用户名“admin”和密码“admin”才能登陆到使用介面的记事本程序://在窗体中需要拖入2个label控件4个button控件3个textBox控件并修改其相应属性Label控件:label1:Text:用户名:Label2:Text:密码:textBox控件:textBox2:passwrodChar:*textBox3:MultiLine(多行)WordWrap:FalseButton控件:button1:Name:btnLoginText:登陆Button2:Name:btnResetText:重置Button3:Name:butWordsText:自动换行Button4:Name:butSaveText:保存usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespace记事本程序{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}///summary///程序运行时隐藏文本编程器////summary///paramname=sender/param///paramname=e/paramprivatevoidForm1_Load(objectsender,EventArgse){//txtWords.WordWrap=false;btnWords.Visible=false;btnSave.Visible=false;txtWords.Visible=false;}privatevoidbutLogin_Click(objectsender,EventArgse){stringname=txtName.Text.Trim();stringpwd=txtPwd.Text;if(name==admin&&pwd==admin){//txtWords.WordWrap=true;btnWords.Visible=true;btnSave.Visible=true;txtWords.Visible=true;label1.Visible=false;label2.Visible=false;butLogin.Visible=false;butReset.Visible=false;txtName.Visible=false;txtPwd.Visible=false;MessageBox.Show(登陆成功!);}else{MessageBox.Show(用户名或密码错误,请重新输入!);txtName.Clear();txtPwd.Clear();txtName.Focus();}}///summary///重置用户与密码的文本////summary///paramname=sender/param///paramname=e/paramprivatevoidbutReset_Click(objectsender,EventArgse){txtName.Clear();txtPwd.Clear();txtName.Focus();}///summary///自动换行////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnWords_Click(objectsender,EventArgse){if(btnWords.Text==自动换行){txtWords.WordWrap=true;btnWords.Text=取消自动换行;}elseif(btnWords.Text==取消自动换行){txtWords.WordWrap=false;btnWords.Text=自动换行;}}///summary///保存文件到指定位置////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnSave_Click(objectsender,EventArgse){using(FileStreamfsWrite=newFileStream(@C:\DocumentsandSettings\Administrator\桌面\new.txt,FileMode.OpenOrCreate,FileAccess.Write)){stringstr=txtWords.Text.Trim();byte[]buffer=System.Text.Encoding.Default.GetBytes(str);fsWrite.Write(buffer,0,buffer.Length);}MessageBox.Show(保存成功);}}}//6.老师或者学生登陆//在窗体中拖入2个label控件2个textBox控件2个radiobutton和1个button控件如下图:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace学生或者老师登陆{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbtn_Click(objectsender,EventArgse){if(rdostudent.Checked||rdoteacher.Checked){stringname=txtName.Text.Trim();st

1 / 48
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功