C#win練習(xí)參考模板_第1頁
C#win練習(xí)參考模板_第2頁
C#win練習(xí)參考模板_第3頁
C#win練習(xí)參考模板_第4頁
C#win練習(xí)參考模板_第5頁
已閱讀5頁,還剩44頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、/1.添加兩個按鈕一個文本框如下圖:當(dāng)用戶點擊愛時彈出“我也愛你喲”,點擊不愛時彈出“還是被你給點到了”并退出程序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

2、 Form1() InitializeComponent(); private void Lovebtn_Click(object sender, EventArgs e) MessageBox.Show("我也愛你喲!"); this.Close(); /觸發(fā)點擊事件 private void NotLovebtn_Click(object sender, EventArgs e) MessageBox.Show("還是被你給點到了"); /關(guān)閉主程序 this.Close(); /針對鼠標(biāo)移動至按鈕所屬區(qū)域時觸發(fā)事件 private void Not

3、Lovebtn_MouseEnter(object sender, EventArgs e) /聲明一個變量用于存放按鈕的軸坐標(biāo) int x = this.ClientSize.Width-NotLovebtn.Width; /聲明一個變量用于存放按鈕的軸坐標(biāo) int y = this.ClientSize.Height-NotLovebtn.Height;1 / 49 /聲明一個隨機數(shù)實例 Random r = new Random(); /聲明按鈕的移動范圍 NotLovebtn.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1)

4、; /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(); priva

5、te void txtBox_TextChanged(object sender, EventArgs e) lblTxt.Text = txtBox.Text; /使lable控件的值等于textbox的值 /3.做一個跑馬燈的練習(xí)控件如下圖:在窗體中放入label和Timer兩個控件并分別設(shè)置兩個控件的屬性Label控件:Text:Timer控件:Enable:true Tick:事件using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System

6、.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和

7、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 Fo

8、rm1 : 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 = &qu

9、ot;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控件并修改其相應(yīng)屬性Label控件:label1:Text: 用戶名:Label2:Text: 密碼

10、:textBox控件:textBox2:passwrodChar:*textBox3: MultiLine(多行)WordWrap:FalseButton控件:button1:Name:btnLoginText:登陸B(tài)utton2:Name:btnResetText:重置Button3:Name:butWordsText:自動換行Button4:Name:butSaveText:保存using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Dr

11、awing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace 記事本程序 public partial class Form1 : Form public Form1() InitializeComponent(); / <summary> / 程序運行時隱藏文本編程器 / </summary> / <param name="sender"></param> / <param name=&quo

12、t;e"></param> 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

13、; 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.

14、Visible = false; MessageBox.Show("登陸成功!"); else MessageBox.Show("用戶名或密碼錯誤,請重新輸入!"); txtName.Clear(); txtPwd.Clear(); txtName.Focus(); / <summary> / 重置用戶與密碼的文本 / </summary> / <param name="sender"></param> / <param name="e"></par

15、am> private void butReset_Click(object sender, EventArgs e) txtName.Clear(); txtPwd.Clear(); txtName.Focus(); / <summary> / 自動換行 / </summary> / <param name="sender"></param> / <param name="e"></param> private void btnWords_Click(object sende

16、r, EventArgs e) if (btnWords.Text = "自動換行") txtWords.WordWrap = true; btnWords.Text = "取消自動換行" else if (btnWords.Text = "取消自動換行") txtWords.WordWrap = false; btnWords.Text = "自動換行" / <summary> / 保存文件到指定位置 / </summary> / <param name="sender&q

17、uot;></param> / <param name="e"></param> 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.Tr

18、im(); byte buffer = System.Text.Encoding.Default.GetBytes(str); fsWrite.Write(buffer, 0, buffer.Length); MessageBox.Show("保存成功"); /6.老師或者學(xué)生登陸/在窗體中拖入2個label控件2個textBox控件2個radiobutton和1個button控件如下圖:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;us

19、ing System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 學(xué)生或者老師登陸 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

20、(); 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 =

