C# 程式设计.ppt
《C# 程式设计.ppt》由会员分享,可在线阅读,更多相关《C# 程式设计.ppt(82页珍藏版)》请在麦多课文档分享上搜索。
1、1,C#.NET 程式設計,第十一章 Windows Forms 控制項,2,11-1 Windows Forms 簡介,“Windows Forms” 是全新的視窗應用程式介面 “Web Forms” 與 “Web Services” 組成了全新的網際網路應用程式介面,.NET Framework 將之統稱為 “ASP.NET” 查看 System.Windows.Forms 命名空間提供的成員,3,11-2 設計階段的表單,11-2-1 建立表單 啟動 Visual Studio.NET。 點取 起始頁 的 新增專案 按鈕。 選擇 Visual C# 專案 類型及 Windows 應用程式
2、 範本,並輸入專案的名稱,然後按 確定。,4,11-2-2 設定表單的格線大小,格線可以幫助我們對齊控制項,如果要設定格線的大小或顯示與否,可以選取 工具 選項,然後點取 Windows Forms 設計工具,再依照下圖操作:,5,11-2-3 設定起始表單,在方案總管內的專案名稱按一下滑鼠右鍵,然後從快顯功能表中選取 屬性,再於如下對話方塊的 啟始物件 欄位選取該表單:,6,11-2-4 設定表單的屬性,只要選取表單,屬性視窗就會列出被選取之表單的常用屬性供您查看或修改,7,表單的外觀屬性 BackColor BackgroundImage Cursor ForeColor RightToL
3、eft Text Font FormBorderStyle,表單的視窗樣式屬性 ControlBox HelpButton Icon IsMdiContainer MaximizeBox Menu MaximizeBox Opacity ShowInTaskBar SizeGripStyle TopMost TransparencyKey,8,表單的配置方式:單一文件介面(SDI) 、多重文件介面(MDI) 、總管樣式介面(Explorer-Style Interface) 表單的配置屬性 AutoScale AutoScroll AutoScrollMargin AutoScrollMinS
4、ize DockPadding Location MaximumSize MinimumSize Size StartPosition WindowState,表單的行為屬性 AllowDrop ContextMenu Enabled ImeMode 表單的其他屬性 AcceptButton CancelButton KeyPreview Name,9,10,執行階段的表單就像所有物件,會有建立與終止的時候。 相關的控制項事件:,11-3 執行階段的表單,11,11-4-1 TextBox (文字方塊) 文字方塊的外觀屬性 BackColor BorderStyle Cursor Font F
5、oreColor Lines RightToLeft ScrollBars Text TextAlign,11-4 文字編輯控制項,12,文字方塊的行為屬性 AcceptsReturn AcceptsTab AllowDrop AutoSize CharacterCasting ContextMenu Enabled HideSelection ImeMode MaxLength Multiline PasswordChar ReadOnly TabIndex TabStop Visible WordWrap,文字方塊的配置屬性 Anchor Dock Location Size Name,1
6、3,11-4-2 RichTextBox,RichTextBox 的屬性 AutoWordSelection BulletIndent DetectUrls ShowSelectionMarginZoomFactor,14,RichTextBox 的格式化屬性 SelectedText SelectionAlignment SelectionBullet SelectionCharOffset SelectionColor SelectionFont SelectionIndent SelectionHangingIndent SelectionRightIndent SelectionSta
7、rt SelectionLength SelectionProtected SelectionType SelectionTabs,15,private void richTextBox1_Enter(object sender, System.EventArgs e) richTextBox1.SelectionBullet = true;richTextBox1.BulletIndent = 10; private void richTextBox2_Enter(object sender, System.EventArgs e) richTextBox2.SelectionHanging
8、Indent = 15; ,16,11-5-1 Button (按鈕) Button 的外觀屬性 BackColor BackgroundImage Cursor FlatStyle Font ForeColor Image ImageAlign ImageIndex ImageList RightToLeft Text TextAlign,11-5 命令控制項,17,Button 的行為屬性 AllowDrop ContextMenu DialogResult Enabled TabIndex TabStop Visible Name,18,11-5-2 NotifyIcon (告知圖示),
9、比較重要的屬性有 Icon 、Visible 、Text,19,11-5-3 ToolBar (工具列),ToolBar 的外觀屬性 BorderStyle ButtonSize Divider DropDownArrows Font TextAlign,20,Toolbar 的行為屬性 AllowDrop Appearance AutoSize Buttons ContextMenu Enabled ImageList ShowToolTips TabIndex TabStop Visible Wrappable Name,21,22,讓程式根據使用者點取第幾個工具列按鈕,秀出對應的對話方塊
10、:,private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) switch(toolBar1.Buttons.IndexOf(e.Button)case 0:MessageBox.Show(“您按下的是第一個按鈕“); break;case 1:MessageBox.Show(“您按下的是第二個按鈕“);break;case 2:MessageBox.Show(“您按下的是第三個按鈕“);break;case 3:MessageBox.Show(“您按
11、下的是第四個按鈕“); break;case 4:MessageBox.Show(“您按下的是第五個按鈕“);break; ,23,11-6 文字顯示控制項,11-6-1 Label (標籤) Label 的外觀屬性 BackColor BorderStyle Cursor FlatStyle ForeColor Font Image ImageAlign ImageIndex ImageList RightToLeft Text TextAlign UseMnemonic,24,Label 的行為屬性 AllowDrop AutoSize ContextMenu Enabled TabInd
12、ex Visible Name,第一個 Label 將第一個 TextBox 的快速存取鍵設定為 Alt + 1,也就是當使用者按下 Alt 鍵加上第一個 Label 內有底線的字元 1 時,就會將焦點移至第一個 TextBox;同理,第二個 Label 將第二個 TextBox 的快速存取鍵設定為 Alt + 2。,25,11-6-2 LinkLabel (超連結標籤),ActiveLinkColor DiabledLinkColor LinkColor LinkVisited VisitedLinkColor LinkBehavior LinkArea,26,private void li
13、nkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) / 將 LinkVisited 屬性設定為 true, 將使藍色的超連結變成已瀏覽的超連結色彩紫色linkLabel1.LinkVisited = true;/ 呼叫 Process.Start() 方法以預設的瀏覽器開啟指定的網址System.Diagnostics.Process.Start(“http:/ private void linkLabel2_LinkClicked(object sender, Sy
14、stem.Windows.Forms.LinkLabelLinkClickedEventArgs e) Form Form2 = new Form(); / 建立另一個表單Form2.Show(); / 呼叫 Show() 方法將表單顯示出來linkLabel2.LinkVisited = true; ,27,11-6-3 StatusBar (狀態列),StatusBar 的外觀屬性 Cursor Font Panels RightToLeft SizingGrip Text,StatusBar 的行為屬性 AllowDrop ContextMenu Enabled ShowPanels T
15、abIndex TabStop Visible Name,28,private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e) switch(statusBar1.Panels.IndexOf(e.StatusBarPanel)case 0:MessageBox.Show(“您點取的是第一個面板“); /您可以在此定義其它動作break;case 1:MessageBox.Show(“您點取的是第二個面板“); /您可以在此定義其它動作break; ,2
16、9,11-7 圖片控制項,11-7-1 PictureBox (圖片方塊)PictureBox 的外觀屬性 BackColor Background Image BorderStyle Cursor Image PictureBox 的行為屬性 ContextMenu Enabled SizeMode Visible Name,30,private void pictureBox1_Click(object sender, System.EventArgs e) pictureBox3.Visible = true; / 顯示第三個 PictureBox 控制項的圖片 ,private voi
17、d pictureBox2_Click(object sender, System.EventArgs e) pictureBox3.Visible = false; / 不顯示第三個 PictureBox 控制項的圖片 ,31,11-7-2 ImageList (圖片清單),ImageList 控制項可以用來儲存圖片清單,以便稍後經由其它控制項顯示,例如 ToolBar、ListView、TreeView、TabControl、Button、CheckBox、RadioButton 和 Label 等控制項都可以搭配 ImageList 控制項來儲存圖片清單。,32,11-8 清單控制項,1
18、1-8-1 CheckBox (核取方塊) 屬性 Appearance CheckAlign Checked CheckState AutoCheck ThreeState,33,private void button1_Click(object sender, System.EventArgs e) string Prefer = “您喜愛的早餐有 “;if (checkBox1.Checked = true) Prefer = Prefer + “三明治 “;if (checkBox2.Checked = true) Prefer = Prefer + “潛水艇 “;if (checkBo
19、x3.Checked = true) Prefer = Prefer + “燒餅 “;if (checkBox4.Checked = true) Prefer = Prefer + “飯糰 “;if (checkBox5.Checked = true) Prefer = Prefer + “羅蔔糕 “;MessageBox.Show(Prefer); ,34,11-8-2 RadioButton (選項按鈕),private void button1_Click(object sender, System.EventArgs e) string Prefer = “您喜愛的早餐有 “;if (
20、radioButton1.Checked = true) Prefer = Prefer + “三明治 “;if (radioButton2.Checked = true) Prefer = Prefer + “潛水艇 “;if (radioButton3.Checked = true) Prefer = Prefer + “燒餅 “;if (radioButton4.Checked = true) Prefer = Prefer + “飯糰 “;if (radioButton5.Checked = true) Prefer = Prefer + “羅蔔糕 “;MessageBox.Show(
21、Prefer); ,35,11-8-3 ListBox (清單方塊),屬性 ColumnWidth DrawMode HorizontalExtent HorizontalScrollBar IntegralHeight ItemHeight MultiColumn ScrollAlwaysVisible SelectedIndex SelectedIndices SelectedItem SelectedItems SelectionMode Sorted,36,private void button1_Click(object sender, System.EventArgs e) Mes
22、sageBox.Show(“這個清單方塊內總共有“ + listBox1.Items.Count + “個項目n“ + “您所選擇的最高學歷為“ + listBox1.SelectedItem); ,37,private void button1_Click(object sender, System.EventArgs e) string Msg = “這個清單方塊內總共有“ + listBox1.Items.Count + “個項目n“ + “您所選擇的休閒活動為“;if (listBox1.SelectedItems.Count != 0) for(int i = 0; i listBo
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程式 设计 PPT
