Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Set 10 Windows Controls and Forms

Similar presentations


Presentation on theme: "Lecture Set 10 Windows Controls and Forms"— Presentation transcript:

1 Lecture Set 10 Windows Controls and Forms
Part A – Repetition Controls: List and Combo Boxes and DataViewGrids

2 Objectives To understand the mechanics and use of the list boxes and combo boxes both of which are repetition based controls Understand how the DataViewGrid control works To be able to design and code a form using any of the controls introduced to date. 8/22/ :30 AM

3 Objectives (continued)
Given a form with two or more controls, set the tab order of the controls Given the specifications for an application that displays custom or standard dialog boxes, design and code the application Describe how you can use the DialogResult enumeration and the Tag property to pass data between a form and a custom dialog box. Describe how you can use the FormClosing event to stop a form from closing. 8/22/ :30 AM

4 Hierarchical Organization of the ListBox and ComboBox Controls
8/22/ :30 AM

5 Sample Form with Five Control Types

6 The System.Windows. Forms.ListControl class
The Items property is a collection Each object is an item in the list The SelectedIndex property contains the 0-based index value of the selected item The value is -1 if no item is selected The SelectedItem property contains a reference to the selected item itself The value is Nothing of no item is selected The ClearSelection method deselects all selected items Windows fires the SelectedIndexChanged event when an item is selected 8/22/ :30 AM

7 Introduction to the ComboBox Control
The ComboBox control displays a list of items from which the end user selects one item The DropDownStyle property defines the visual appearance of the ComboBox DropDown Simple DropDownList 8/22/ :30 AM

8 Introduction to the ListBox Control
The ListBox control is similar to the ComboBox control Except -- The visible region does not drop down The SelectedItems property returns a collection of the selected items The SelectionMode property defines whether the user can select one item or many items The ClearSelected method clears the selected items 8/22/ :30 AM

9 Working with the ComboBox and ListBox Controls
Common operations Items must be added to the list Conditional actions must be performed as the end user selects list items Items must be selectable programmatically 8/22/ :30 AM

10 Adding Items to a ComboBox or ListBox
Items can be added at design time using the String Collection Editor Items can be added at run time by calling the Add method as follows: for (int count = 1; count <= 20; count++) { lstDemo.Add("Item " + count.ToString()); } // end for 8/22/ :30 AM

11 The String Collection Editor Dialog Box
8/22/ :30 AM

12 Working with the Selected Item
When the end user selects an item, a SelectedIndexChanged event fires Two properties are of interest The SelectedItem property references the object currently selected The SelectedIndex property stores the 0-based index of the currently selected item 8/22/ :30 AM

13 Working with the Selected Item (Example)
Display information about the selected item private void cboSingleSelect_SelectedIndexChanged (object sender, System.EventArgs e) { lblSelectedComboBoxItem.Text = cboSingleSelect.SelectedItem.ToString(); } // end SelectIndexChanged 8/22/ :30 AM

14 Selecting an Item Programmatically (Examples)
Select the first item in the combo box named cboSingleSelect cboSingleSelect.SelectedIndex = 0; Clear the selected item cboSingleSelect.SelectedIndex = -1; 8/22/ :30 AM

15 The DataGridView Control
The DataGridView control displays data as a 2-dimensional grid made up of rows and columns It is very much like a special case of a 2-D array The intersection of a row and column is called a cell It can operate in two modes The DataGridView can be bound to a data source It can operate in an unbound mode Rows and columns are added manually 8/22/ :30 AM

16 DataGridView Control Instance Displaying a Multiplication Table

17 Working with the DataGridView Control
Steps to populate the grid Add the columns Add the rows The columns must be added before the rows are added Populate the cells It's possible to delete existing rows 8/22/ :30 AM

18 Adding Columns to the DataGridView
The Add method of the Columns collection adds a column as follows: dgvDemo.Columns.Add("Column1", "VisibleText"); The first argument contains the column name The second argument contains the column's caption Columns are added to the end of the list 8/22/ :30 AM

19 Adding Rows to the DataGridView
Without arguments, the Add method adds one row dgvDemo.Rows.Add(); The Add method will add multiple rows when called with an Integer argument dgvDemo.Rows.Add(20); 8/22/ :30 AM

20 Referencing and Populating Individual Grid Cells
The following statements reference the first cell in the first row: DataGridViewCell CurrentCell = dgvDemo.Rows(0).Cells(0); dgvDemo.Rows(0) references the first row in the collection Cells(0) references the first cell in the first row The data type of a cell is DataGridViewCell 8/22/ :30 AM

21 Storing and Retrieving Cell Values
The Value property of the DataGridViewCell stores the cell's value Example: CurrentCell.Value = 42; txtDemo.Text = currentCell.Value.ToString(); 8/22/ :30 AM

22 Deleting Rows from the DataGridView
Like most collections, the RemoveAt method removes a row The argument contains the 0-based index value Example to remove the first row: dgvDemo.Rows.RemoveAt(0); 8/22/ :30 AM

23 Using a Loop to Examine Cells
The Rows collection has a Count property containing the number of elements (rows) Thus, a For loop can be used to examine each row Examine the first column (cell) in each row double total = 0; for (int currentRow = 0; currentRow < gvDemo.Rows.Count; currentRow++) { total += dgvDemo.Rows(currentRow).Cells(0).Value; } // end for 8/22/ :30 AM

24 1/16/2019 6:09 AM

25 Common Members of List Box and Combo Box Controls (continued)
1/16/2019 6:09 AM

26 1/16/2019 6:09 AM

27 Code to Loading Combo Boxes
1/16/2019 6:09 AM

28 Clearing and Reloading a Combo Box
1/16/2019 6:09 AM

29 Retrieving Information from a Combo Box
1/16/2019 6:09 AM

30 1/16/2019 6:09 AM

31 1/16/2019 6:09 AM

32 1/16/2019 6:09 AM

33 1/16/2019 6:09 AM

34 1/16/2019 6:09 AM

35 1/16/2019 6:09 AM

36 1/16/2019 6:09 AM

37 Adding Forms 1/16/2019 6:09 AM

38 1/16/2019 6:09 AM

39 1/16/2019 6:09 AM

40 1/16/2019 6:09 AM

41 1/16/2019 6:09 AM

42 1/16/2019 6:09 AM

43 1/16/2019 6:09 AM

44 DialogResults and Tags
1/16/2019 6:09 AM

45 1/16/2019 6:09 AM

46 1/16/2019 6:09 AM

47 More on the Message Box Class
1/16/2019 6:09 AM

48 Displaying a Dialog Box
1/16/2019 6:09 AM

49 1/16/2019 6:09 AM

50 1/16/2019 6:09 AM

51 1/16/2019 6:09 AM

52 1/16/2019 6:09 AM

53 1/16/2019 6:09 AM

54 1/16/2019 6:09 AM

55 1/16/2019 6:09 AM

56 1/16/2019 6:09 AM

57 1/16/2019 6:09 AM

58 Code for the Customer Form (continued)
private void SaveData() { cboNames.SelectedIndex = -1; lblPayment.Text = "“; isDataSaved = true; cboNames.Select(); } // end SaveData 1/16/2019 6:09 AM

59 1/16/2019 6:09 AM

60 1/16/2019 6:09 AM

61 1/16/2019 6:09 AM

62 1/16/2019 6:09 AM

63 1/16/2019 6:09 AM

64 1/16/2019 6:09 AM

65 1/16/2019 6:09 AM

66 1/16/2019 6:09 AM

67 1/16/2019 6:09 AM


Download ppt "Lecture Set 10 Windows Controls and Forms"

Similar presentations


Ads by Google