21、 "teacher") MessageBox.Show("登陸成功"); else MessageBox.Show("登陸失敗"); txtName.Clear(); txtPwd.Clear(); txtName.Focus(); else MessageBox.Show("請首先選擇登陸身份"); /7.父(MDI)窗口練習(xí)在form1主窗口中放置菜單:"顯示子窗體" "橫向排列""縱向排列"/依次創(chuàng)建4個窗體在form1窗體form1窗體:isMdi

22、Container: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窗體設(shè)計 public partial class Form1 : Form public Form1

23、() 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, EventAr

24、gs 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.實現(xiàn)圖片的上翻下翻在窗中拖放入pictureBox工具兩個botton按鈕工具如下圖botton1:Text:上一張botton2:Text:下一張using System;using System.Colle

25、ctions.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_L

26、oad(object sender, EventArgs e) /設(shè)置窗體加載時顯示的默認(rèn)圖片 pictureBox1.Image = Image.FromFile("C:UsersSkyDesktoppicture1.jpg"); /設(shè)置圖片在窗口中如何顯示 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; /圖片路徑字段 string path = Directory.GetFiles("C:UsersSkyDesktoppicture"); /圖片下標(biāo)變量 int i = 0; / &l

27、t;summary> / 下一張 / </summary> / <param name="sender"></param> / <param name="e"></param> private void button2_Click(object sender, EventArgs e) i+; if (i = path.Length) i = 0; pictureBox1.Image = Image.FromFile(pathi); / <summary> / 上一張 / &l

28、t;/summary> / <param name="sender"></param> / <param name="e"></param> private void button1_Click(object sender, EventArgs e) i-; if(i<0) i = path.Length - 1; pictureBox1.Image = Image.FromFile(pathi); /9.加載圖片并在每分鐘更換一張圖片如下圖:timer控件Enable:TrueInterval

29、: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 For

30、m1 : 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 = Picture

31、BoxSizeMode.StretchImage; pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage; /在窗體加載的時候給每一個pictureBox都賦值一個圖片 pictureBox1.Image =

32、 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"); pic

33、tureBox5.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, EventArg

34、s 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.FromF

35、ile(pathr.Next(0, path.Length); pictureBox6.Image = Image.FromFile(pathr.Next(0, path.Length); /10.目錄練習(xí)using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace 目錄練習(xí) class Program static void Main(string args) /新建文件夾 /Dir

36、ectory.CreateDirectory("D:a"); /Console.WriteLine("創(chuàng)建成功"); /Console.ReadKey(); /刪除文件夾 /Directory.Delete("d:a",true); /Console.WriteLine("刪除成功"); /Console.ReadKey(); /移動文件夾(必須在同一個根下) /Directory.Move("c:new", "C:UsersSkyDesktoppicturenew"); /

37、Console.WriteLine("剪切成功"); /Console.ReadKey(); /獲取文件完整目錄下的全部文件(指定文件類型顯示) /string pathfiles = Directory.GetFiles("C:UsersSkyDesktoppicture","*.jpg"); /for (int i = 0; i < pathfiles.Length;i+ ) / Console.WriteLine(pathfilesi); / Console.ReadKey(); /獲取指定目錄下所有文件夾的全部路徑 /s

38、tring path= Directory.GetDirectories("c:UsersSkyDesktoppicturenew"); / for(int i=0;i<path.Length;i+) / Console.WriteLine(pathi); / Console.ReadKey(); /判斷指定的文件夾是否存在 if(Directory.Exists("c:UsersSkyDesktoppicturenew") for (int i=0;i<100;i+) Directory.CreateDirectory("c:Us

39、ersSkyDesktoppicturenew" + i); Console.ReadKey(); /11.做一個簡單的瀏覽器/在窗體中拖入WebBrowser控件,TextBox控件和一個Botton控件botton控件Name:btnText:轉(zhuǎn)到using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threadi

40、ng.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控件:DropDownSty

41、le: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() InitializeCompo

42、nent(); / <summary> / 程序加載時顯示年份 / </summary> / <param name="sender"></param> / <param name="e"></param> private void Form1_Load(object sender, EventArgs e) int year = DateTime.Now.Year; for(int i=year;i>1949;i-) cboYear.Items.Add(i+"年"); / <summary> / 月份被選定時加載天 / </summary> / <param name="sender"></param> / <param name="e"></param> private void cboMonth_Se

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論