C#winform练习

上传人:积*** 文档编号:120180321 上传时间:2022-07-16 格式:DOC 页数:72 大小:395KB
返回 下载 相关 举报
C#winform练习_第1页
第1页 / 共72页
C#winform练习_第2页
第2页 / 共72页
C#winform练习_第3页
第3页 / 共72页
点击查看更多>>
资源描述
/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:1000Enable:TrueLabel 控件:Name:labtimeusing 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 & DateTime.Now.Minute = 02 & DateTime.Now.Second = 00) SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = C:Program FilesMicrosoft OfficeOFFICE11MEDIADRUMROLL.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:FalseButton控件:button1:Name:btnLoginText:登陆Button2:Name:btnResetText:重置Button3:Name:butWordsText:自动换行Button4:Name:butSaveText:保存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 & 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(); / / 重置顾客与密码的文本 / / / 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 SettingsAdministrator桌面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 & pwd = student) MessageBox.Show(登陆成功); else MessageBox.Show(登陆失败); txtName.Clear(); txtPwd.Clear(); txtName.Focus(); else if (name = teacher & pwd = teacher) MessageBox.Show(登陆成功); 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:UsersSkyDesktoppicture1.jpg); /设立图片在窗口中如何显示 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; /图片途径字段 string path = Directory.GetFiles(C:UsersSkyDesktoppicture); /图片下标变量 int i = 0; / / 下一张 / / / private void button2_Click(object sender, EventArgs e) i+; if (i = path.Length) i = 0; pictureBox1.Image = Image.FromFile(pathi); / / 上一张 / / / private void button1_Click(object sender, EventArgs e) i-; if(i0) i = path.Length - 1; pictureBox1.Image = Image.FromFile(pathi); /9.加载图片并在每分钟更换一张图片如下图:timer控件Enable:TrueInterval:1000using 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:UsersSkyDesktoppicture1.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:UsersSkyDesktoppicture1.jpg); pictureBox2.Image = Image.FromFile(C:UsersSkyDesktoppicture1.jpg); pictureBox3.Image = Image.FromFile(C:UsersSkyDesktoppicture1.jpg); pictureBox4.Image = Image.FromFile(C:UsersSkyDesktoppicture1.jpg); pictureBox5.Image = Image.FromFile(C:UsersSkyDesktoppicture1.jpg); pictureBox6.Image = Image.FromFile(C:UsersSkyDesktoppicture1.jpg); string path = Directory.GetFiles(C:UsersSkyDesktoppicture); Random r = new Random(); private void timer1_Tick(object sender, EventArgs e) /每隔一秒钟换一张图片 pictureBox1.Image = Image.FromFile(pathr.Next(0, path.Length); pictureBox2.Image = Image.FromFile(pathr.Next(0, path.Length); pictureBox3.Image = Image.FromFile(pathr.Next(0, path.Length); pictureBox4.Image = Image.FromFile(pathr.Next(0, path.Length); pictureBox5.Image = Image.FromFile(pathr.Next(0, path.Length); pictureBox6.Image = Image.FromFile(pathr.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:UsersSkyDesktoppicturenew); /Console.WriteLine(剪切成功); /Console.ReadKey(); /获取文献完整目录下的所有文献(指定文献类型显示) /string pathfiles = Directory.GetFiles(C:UsersSkyDesktoppicture,*.jpg); /for (int i = 0; i pathfiles.Length;i+ ) / Console.WriteLine(pathfilesi); / Console.ReadKey(); /获取指定目录下所有文献夹的所有途径 /string path= Directory.GetDirectories(c:UsersSkyDesktoppicturenew); / for(int i=0;ipath.Length;i+) / Console.WriteLine(pathi); / Console.ReadKey(); /判断指定的文献夹与否存在 if(Directory.Exists(c:UsersSkyDesktoppicturenew) for (int i=0;i100;i+) Directory.CreateDirectory(c:UsersSkyDesktoppicturenew + i); Console.ReadKey(); /11.做一种简朴的浏览器/在窗体中拖入WebBrowser控件,TextBox控件和一种Botton控件botton控件Name:btnText:转到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:DropDownListusing 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;i1949;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&year%100!=0) day=29; 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:Stretchimageusing 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;ipath.Length;i+) /listBox1.Items.Add
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!