

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、--------------基于C#語言,主講教師:錢 哨本課學(xué)時:72課時聯(lián)系方式:qianshao@bjjtxy.bj.cn,Windows程序設(shè)計,課程地位,,,,綜合基礎(chǔ)課程,SQL Server,,,,XML,Ajax [Javascript&XML],ASP.NET,Oracle,RSS,,Computer Base,,HTML&JavaScript,OOP\Java,,,C,,,,,Dat
2、abase Base,JSP/Servlet,,,EJB/WebService,C#,,,,,,,Struts/JSF,Testing/SQA,,,,Linux,,WinForms,,,第一章、Windows編程基礎(chǔ),本章主要內(nèi)容介紹1.1 windows和窗體1.2 Visual Stutio .net IDE簡介1.3 事件處理,CONTENT,1.1 Windows和窗體,,,,本章學(xué)習(xí)目標(biāo):,理解 W
3、indows 窗體使用基本控件如標(biāo)簽、文本、按鈕、列表框和組合框掌握窗體的常用屬性和方法,,GUI界面,1.1 Windows和窗體,,各種控件,,屬性,,放置控件的區(qū)域,1.1 Windows和窗體,WinForms應(yīng)用程序可能存在多個窗體,用于獲取用戶輸入的數(shù)據(jù)和向用戶顯示數(shù)據(jù),System.Windows.Forms,簡單而強大 改善了接口和基類 IntelliSense 新的管理數(shù)據(jù)提供程序 安全 靈活的
4、控件 通曉數(shù)據(jù) 向?qū)?1.1 Windows和窗體,1.1.2 創(chuàng)建 WinForms應(yīng)用程序,“開始”?“程序”?“Microsoft Visual Studio.NET 2005”?“Microsoft Visual Studio.NET 2005”,創(chuàng)建 WinForms應(yīng)用程序 6-2,,設(shè)計窗口,,1.1.2 創(chuàng)建 WinForms應(yīng)用程序,using System;using System.Drawing
5、;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace SampleProject{/// /// Form1 的摘要說明。/// public class Form1 : System.Windows.Forms.Form{/// /// 必需的設(shè)計器變量.///
6、 ,提供了大量繪圖工具的訪問權(quán)限,基礎(chǔ)核心命名空間,ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 類,大量窗體和控件,,從 System.Windows.Forms.Form 派生,Visual Studio .NET 生成的代碼,1.1.2 創(chuàng)建 WinForms應(yīng)用程序,private System.ComponentModel.Contain
7、er components = null;public Form1(){//// Windows 窗體設(shè)計器支持所必需的//InitializeComponent();//// TODO:在 InitializeComponent 調(diào)用之后添加任何構(gòu)造函數(shù)代碼//},,構(gòu)造函數(shù)調(diào)用 InitializeComponent() 方法,//下面代碼見:Form1.Designer.cs文件priva
8、te void InitializeComponent(){ this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1";},項目的容器,創(chuàng)建 WinForms應(yīng)用程序,/// /// 清理所有正在使用的資源。
9、【下面代碼: Form1.Designer.cs 】/// protected override void Dispose( bool disposing ){if( disposing ){if(components != null){components.Dispose();}}base.Dispose( disposing );},,釋放系統(tǒng)資源,1.1.2 創(chuàng)建 WinF
10、orms應(yīng)用程序,//下面代碼見:program.cs文件[STAThread]static void Main(){Application.Run(new Form1());},程序的主入口點,1.1.3 WinForms 中的常用控件,可視化界面組件統(tǒng)稱為控件,System.Windows.Forms.Control,1.1.3 WinForms 中的常用控件,,標(biāo)簽,,按鈕,,組合框,,列表框,,文本框,標(biāo)簽,
11、,1.1.3 WinForms 中的常用控件,標(biāo)簽控件,,按鈕控件,,文本框控件,,列表控件,,組合框控件,,private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){ linkLabel1.LinkVisited = true; Form2 newform = new Fo
12、rm2(); newform.Show(); this.Hide(); }private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //label2.Visible = true; label2.Show();
13、 } private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { label2.Visible = false; label2.Hide(); },案例:窗口的打開和關(guān)閉,文本框,,1.1.3 WinForms
14、 中的常用控件,按鈕,,1.1.3 WinForms 中的常用控件,案例:用戶登錄設(shè)計,private void button2_Click(object sender, EventArgs e) { clear(); } private void button1_Click(object sender, EventArgs e) {
15、 if (textBox1.Text == string.Empty || textBox2.Text == string.Empty) { MessageBox.Show("信息禁止為空!","登錄提示"); clear(); return;
16、 } if (!textBox1.Text.Equals("admin") || !textBox2.Text.Equals("admin")) { MessageBox.Show("用戶名稱或密碼為空!", "登錄提示"); clear();
17、 return; } else { MessageBox.Show("歡迎您登錄本系統(tǒng)!","消息提示"); clear(); } } public void clear()
18、 { textBox1.Clear(); textBox2.Clear(); textBox2.Focus(); },,列表框,,1.1.3 WinForms 中的常用控件,使用列表框(1),private void Form1_Load(object sender, EventArgs e) {
19、 this.listBox1.Items.Add("軟件部"); this.listBox1.Items.Add("硬件部"); this.listBox1.Items.Add("財務(wù)部"); this.listBox1.Items.Add("人事部"); },p
20、rivate void listBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("您選擇的部門是:"+listBox1.SelectedItem.ToString()+",位列第"+listBox1.SelectedIndex.ToString(),"信
21、息提示"); },使用列表框(2),private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); listBox1.Items.Add("軟件部"); listBox1.Items.Add("硬
22、件部"); listBox1.Items.Add("財務(wù)部"); listBox1.Items.Add("人事部"); } private void button2_Click(object sender, EventArgs e) { listBox1.Items.
23、Insert(2,"插入值"); label1.Text = "已經(jīng)添加" + listBox1.Items.Count.ToString() + "條記錄"; },組合框,,1.1.3 WinForms 中的常用控件,使用組合框,private void Form1_Load(object sender, EventArgs e)
24、 { this.comboBox1.Items.Add("財務(wù)部"); this.comboBox1.Items.Add("產(chǎn)品部"); this.comboBox1.Items.Add("銷售部"); this.comboBox1.Items.Add("生產(chǎn)
25、部"); //默認(rèn)的選擇是"產(chǎn)品部" this.comboBox1.SelectedIndex = 1; this.comboBox2.Items.Add("財務(wù)部"); this.comboBox2.Items.Add("產(chǎn)品部"); this.
26、comboBox2.Items.Add("銷售部"); this.comboBox2.Items.Add("生產(chǎn)部"); //默認(rèn)的選擇是"產(chǎn)品部" this.comboBox2.SelectedIndex = 1; this.comboBox3.Items.Add("財
27、務(wù)部"); this.comboBox3.Items.Add("產(chǎn)品部"); this.comboBox3.Items.Add("銷售部"); this.comboBox3.Items.Add("生產(chǎn)部"); //默認(rèn)的選擇是"產(chǎn)品部"
28、 this.comboBox3.SelectedIndex = 1; },消息框窗口,MessageBox.Show(“[消息文本]");,消息框用于顯示消息,,Abort, Cancel, Ignore, No, None, Ok, Retry 和 Yes,if (MessageBox.Show(“保存文件”,“保存", MessageBoxButtons.YesNo, MessageBox
29、Icon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes){//保存文件所用的代碼//保存后的 MessageBox},1.1.3 WinForms 中的常用控件,消息框窗口,private void button1_Click(object sender, EventArgs e) { M
30、essageBox.Show("嘿,這是簡單提示!","信息提示"); }private void button2_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("嘿,這是問詢提示!","問詢提示"
31、,MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { label1.Text = "您選擇了YES"; } else { label1.T
32、ext = "您選擇了NO"; } },private void button3_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("嘿,這是帶有圖標(biāo)的問詢提示!", "問詢提示", MessageBo
33、xButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button3,MessageBoxOptions.RightAlign); if (result == DialogResult.Yes) { label1.Text = "您選擇了圖標(biāo)YES";
34、 } else if(result==DialogResult.Cancel) { label1.Text = "您選擇了圖標(biāo)取消"; } else if (result == DialogResult.No) {
35、 label1.Text = "您選擇了圖標(biāo)NO"; } },應(yīng)用程序示例,解決方案資源管理器,屬性窗口,工具箱,應(yīng)用程序示例,,private void btnAdd_Click(object sender, System.EventArgs e){},private void btnAdd_Click(object sender, System.EventAr
36、gs e){this.txtEmpName.Enabled=true;this.txtAddress.Enabled=true;this.cboDesignation.Enabled=true;this.lstCurrDeptName.Enabled=true;},private void btnCancel_Click(object sender, System.EventArgs e){this.txtE
37、mpName.Text="";this.txtAddress.Text="";this.cboDesignation.Text=“經(jīng)理";},private void btnExit_Click (object sender, System.EventArgs e){string str="";for(int ctr=0;ctr<=th
38、is.lstCurrDeptName.SelectedItems.Count-1;ctr++) str += "\n"+this.lstCurrDeptName.SelectedItems[ctr].ToString();MessageBox.Show(“選定的項目為\n" +str);Application.Exit();},,應(yīng)用程序示例,private void cboDesig
39、nation_SelectedIndexChanged(object sender, System.EventArgs e){MessageBox.Show(“您已經(jīng)選定了" +this.cboDesignation.SelectedItem.ToString());},在退出應(yīng)用程序之前,使用 MessageBox.Show() 顯示在 str 變量中存儲選定項的消息框,,圖標(biāo),,系統(tǒng)按鈕,標(biāo)題欄,,控件
40、,1.1.4 窗體容器簡介,1.1.4 窗體容器簡介,SDI [單文檔界面] MDI [多文檔界面],1.1.5 窗體的屬性,1.1.5 窗體的常用方法和事件,示例:顯示另一窗體,示例:顯示另一窗體,[被調(diào)用的窗體類] [窗體實例] = new [被調(diào)用的窗體類]();,[窗體實例].Show();下面,在菜單的單擊事件中寫下如下的事件。,private void menuItem3_Click(object
41、sender, EventArgs e) { Form2 Mdichild = new Form2(); Mdichild.MdiParent = this; Mdichild.Show(); },當(dāng)然,需要再建立兩個窗體對象,form2和form3窗體,示例1:在form2窗體中進(jìn)行如下操作,,單擊“發(fā)送”按鈕,應(yīng)用程序示例,首先,
42、將form2的comboBox1下拉框填充完畢后,增加load事件private void Form2_Load(object sender, EventArgs e) { comboBox1.SelectedIndex = 0; textBox3.Text = ""; textBox1.Focus(); }其
43、次,添加form2的發(fā)送信息事件public void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "") {
44、 MessageBox.Show("姓名,或者郵件,或者提交,信息禁止為空!", "信息提示"); } else { this.Hide(); Form3 childform3 = new Form3(this.textBox1.Text,this.tex
45、tBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text); childform3.Show(); } }最后關(guān)閉窗體事件:private void button2_Click(object sender, EventArgs e) {
46、 this.Close(); },應(yīng)用程序示例,對于form3窗體而言,在系統(tǒng)初始事件填寫如下代碼:public partial class Form3 : Form { private string _name; private string _emailId; private string _subject; privat
47、e string _feedBack; public Form3(string varName, string varEmail, string varSubject, string varFeedBack) { InitializeComponent(); // 在 private 變量中存儲值 this._name = var
48、Name; this._emailId = varEmail; this._subject = varSubject; this._feedBack = varFeedBack; // 在列表框中放置值 listBox1.Items.Add("姓名:" + this._name);
49、 listBox1.Items.Add("郵件地址:" + this._emailId); listBox1.Items.Add("信息主題:" + this._subject); listBox1.Items.Add("反饋意見:" + this._feedBack); } privat
50、e void button1_Click(object sender, EventArgs e) { MessageBox.Show("感謝您輸入的反饋!"); this.Close(); } },示例2:在MDI父窗口中,子窗口如何彼此之間傳遞信息?,代碼見下,示例3:如何防止重復(fù)打開子窗體啊?,方法: 直接檢測是否已經(jīng)打
51、開此MDI窗體 // 是否已經(jīng)打開了?(用循環(huán)來判斷) foreach (Form childrenForm in this.MdiChildren) { //檢測是不是當(dāng)前子窗體名稱 if (childrenForm.Name == "子窗體名稱") { //是的話就是把他顯示 childrenForm.Visible = true; //并激活該窗體 childrenFor
52、m.Activate(); return; } } //下面是打開子窗體 Form1 childrenForm = new Form1(); childrenForm.MdiParent = this; childrenForm.Show(); childrenForm.WindowState = FormWindowState.Maximized;,示例4:另一種窗體之間的傳值技巧(一)——傳單個值,
53、1、先在Form2中定義一個成員變量和一個屬性如下: private string form2zhi = null; public string Form2ChuanZhi { get { return form2zhi; } } 2、再在Form3中定義一個成員變量和一個屬性如下: private string form3zhi = null; public string Form3ChuanZhi { se
54、t { form3zhi = value; } get { return form3zhi; } },3、雙擊btn_ChuanZhi在這個事件中寫入以下代碼(主要是顯示Form3窗體和將Form2中的值傳過去): Form3 form3 = new Form3(); form3.Form3ChuanZhi = form2zhi;//將值傳過去 form3.Show();,代碼見下,示例5:另一種窗體之間的傳值技巧
55、(二)——類保存任意值,代碼見下,Winform界面美化技巧,1、從附件資料中確認(rèn)有第三方動態(tài)鏈接庫文件DotNetSkin.dll或者IrisSkin2.dll,這兩個文件是第三方開發(fā)設(shè)計的Winform界面美化的主要文件,2、打開VS2005,展開工具箱,右鍵點擊界面選擇“添加選項卡”,新建選項卡“皮膚”,Winform界面美化技巧,4、在工具箱項窗口點擊“瀏覽”,導(dǎo)入第三方動態(tài)鏈接庫文件DotNetSkin.dll或者Iris
56、Skin2.dll,兩個dll都是一樣的用,不同的是DotNetSkin.dll用的皮膚文件是*.skn,IrisSkin2.dll是用的*.ssk,3、在工具箱的新建選項卡“皮膚”里面單擊右鍵,選擇“選擇項”,將展開選擇工具箱項,Winform界面美化技巧,5、則在工具箱的皮膚選項卡內(nèi)將出現(xiàn)皮膚控件。,5、皮膚文件的基本用法是:拖拽任何一個皮膚控件到某個窗體上面,進(jìn)行如下的編碼:namespace porjectname{
57、 public partial class Form1 : Form { public Form1() { InitializeComponent(); this.skinEngine1.SkinFile = "*.ssk"; 或是 this.skinUI1.SkinFi
58、le = "*.skn"; } }},Winform界面美化技巧,namespace WindowsApplication1{ public partial class Form7 : Form { public Form7() {
59、 InitializeComponent(); //this.skinEngine1.SkinFile = "*.ssk"; // string path = Environment.CurrentDirectory + "\\skn皮膚\\LE4-DEFAULT.skn"; this.
60、skinUI1.SkinFile = path; }},有關(guān)ssk文件和skn文件庫,請參見附件文件庫中相關(guān)文件,總結(jié),WinForms可用于 Windows 窗體應(yīng)用程序開發(fā)Windows 窗體控件是從 System.Windows.Forms.Control 類派生的類標(biāo)簽控件用于顯示用戶不能編輯的文本或圖像按鈕控件提供用戶與應(yīng)用程序交互的最簡便方法組合框控件是列表框控件和文本框控件的組合,用戶可以鍵入文本,
61、也可以從所提供的列表中選擇項目窗體提供了收集、顯示和傳送信息的界面,是 GUI的重要元素消息框顯示消息,用于與用戶交互,--------------基于C#語言,主講教師:錢 哨本課學(xué)時:72課時聯(lián)系方式:qianshao@bjjtxy.bj.cn,Windows程序設(shè)計,回顧,WinForms可用于 Windows 窗體應(yīng)用程序開發(fā)Windows 窗體控件是從 System.Windows.Forms.Control
62、 類派生的類標(biāo)簽控件用于顯示用戶不能編輯的文本或圖像按鈕控件提供用戶與應(yīng)用程序交互的最簡便方法組合框控件是列表框控件和文本框控件的組合,用戶可以鍵入文本,也可以從所提供的列表中選擇項目窗體提供了收集、顯示和傳送信息的界面,是 GUI的重要元素消息框顯示消息,用于與用戶交互,第二章、WinForms控件,本章主要內(nèi)容介紹2.1 WinForms的高級控件2.2 單(多)文檔操作及菜單,CONTENT,,,,本
63、節(jié)學(xué)習(xí)目標(biāo):,使用WinForms中的高級控件單選按鈕圖片框選項卡控件滾動條 進(jìn)度條ImageList 控件ToolBar 控件StatusBar 控件Timer 控件TreeView 控件ListView 控件,2.1 Winforms的高級控件,2.1.1 單選按鈕(radioButton),Windows 窗體單選按鈕控件以組的形式使用單選按鈕允許用戶從多個選項中選擇一個選項,2.1.1 單選按鈕(r
64、adioButton),如何按功能分組 Windows 窗體 RadioButton 控件?,1、在一個容器(如 Panel 控件、GroupBox 控件或窗體)內(nèi)繪制單選按鈕即可將它們分組。2、若要添加不同的組,必須將它們放到面板或分組框中。,步驟:1、從“工具箱”的“Windows 窗體”選項卡中,將 GroupBox 或 Panel 控件拖到窗體上。2、在 GroupBox 或 Panel 控件上繪制 RadioButt
65、on 控件。 3、代碼見下。,2.1.2 圖片框,圖片框控件表示可用于顯示圖像的 Windows 圖片框控件,顯示位圖、元文件、圖標(biāo)、JPEG、GIF 或 PNG 等格式的圖形,是一種圖形顯示控件,圖片框,2.1.2 圖片框,練習(xí)1:使用設(shè)計器加載圖片(Windows 窗體) 練習(xí)2:運行時候修改圖片大小和位置,public Form1() { InitializeComponent();
66、 showpic(); } public void showpic() { pictureBox1.Image = Image.FromFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + @"\image.gif&quo
67、t;); pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; } private void pictureBox1_Click(object sender, EventArgs e) { if (pictureBox1.Image != nu
68、ll) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } else { showpic(); } },2.1.3 選項卡控件,在 Wind
69、ows 應(yīng)用程序中,選項卡用于將相關(guān)的控件集中在一起,放在一個頁面中選項卡控件用于顯示多個選項卡,其中每個選項卡均可包含圖片和其他控件選項卡相當(dāng)于另一個窗體,可以容納其他控件,選項卡控件,,選項卡控件的屬性,選項卡控件的屬性,設(shè)定選項卡的數(shù)目,,或者,綜合應(yīng)用程序示例(一),設(shè)定選項卡的提示信息,,總選項卡設(shè)計,分選項卡屬性設(shè)計,綜合應(yīng)用程序示例(二),統(tǒng)計頁面操作基本信息,private void tabControl1_Sele
70、ctedIndexChanged(object sender, EventArgs e) { label1.Text = "當(dāng)前操作統(tǒng)計信息為:頁面為第" + this.tabControl1.SelectedIndex.ToString() + "頁,選項卡頁為" + tabControl1.SelectedTab.Text + ",共有頁數(shù)&q
71、uot; + tabControl1.TabCount.ToString(); },綜合應(yīng)用程序示例(三),綜合應(yīng)用程序示例(四),使用窗體接受職員的個人信息和職業(yè)信息將使用單選按鈕、圖片框和選項卡控件應(yīng)用程序提供有兩個選項卡頁第一個選項卡頁顯示個人信息的文本框,綜合應(yīng)用程序示例(四),第二個選項卡頁顯示職員信息的文本框,綜合應(yīng)用程序示例(五),選項卡的基本操作,2.1.4 進(jìn)度條,用于指示操作的進(jìn)度、完成的百分
72、比外觀是排列在水平條中的一定數(shù)目的矩形,,,,,,,,,,,,,,,,,,,,,,,,,進(jìn)度條的屬性和方法,案例見下:,進(jìn)度條練習(xí)1:加載數(shù)據(jù)庫練習(xí),private void button1_Click(object sender, EventArgs e) { string sqlstring = "Data Source=(local);Initial Catalog=zrzx;U
73、ser ID=sa"; SqlConnection conn = new SqlConnection(sqlstring); string sql="select * from tbl_advice"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdap
74、ter adp = new SqlDataAdapter(); adp.SelectCommand = cmd; DataSet ds = new DataSet(); adp.Fill(ds); conn.Dispose(); conn.Close(); conn = null;
75、 label1.Visible = true; progressBar1.Visible = true; progressBar1.Minimum = 0; progressBar1.Maximum = ds.Tables[0].Rows.Count; progressBar1.BackColor = Color.Red;
76、 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { progressBar1.Value++; Application.DoEvents(); this.label1.Text = progressBar1.Value.ToString();
溫馨提示
- 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. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論