Lecture Set 10 Windows Controls and Forms Part A – Repetition Controls: List and Combo Boxes and DataViewGrids
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/2013 11:30 AM
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/2013 11:30 AM
Hierarchical Organization of the ListBox and ComboBox Controls 8/22/2013 11:30 AM
Sample Form with Five Control Types
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
The String Collection Editor Dialog Box 8/22/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
DataGridView Control Instance Displaying a Multiplication Table
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
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/2013 11:30 AM
1/16/2019 6:09 AM
Common Members of List Box and Combo Box Controls (continued) 1/16/2019 6:09 AM
1/16/2019 6:09 AM
Code to Loading Combo Boxes 1/16/2019 6:09 AM
Clearing and Reloading a Combo Box 1/16/2019 6:09 AM
Retrieving Information from a Combo Box 1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
Adding Forms 1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
DialogResults and Tags 1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
More on the Message Box Class 1/16/2019 6:09 AM
Displaying a Dialog Box 1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
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
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM
1/16/2019 6:09 AM