Bài giảng C# 2010 - Chapter 2: Windows Form and Windows Forms Controls (Part 2)

public class CStudent

 { private string m_strID;

 private string m_strName;

 public CStudent(string strID,

 string strName)

 { this.m_strID = strID;

 this.m_strName = strName;

 }

 public string ID

 { get { return this.m_strID; }

 set { this.m_strID = value; }

 }

 public string Name

 { get { return this.m_strName; }

 set { this.m_strName = value; }

 }

 }

 

pptx56 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 1920 | Lượt tải: 5download
Tóm tắt nội dung Bài giảng C# 2010 - Chapter 2: Windows Form and Windows Forms Controls (Part 2), để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13/05/2013 ‹#› DateTimePicker & MonthCalendar Name Description Format Gets or sets the format of the date and time displayed in the control CustomFormat Gets or sets the custom date/time format string Value Gets or sets the date/time value assigned to the control. dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"; Next Slide to see list Custom Format Format string Description d The one- or two-digit day. dd The two-digit day. Single-digit day values are preceded by a 0. ddd The three-character day-of-week abbreviation. dddd The full day-of-week name. h The one- or two-digit hour in 12-hour format. hh The two-digit hour in 12-hour format. Single digit values are preceded by a 0. H The one- or two-digit hour in 24-hour format. Format string Description HH The two-digit hour in 24-hour format. Single digit values are preceded by a 0. m The one- or two-digit minute. mm The two-digit minute. Single digit values are preceded by a 0. M The one- or two-digit month number. mm The two-digit minute. Single digit values are preceded by a 0. M The one- or two-digit month number. MM The two-digit month number. Single digit values are preceded by a 0. MMM The three-character month abbreviation. Format string Description MMMM The full month name. s The one- or two-digit seconds. ss The two-digit seconds. Single digit values are preceded by a 0. t The one-letter A.M./P.M. abbreviation (A.M. is displayed as "A"). tt The two-letter A.M./P.M. abbreviation (A.M. is displayed as "AM"). y The one-digit year (2001 is displayed as "1"). yy The last two digits of the year (2001 is displayed as "01"). yyyy The full year (2001 is displayed as "2001"). private void frmListBox_Load(object sender, EventArgs e) { listBox1.Items.Clear(); for (int i = 0; i < 10; i++) listBox1.Items.Add("Item " + i); } private void listBox1_SelectedIndexChanged (object sender, EventArgs e) { lblMessage.Text = listBox1.Text +" was clicked!"; } ListBox And We can use AddRange method to add data: private void frmListBox_Load(object sender, EventArgs e) { string[] strArr = new string[] { "Tèo","Tí","Bin","Bo"}; listBox1.Items.AddRange(strArr); } public class CStudent { private string m_strID; private string m_strName; public CStudent(string strID, 	string strName) { this.m_strID = strID; this.m_strName = strName; } public string ID { get { return this.m_strID; } set { this.m_strID = value; } } public string Name { get { return this.m_strName; } set { this.m_strName = value; } } } using System.Collections; ArrayList arr = new ArrayList(); for(int i=0;i<10;i++) {arr.Add(new CStudent("ID_"+i,"Name "+i)); } listBox1.DataSource = arr; listBox1.ValueMember = "ID"; listBox1.DisplayMember = "Name"; Also We can use DataSource to display data To get object from Listbox, We could use coding below: if (listBox1.SelectedItem != null) { CStudent svTeo = (CStudent ) 	listBox1.SelectedItem; lblMessage.Text = aStudent.Name 	+ " Was selected"; } CheckedListBox chklbLeft chklbRight btnAdd btnAddAll btnRemove btnRemoveAll Use Items to add data private void frmCheckListBox_Load (object sender, EventArgs e) { chklbLeft.Items.Add("Tèo"); 	chklbLeft.Items.Add("Tí"); 	chklbLeft.Items.Add("Bin"); 	chklbLeft.Items.Add("Bo"); } Or we could use AddRange chklbLeft.Items.AddRange(new string[] { "Tèo","Tí","Bin","Bo"}); How to process Items Checked??? Case 1: CheckedListBox.CheckedIndexCollection indexCollection = chklbLeft.CheckedIndices; string strChecked = ""; foreach (int i in indexCollection) { strChecked += i + ";"; } MessageBox.Show(strChecked); How to process Items Checked??? Case 2: CheckedListBox.CheckedItemCollection items = chklbLeft.CheckedItems; string strChecked = ""; foreach (string s in items) { strChecked += s + ";"; } MessageBox.Show(strChecked); How to process Items Checked??? Case 3: string strChecked = ""; for (int i = 0; i < chklbLeft.Items.Count; i++){ if (chklbLeft.GetItemChecked(i)) { //Process Item checked here } } Go back ChecklistBox Example: private void btnAdd_Click (object sender, EventArgs e) { foreach(int i in chklbLeft.CheckedIndices) { chklbRight.Items.Add(chklbLeft.Items[i]); } foreach (string s in chklbRight.Items) {chklbLeft.Items.Remove(s);} } private void btnAddAll_Click (object sender, EventArgs e) { 	chklbRight.Items.AddRange 	(chklbLeft.Items); 	chklbLeft.Items.Clear(); } private void btnRemove_Click (object sender, EventArgs e) { foreach (string s in 	chklbRight.CheckedItems) 	chklbLeft.Items.Add(s); foreach(string s in 	chklbLeft.Items) 	chklbRight.Items.Remove(s); } private void btnRemoveAll_Click (object sender, EventArgs e) { chklbLeft.Items.AddRange 	(chklbRight.Items); chklbRight.Items.Clear(); } Menu (2 ways to use) MainMenu MenuStrip MenuItem ToolStripMenuItem Menu Property MainMenuStrip Property I would like to tell you : using MainMenu is the basic Menu. In this case, I will demo add Menu at Runtime for you. Please see next slide ! Coding to create Menu at Runtime private MainMenu mainMenuBar; private MenuItem menuFile, menuEdit, menuFileNew, menuFileOpen, menuFileExit, menuEditCut, menuEditCopy, menuEditPaste; private void createMenu() { mainMenuBar = new MainMenu(); this.Menu = mainMenuBar; menuFile=new MenuItem("File"); menuFileNew = new MenuItem("New"); menuFileOpen = new MenuItem("Open"); menuFileExit = new MenuItem("Exit"); menuFile.MenuItems.Add(menuFileNew); menuFile.MenuItems.Add(menuFileOpen); private void createMenu(){…… menuFile.MenuItems.Add("-"); menuFile.MenuItems.Add(menuFileExit); mainMenuBar.MenuItems.Add(menuFile); menuEdit = new MenuItem("Edit"); menuEditCut = new MenuItem("Cut"); menuEditCopy = new MenuItem("Copy"); menuEditPaste = new MenuItem("Paste"); menuEdit.MenuItems.AddRange(new MenuItem[] { menuEditCut,menuEditCopy,menuEditPaste}); mainMenuBar.MenuItems.Add(menuEdit); attachEvents();} private void attachEvents() { menuFileNew.Click += process_MenuClick; menuFileOpen.Click += process_MenuClick; menuFileExit.Click += process_MenuClick; menuEditCut.Click += process_MenuClick; menuEditCopy.Click += process_MenuClick; menuEditPaste.Click += process_MenuClick; } private void process_MenuClick (object sender, EventArgs e) { if (sender.Equals(menuFileExit)) { Application.Exit(); } } private void frmMenuBasic_Load (object sender, EventArgs e) {createMenu(); } MenuStrip Designer Click on button to add MenuItem Click Add button to add new MenuItem Text to Display Click here to add Sub MenuItem MenuItem’s Name Image Icon Text to Display Click here to add Sub MenuItem MenuItem’s Name We could add MenuItem direct on the Windows Form. Enter Name in the “Type Here” Add Event for each MenuItem Image List Drag & Drop Gets the ImageList. ImageCollection for this image list. See Next Slide Gets or sets the size of the images in the image list Gets the color depth of the image list Click Add button to insert Image into Collection Select image & click Open MenuStrip At Runtime private MenuStrip menuBar; private ToolStripMenuItem menuFile, menuEdit, menuFileNew, menuFileOpen, menuFileExit, menuEditCut, menuEditCopy, menuEditPaste; private void createMenu() {menuBar = new MenuStrip(); menuBar.Font = new Font("arial", 36, FontStyle.Bold, GraphicsUnit.Pixel); this.MainMenuStrip = menuBar; menuFile = new ToolStripMenuItem("File"); menuFileNew = new ToolStripMenuItem("New"); menuFileNew.Image = imageList1.Images[0]; menuFileOpen = new ToolStripMenuItem("Open"); ToolStripSeparator sp = new ToolStripSeparator(); menuFileExit = new ToolStripMenuItem("Exit"); menuFileExit.Image = imageList1.Images[1]; menuFile.DropDownItems.Add( menuFileNew); menuFile.DropDownItems.Add( menuFileOpen); menuFile.DropDownItems.Add(sp); menuFile.DropDownItems.Add( menuFileExit); menuEdit = new ToolStripMenuItem("Edit"); menuEditCut = new ToolStripMenuItem("Cut"); menuEditCopy = new ToolStripMenuItem("Copy"); menuEditPaste = new ToolStripMenuItem("Paste"); menuEdit.DropDownItems.AddRange(new ToolStripItem[] { menuEditCut,menuEditCopy, menuEditPaste}); menuBar.Items.AddRange(new ToolStripItem[] { menuFile,menuEdit}); this.Controls.Add(menuBar); attachEvents(); } private void attachEvents() {menuFileExit.Click += processClick; } private void processClick (object o, EventArgs e) { if (o.Equals(menuFileExit)) Application.Exit(); } private void frmMenuStrip_Load (object sender, EventArgs e) { createMenu(); } ContextMenuStrip Drag & Drop into the FORM Click Items (Collection) to add MenuItem ImageScalingSize to set Width, Height Icon As the same MenuStrip 1.Choose button 2.Set ContextMenuStrip 3.Attach event as the same MenuStrip ToolStripContainer & ToolStrip ToolStripContainer ToolStripContainer Provides panels on each side of the form and a central panel that can hold one or more controls Drag & Drop ToolStrip into the Form As the same MenuStrip Add Items Collection for ToolBar Attach Events for each Item Exercise: (for Student) Coding ContextMenuStrip & ToolStrip at Runtime END 

File đính kèm:

  • pptxBài giảng C# 2010 - Chapter 2 Windows Form and Windows Forms Controls (Part 2).pptx