




已阅读5页,还剩43页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 添加两个按钮一个文本框如下图 当用户点击爱时弹出 我也爱你哟 点击不爱时弹出 还是被你给点到了 并退出程序 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms namespace Love and notLove public partial class Form1 Form public Form1 InitializeComponent private void Lovebtn Click object sender EventArgs e MessageBox Show 我也爱你哟 this Close 触发点击事件 private void NotLovebtn Click object sender EventArgs e MessageBox Show 还是被你给点到了 关闭主程序 this Close 针对鼠标移动至按钮所属区域时触发事件 private void NotLovebtn MouseEnter object sender EventArgs e 声明一个变量用于存放按钮的 轴坐标 int x this ClientSize Width NotLovebtn Width 声明一个变量用于存放按钮的 轴坐标 int y this ClientSize Height NotLovebtn Height 声明一个随机数实例 Random r new Random 声明按钮的移动范围 NotLovebtn Location new Point r Next 0 x 1 r Next 0 y 1 2 在窗口中放置两个控件如下图 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms namespace Lable控件和TextBox控件 public partial class Form1 Form public Form1 InitializeComponent private void txtBox TextChanged object sender EventArgs e lblTxt Text txtBox Text 使lable控件的值等于textbox的值 3 做一个跑马灯的练习控件如下图 在窗体中放入label和Timer两个控件并分别设置两个控件的属性 Label控件 Text Timer控件 Enable true Tick 事件 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms namespace 跑马灯 public partial class Form1 Form public Form1 InitializeComponent private void timer1 Tick object sender EventArgs e label1 Text label1 Text Substring 1 label1 Text Substring 0 1 4 小闹钟 在窗体中放一个 label 和 timer 两个控件并修改其属性 Timer 控件 Interval 1000 Enable True Label 控件 Name labtime using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms using System Media namespace 小闹钟 public partial class Form1 Form public Form1 InitializeComponent private void timer2 Tick object sender EventArgs e labTime Text DateTime Now ToString if DateTime Now Hour 12 sp SoundLocation C Program Files Microsoft Office OFFICE11 MEDIA DRUMROLL WAV sp Play private void Form1 Load object sender EventArgs e 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 False Button 控件 button1 Name btnLogin Text 登陆 Button2 Name btnReset Text 重置 Button3 Name butWords Text 自动换行 Button4 Name butSave Text 保存 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms using System IO namespace 记事本程序 public partial class Form1 Form public Form1 InitializeComponent 程序运行时隐藏文本编程器 private void Form1 Load object sender EventArgs e txtWords WordWrap false btnWords Visible false btnSave Visible false txtWords Visible false private void butLogin Click object sender EventArgs e string name txtName Text Trim string pwd txtPwd Text if name admin 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 重置用户与密码的文本 private void butReset Click object sender EventArgs e txtName Clear txtPwd Clear txtName Focus 自动换行 private void btnWords Click object sender EventArgs e if btnWords Text 自动换行 txtWords WordWrap true btnWords Text 取消自动换行 else if btnWords Text 取消自动换行 txtWords WordWrap false btnWords Text 自动换行 保存文件到指定位置 private void btnSave Click object sender EventArgs e using FileStream fsWrite new FileStream C Documents and Settings Administrator 桌面 new txt FileMode OpenOrCreate FileAccess Write string str 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控件如下图 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms namespace 学生或者老师登陆 public partial class Form1 Form public Form1 InitializeComponent private void btn Click object sender EventArgs e if rdostudent Checked rdoteacher Checked string name txtName Text Trim string pwd txtPwd Text if rdostudent Checked if name student else MessageBox Show 登陆失败 txtName Clear txtPwd Clear txtName Focus else if name teacher else MessageBox Show 登陆失败 txtName Clear txtPwd Clear txtName Focus else MessageBox Show 请首先选择登陆身份 7 父 MDI 窗口练习在form1主窗口中放置菜单 显示子窗体 横向排列 纵向排列 依次创建4个窗体在form1窗体 form1窗体 isMdiContainer true 添加MenuStrip工具 如下图 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms namespace MDI窗体设计 public partial class Form1 Form public Form1 InitializeComponent private void 纵向排列ToolStripMenuItem Click object sender EventArgs e LayoutMdi MdiLayout TileVertical private void 横向排列ToolStripMenuItem Click object sender EventArgs e LayoutMdi MdiLayout TileHorizontal private void 显示子窗体ToolStripMenuItem Click object sender EventArgs e Form2 frm2 new Form2 frm2 MdiParent this frm2 Show Form3 frm3 new Form3 frm3 MdiParent this frm3 Show Form4 frm4 new Form4 frm4 MdiParent this frm4 Show 8 实现图片的上翻下翻 在窗中拖放入pictureBox工具两个botton按钮工具如下图 botton1 Text 上一张 botton2 Text 下一张 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms using System IO namespace 图片上翻与下翻 public partial class Form1 Form public Form1 InitializeComponent private void Form1 Load object sender EventArgs e 设置窗体加载时显示的默认图片 pictureBox1 Image Image FromFile C Users Sky Desktop picture 1 jpg 设置图片在窗口中如何显示 pictureBox1 SizeMode PictureBoxSizeMode StretchImage 图片路径字段 string path Directory GetFiles C Users Sky Desktop picture 图片下标变量 int i 0 下一张 private void button2 Click object sender EventArgs e i if i path Length i 0 pictureBox1 Image Image FromFile path i 上一张 private void button1 Click object sender EventArgs e i if i 0 i path Length 1 pictureBox1 Image Image FromFile path i 9 加载图片并在每分钟更换一张图片如下图 timer控件 Enable True Interval 1000 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms using System IO using System Media namespace PictureBow和Timer的小程序 public partial class Form1 Form public Form1 InitializeComponent private void Form1 Load object sender EventArgs e 播放音乐 SoundPlayer sp new SoundPlayer sp SoundLocation C Users Sky Desktop picture 1 wav sp Play pictureBox1 SizeMode PictureBoxSizeMode StretchImage pictureBox2 SizeMode PictureBoxSizeMode StretchImage pictureBox3 SizeMode PictureBoxSizeMode StretchImage pictureBox4 SizeMode PictureBoxSizeMode StretchImage pictureBox5 SizeMode PictureBoxSizeMode StretchImage pictureBox6 SizeMode PictureBoxSizeMode StretchImage 在窗体加载的时候给每一个pictureBox都赋值一个图片 pictureBox1 Image Image FromFile C Users Sky Desktop picture 1 jpg pictureBox2 Image Image FromFile C Users Sky Desktop picture 1 jpg pictureBox3 Image Image FromFile C Users Sky Desktop picture 1 jpg pictureBox4 Image Image FromFile C Users Sky Desktop picture 1 jpg pictureBox5 Image Image FromFile C Users Sky Desktop picture 1 jpg pictureBox6 Image Image FromFile C Users Sky Desktop picture 1 jpg string path Directory GetFiles C Users Sky Desktop picture Random r new Random private void timer1 Tick object sender EventArgs e 每隔一秒钟换一张图片 pictureBox1 Image Image FromFile path r Next 0 path Length pictureBox2 Image Image FromFile path r Next 0 path Length pictureBox3 Image Image FromFile path r Next 0 path Length pictureBox4 Image Image FromFile path r Next 0 path Length pictureBox5 Image Image FromFile path r Next 0 path Length pictureBox6 Image Image FromFile path r Next 0 path Length 10 目录练习 using System using System Collections Generic using System Linq using System Text using System Threading Tasks using System IO namespace 目录练习 class Program static void Main string args 新建文件夹 Directory CreateDirectory D a Console WriteLine 创建成功 Console ReadKey 删除文件夹 Directory Delete d a true Console WriteLine 删除成功 Console ReadKey 移动文件夹 必须在同一个根下 Directory Move c new C Users Sky Desktop picture new Console WriteLine 剪切成功 Console ReadKey 获取文件完整目录下的全部文件 指定文件类型显示 string pathfiles Directory GetFiles C Users Sky Desktop picture jpg for int i 0 i pathfiles Length i Console WriteLine pathfiles i Console ReadKey 获取指定目录下所有文件夹的全部路径 string path Directory GetDirectories c Users Sky Desktop picture new for int i 0 i path Length i Console WriteLine path i Console ReadKey 判断指定的文件夹是否存在 if Directory Exists c Users Sky Desktop picture new for int i 0 i 100 i Directory CreateDirectory c Users Sky Desktop picture new i Console ReadKey 11 做一个简单的浏览器 在窗体中拖入WebBrowser控件 TextBox控件和一个Botton控件 botton控件 Name btn Text 转到 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms namespace 浏览器控件 public partial class Form1 Form public Form1 InitializeComponent private void btn Click object sender EventArgs e string text textBox1 Text Uri uri new Uri http text webBrowser1 Url uri 12 日期选择器 comboBox控件 DropDownStyle DropDownList using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms namespace 日期选择器 public partial class Form1 Form public Form1 InitializeComponent 程序加载时显示年份 private void Form1 Load object sender EventArgs e int year DateTime Now Year for int i year i 1949 i cboYear Items Add i 年 月份被选定时加载天 private void cboMonth SelectedIndexChanged object sender EventArgs e cboDay Items Clear int day 0 获得月份 string strMonth cboMonth SelectedItem ToString Split new char 月 StringSplitOptions RemoveEmptyEntries 0 获得年份 string strYear cboYear SelectedItem ToString Split new char 年 StringSplitOptions RemoveEmptyEntries 0 int year Convert ToInt32 strYear int month Convert ToInt32 strMonth switch month case 1 case 3 case 5 case 7 case 8 case 10 case 12 day 31 break case 2 if year 400 0 year 4 0 else day 28 break default day 30 break for int i 1 i day i cboDay Items Add i 日 年份被选定时加载月份 private void cboYear SelectedIndexChanged object sender EventArgs e cboMonth Items Clear for int i 1 i 13 i cboMonth Items Add i 月 13 点击列表更换图片 listBox控件 pictureBox控件 SizeMode Stretchimage using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System IO using System Linq using System Text using System Threading Tasks using System Windows Forms namespace 双击列表显示对应图片 public partial class Form1 Form public Form1 InitializeComponent private void Form1 Load object sender EventArgs e 程序加载时显示图片名列表 for int i 0 i path Length i listBox1 Items Add path i string fileName Path GetFileName path i listBox1 Items Add fileName 集合中存放图片的全路径 list Add path i 新建一个集合 List list new List 图片的全路径 string path Directory GetFiles C Users Sky Desktop picture jpg 双击更换图片 private void listBox1 DoubleClick object sender EventArgs e pictureBox1 Image Image FromFile path listBox1 SelectedIndex 点击更换图片 private void listBox1 MouseClick object sender MouseEventArgs e pictureBox1 Image Image FromFile path listBox1 SelectedIndex 14 单击切换音乐 在窗体内拖入一个listBox控件 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System IO using System Linq using System Media using System Text using System Threading Tasks using System Windows Forms namespace 双击切换音乐 public partial class Form1 Form public Form1 InitializeComponent 在程序加载时导入歌曲名列表 private void Form1 Load object sender EventArgs e string path Directory GetFiles c Users Sky Desktop picture wav for int i 0 i path Length i string fileNames Path GetFileName path i listBox1 Items Add fileNames listSongs Add path i 点击播放音乐 private void listBox1 Click object sender EventArgs e SoundPlayer sp new SoundPlayer sp SoundLocation listSongs listBox1 SelectedIndex sp Play List listSongs new List 15 剪刀石头布游戏 在窗体中放入6个lable控件 3个botton控件分别修改其属性 lable控件 lable1 Text 玩家出 lable2 Name lblPlayer lable3 Text 电脑出 lable4Name lblComputer lable5Text 裁判 lable6Name lblResult using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms namespace 剪刀石头布 public partial class Form1 Form public Form1 InitializeComponent 将石头按钮上的参数传给PlayerGame方法 private void button1 Click object sender EventArgs e string str 石头 PlayerGame str 生成共同的方法 private void PlayerGame string str 给玩家的文本赋值 lblPlayer Text str 生成一个玩家对象 Player player new Player 玩家返回一个值给裁判 int playerNumber player ShowFist str 生成一个电脑对象 Computer cpu new Computer 电脑返回一个随机值给裁判 int computerNumber cpu ShowFist 给电脑的文本赋值 lblcomputer Text cpu Fist 传递参数给裁判并返回结果 Result res CaiPan Judge playerNumber computerNumber 结果显示给文本 lblResult Text res ToString 将剪刀按钮上的参数传给PlayerGame方法 private void btnCut Click object sender EventArgs e string str 剪刀 PlayerGame str 将布按钮上的参数传给PlayerGame方法 private void btnNo Click object sender EventArgs e string str 布 PlayerGame str using System using System Collections Generic using System Linq using System Text using System Threading Tasks namespace 剪刀石头布 class Player public int ShowFist string fist int num 0 switch fist case 石头 num 1 break case 剪刀 num 2 break case 布 num 3 break return num using System using System Collections Generic using System Linq using System Text using System Threading Tasks namespace 剪刀石头布 class Computer public string Fist get set public int ShowFist Random r new Random int rNumber r Next 1 4 switch rNumber case 1 this Fist 石头 break case 2 this Fist 剪刀 break case 3 this Fist 布 break return rNumber using System using System Collections Generic using System Linq using System Text using System Threading Tasks namespace 剪刀石头布 public enum Result 玩家赢 电脑赢 平手 class CaiPan public static Result Judge int playerNumber int computerNumber if playerNumber computerNumber 1 playerNumber computerNumber 2 return Result 玩家赢 else if playerNumber computerNumber 0 return Result 平手 else return Result 电脑赢 16 打开文件对话框并在textBox中显示打开文件的内容 在窗体拖入botton控件和textBox控件 botton控件Text 打开文件 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Threading Tasks using System Windows Forms using System IO namespace 打开文件对话框 public partial class Form1 Form public Form1 InitializeComponent private void btn Click object sender EventArgs e 创建打开对话框实例 OpenFileDialog odf new OpenFileDialog 设置对话框的标题 odf Title 请选择要打开的文件O n n O 设置对话框多选 odf Multiselect true 设置对话框的初识目录 odf InitialDirectory c Users Sky Desktop 设置对话框的文件类型 odf Filter 文本文件 txt 媒体文件 wav 图片文件 jpg 所有文件 显示对话的方法 odf ShowDialog 获得在打开对话框中文件的路径 string path odf FileName if path return using FileStream fsRead new FileStream path FileMode OpenOrCreate FileAccess Read byte buffer new byte 1024 1024 5 int r fsRead Read buffer 0 buffer Length textBox1 Text Encoding Default GetString buffer 0 r 17 简单的文本编辑器 可以保存文本内容 在窗体拖入botton控件和textbox控件 Botton控件 Text 保存 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms using System IO namespace 保存对话框 public partial class Form1 Form public Form1 InitializeComponent private void button1 Click object sender EventArgs e SaveFileDialog sdf new SaveFileDialog sdf Title 请选择要保存的路径 sdf InitialDirectory C Documents and Settings Administrator 桌面 vs2013版本可以这样写 sdf Filter 文本文件 txt 所有文件 VS2008版本的写法 sdf Filter 文本文件 txt txt 所有文件 sdf ShowDialog string path sdf FileName if path return using FileStream fsWrite new FileStream path FileMode OpenOrCreate FileAccess Write byte buffer Encoding Default GetBytes textBox1 Text fsWrite Write buffer 0 buffer Length MessageBox Show 保存成功 18 文本字体和颜色编辑器 在窗体拖入2个botton控件和1个textBox控件 Botton控件 Botton1Text 字体 Botton2Text 颜色 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms namespace 字体和颜色对话框 public partial class Form1 Form public Form1 InitializeComponent private void button1 Click object sender EventArgs e FontDialog f new FontDialog f ShowDialog textBox1 Font f Font private void button2 Click object sender EventArgs e ColorDialog c new ColorDialog c ShowDialog textBox1 ForeColor c Color 19 文本编辑器 在窗体拖入1 个textbox控件在该控件中放入1个panel控件在该控件中放入1个listBox控件和1个botton控件 在放入一个menuStrip 控件并分别修改其属性 enmuStrip控件 文件 打开 保存 格式 自动换行 样式 字体 颜色 打开记录 显示 隐藏 Botton控件 Text using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using System Text using System Windows Forms using System IO namespace 记事本 public partial class Form1 Form public Form1 InitializeComponent private void Form1 Load object sender EventArgs e 加载程序时隐藏panel panel1 Visible false 取消文本框的自动换行功能 textBox1 WordWrap false private void 显示ToolStripMenuItem Click object sender EventArgs e panel1 Visible true private void 隐藏ToolStripMenuItem Click object sender EventArgs e panel1 Visible false List list new List 打开对话框 private void 打开ToolStripMenuItem Click object sender EventArgs e OpenFileDialog op new OpenFileDialog op Title 选择打开的文本文件 op Initi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 年关酒店安全培训课件
- 物业管理租赁合同书5篇
- 平面设计的色彩课件
- 体育赛事商业开发经纪合同5篇
- 平面与回转体相交课件
- 农发行郑州市新郑市2025秋招数据分析师笔试题及答案
- 农发行乌兰察布市集宁区2025秋招信息科技岗笔试题及答案
- 2025年工业互联网平台数据清洗算法在工业互联网平台数据采集中的应用报告
- 农发行钦州市浦北县2025秋招笔试创新题型专练及答案
- 私募股权投资热点行业2025年分析:退出策略与市场前景报告
- 发展经济学第四版教学课件02章发展经济学的形成与发展
- TCADHOH 0005-2024 听障人士120 报警服务规范
- 小学2年级心理健康教育课件《不做“小火山”》
- TSDDP 8-2024 新型无机磨石施工质量与验收规范
- 《人工智能导论》课程考试复习题库(含答案)
- 法律咨询服务质量控制方案
- 村集体经济理事长述职报告范本
- GB 1002-2024家用和类似用途单相插头插座型式、基本参数和尺寸
- DL∕T 515-2018 电站弯管 标准
- DZ∕T 0270-2014 地下水监测井建设规范
- 2024年河北石家庄市轨道交通集团有限责任公司招聘笔试参考题库含答案解析
评论
0/150
提交评论