Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 27 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
27
Dung lượng
692 KB
Nội dung
Week 2 WindowsControls(Chapter1-4) Contents Adding control and event at run-time (p.25+p.188) Sharing event handler (p.183) ImageList component (p.128) ListView control (p.108) Slide 2 Adding control at run-time (p.25) Steps: Instantiate a new control Set some properties of the control Name property Location property Text property … Add it to the Controls collection of the form or the container (GroupBox,Panel, FlowLayoutPanel,…) Slide 3 Adding control at run-time: Example Button aButton = new Button(); aButton.Location = new Point(20,20); aButton.Text = "Test Button"; this.Controls.Add(aButton); // add button to form // panel1.Controls.Add(aButton); // add button to panel Exercise 3 – Module 3 Slide 4 Creating event handlers at run- time (p.188) Steps: Create a method whose signature matches the signature for the event Use the += operator to associate the method with the event Example: private void EventMethod( object sender, EventArgs e ) { MessageBox.Show("aaaa"); } … aButton.Click += new EventHandler(EventMethod); Remove an event handler by using the =- operator Slide 5 Exercise 3 – Module 3 Sharing event handler (p.183) Sharing event handler for case: Multi-controls use the same event handler At design-time: See near figure Exercise 2,6 – Module 3 At run-time: Write code: use += Exercise 5 – Module 3 Slide 6 Sharing event handler: Example Example: Create 10 button at run-time with random numbers When the user click a button, a message box displays appropriately Exercise 5 – Module 3 Slide 7 ImageList component (p.128) ImageList is a component that allows you to organize groups of images ImageList supplies images to other controls, such as ListView, TreeView control Some important properties: Images ImageSize To access the images contained in the ImageList PictureBox1.Image = ImageList1.Images[0]; Exercise 5 – Module 3 (rotate moon), at home Slide 8 ListView control (p.108) Column Header Items SubItems Use to displays list of items Slide 9 Properties of ListView control Slide 10 Properties Description Columns Contains the collection of columns to be displayed when the View property is set to Details FullRowSelect Indicating whether the user can select an entire row GridLines Indicating whether grid lines between items and subitems are drawn HideSelection Indicating whether the selected items to be hide when lost focus [...]... item.Text = textBox1.Text; item.SubItems [1] .Text = textBox2.Text; item.SubItems[2].Text = textBox3.Text; } } Slide 20 Display selected item Display the first selected item: private void listview1_SelectedIndexChanged(…) { if (listView1.SelectedItems.Count > 0) { ListViewItem item = listView1.SelectedItems[0]; TextBox1.Text = item.Text; TextBox2.Text = item.SubItems [1] .Text; Label1.Text = item.SubItems[2].Text;... row].SubItems[int col] Slide 14 Using image icon in ListView Steps to using icon for ListViewItem: Add icons to ImageList control Set the SmallImageList, LargeImageList of ListView control With each item added to the ListView, set ImageIndex property of that item Example: Page 11 0: create icon at design-time Slide 15 Example ListView Create data for Listview at run-time Slide 16 Some operations... listView1.Items[5].Selected = true; listView1.Items[5].EnsureVisible(); listView1.Focus(); Unselect the selected items: listView1.SelectedItems.Clear(); Slide 24 Example: Search and select items found bool found = false; listView1.SelectedItems.Clear(); foreach (ListViewItem item in listView1.Items) if (item.Text == sFind) { found = true; item.Selected = true; item.EnsureVisible(); listView1.Focus();... 26 Using Group in ListView: Example ListViewGroup nhom1 = new ListViewGroup(); nhom1.Header = "Group 1" ; nhom1.HeaderAlignment = HorizontalAlignment.Left; listView1.Groups.Add(nhom1); ListViewItem it = listView1.Items.Add( "12 345"); it.SubItems.Add("Jack"); it.SubItems.Add("Ca"); it.Group = nhom1; Slide 27 ... text-based header followed by a horizontal line and the items assigned to that group The header text can be aligned to the left, right, or center To use Group 1 2 Add ListViewGroup objects to the ListView.Groups collection Assign a ListView Item by setting the ListViewItem.Group property Slide 26 Using Group in ListView: Example ListViewGroup nhom1 = new ListViewGroup(); nhom1.Header = "Group 1" ;... ListView Slide 17 Add item private void AddItem( ListView lvw ) { if (CheckValidData()) { ListViewItem it = ListView1.Items.Add(txt1.Text); it.SubItems.Add(txt2.Text); it.SubItems.Add(txt3.Text); it.ImageIndex = 0; } } Slide 18 Delete selected items private void DeleteSelectedItems( ListView lvw ) { foreach (ListViewItem item in lvw.SelectedItems) { lvw.Items.Remove(item); } } Slide 19 Edit selected... Determines whether the groups contained in the Groups collection are shown Slide 12 Default event in ListView control SelectedIndexChanged event: occurs when current index in ListView changes private void ListView1_SelectedIndexChanged(…) { foreach (ListViewItem it in ListView1.SelectedItems) { // process it } } Slide 13 Methods, properties of ListView’s Items Some methods, properties of Items collection:... listView1.SelectedItems[0]; TextBox1.Text = item.Text; TextBox2.Text = item.SubItems [1] .Text; Label1.Text = item.SubItems[2].Text; } } Slide 21 Searching in ListView Search on Text of ListViewItem Search on SubItems bool found = false; foreach (ListViewItem item in listView1.Items) if (find match with item) found = true; if (found == false) MessageBox.Show(“Not found”); Slide 22 Example: Searching in ListView... in which ListView items are displayed LargeImageList The ImageList component from which images for ListViewItems are drawn SmallImageList when the View property is set to LargeIcon or SmallIcon Slide 11 Properties of ListView control (cont.) Properties SelectedItems Description Get selected items, have the type SelectedListViewItemCollection Items[int index] Return an item at the index position, have