C#win练习.doc_第1页
C#win练习.doc_第2页
C#win练习.doc_第3页
C#win练习.doc_第4页
C#win练习.doc_第5页
已阅读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: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, Even

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论