Lecture Set 10 Windows Controls and Forms Part

  • Slides: 67
Download presentation
Lecture Set 10 Windows Controls and Forms Part A – Repetition Controls: List and

Lecture Set 10 Windows Controls and Forms Part A – Repetition Controls: List and Combo Boxes and Data. View. Grids

Objectives n n n Slide 2 To understand the mechanics and use of the

Objectives n n n Slide 2 To understand the mechanics and use of the list boxes and combo boxes both of which are repetition based controls Understand how the Data. View. Grid 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) n n Slide 3 Given a form with two or more controls,

Objectives (continued) n n Slide 3 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 Dialog. Result enumeration and the Tag property to pass data between a form and a custom dialog box. Describe how you can use the Form. Closing event to stop a form from closing. 8/22/2013 11: 30 AM

Hierarchical Organization of the List. Box and Combo. Box Controls Slide 4 8/22/2013 11:

Hierarchical Organization of the List. Box and Combo. Box Controls Slide 4 8/22/2013 11: 30 AM

Sample Form with Five Control Types Slide 5 6/14/2021 9: 23 AM

Sample Form with Five Control Types Slide 5 6/14/2021 9: 23 AM

The System. Windows. Forms. List. Control class n n n Slide 6 The Items

The System. Windows. Forms. List. Control class n n n Slide 6 The Items property is a collection n Each object is an item in the list n The value is -1 if no item is selected n The value is Nothing of no item is selected The Selected. Index property contains the 0 -based index value of the selected item The Selected. Item property contains a reference to the selected item itself The Clear. Selection method deselects all selected items Windows fires the Selected. Index. Changed event when an item is selected 8/22/2013 11: 30 AM

Introduction to the Combo. Box Control n n The Combo. Box control displays a

Introduction to the Combo. Box Control n n The Combo. Box control displays a list of items from which the end user selects one item The Drop. Down. Style property defines the visual appearance of the Combo. Box n n n Slide 7 Drop. Down Simple Drop. Down. List 8/22/2013 11: 30 AM

Introduction to the List. Box Control n The List. Box control is similar to

Introduction to the List. Box Control n The List. Box control is similar to the Combo. Box control n n Slide 8 Except -- The visible region does not drop down The Selected. Items property returns a collection of the selected items The Selection. Mode property defines whether the user can select one item or many items The Clear. Selected method clears the selected items 8/22/2013 11: 30 AM

Working with the Combo. Box and List. Box Controls n Common operations n n

Working with the Combo. Box and List. Box Controls n Common operations n n n Slide 9 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 Combo. Box or List. Box n n Items can be

Adding Items to a Combo. Box or List. Box n n 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++) { lst. Demo. Add("Item " + count. To. String()); } // end for Slide 10 8/22/2013 11: 30 AM

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

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

Working with the Selected Item n n When the end user selects an item,

Working with the Selected Item n n When the end user selects an item, a Selected. Index. Changed event fires Two properties are of interest n n Slide 12 The Selected. Item property references the object currently selected The Selected. Index property stores the 0 -based index of the currently selected item 8/22/2013 11: 30 AM

Working with the Selected Item n (Example) Display information about the selected item private

Working with the Selected Item n (Example) Display information about the selected item private void cbo. Single. Select_Selected. Index. Changed (object sender, System. Event. Args e) { lbl. Selected. Combo. Box. Item. Text = cbo. Single. Selected. Item. To. String(); } // end Select. Index. Changed Slide 13 8/22/2013 11: 30 AM

Selecting an Item Programmatically (Examples) n Select the first item in the combo box

Selecting an Item Programmatically (Examples) n Select the first item in the combo box named cbo. Single. Selected. Index = 0; n Clear the selected item cbo. Single. Selected. Index = -1; Slide 14 8/22/2013 11: 30 AM

The Data. Grid. View Control n n The Data. Grid. View control displays data

The Data. Grid. View Control n n The Data. Grid. View 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 n n The intersection of a row and column is called a cell It can operate in two modes n n The Data. Grid. View can be bound to a data source It can operate in an unbound mode n Slide 15 Rows and columns are added manually 8/22/2013 11: 30 AM

Data. Grid. View Control Instance Displaying a Multiplication Table Slide 16 8/22/2013 11: 30

Data. Grid. View Control Instance Displaying a Multiplication Table Slide 16 8/22/2013 11: 30 AM

Working with the Data. Grid. View Control n Steps to populate the grid n

Working with the Data. Grid. View Control n Steps to populate the grid n n Add the columns Add the rows n n n Slide 17 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 Data. Grid. View n The Add method of the Columns

Adding Columns to the Data. Grid. View n The Add method of the Columns collection adds a column as follows: dgv. Demo. Columns. Add("Column 1", "Visible. Text"); n n n Slide 18 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 Data. Grid. View n Without arguments, the Add method adds

Adding Rows to the Data. Grid. View n Without arguments, the Add method adds one row dgv. Demo. Rows. Add(); n The Add method will add multiple rows when called with an Integer argument dgv. Demo. Rows. Add(20); Slide 19 8/22/2013 11: 30 AM

Referencing and Populating Individual Grid Cells n The following statements reference the first cell

Referencing and Populating Individual Grid Cells n The following statements reference the first cell in the first row: Data. Grid. View. Cell Current. Cell = dgv. Demo. Rows(0). Cells(0); n n n Slide 20 dgv. Demo. 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 Data. Grid. View. Cell 8/22/2013 11: 30 AM

Storing and Retrieving Cell Values n n The Value property of the Data. Grid.

Storing and Retrieving Cell Values n n The Value property of the Data. Grid. View. Cell stores the cell's value Example: Current. Cell. Value = 42; txt. Demo. Text = current. Cell. Value. To. String(); Slide 21 8/22/2013 11: 30 AM

Deleting Rows from the Data. Grid. View n Like most collections, the Remove. At

Deleting Rows from the Data. Grid. View n Like most collections, the Remove. At method removes a row n n The argument contains the 0 -based index value Example to remove the first row: dgv. Demo. Rows. Remove. At(0); Slide 22 8/22/2013 11: 30 AM

Using a Loop to Examine Cells n The Rows collection has a Count property

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

Slide 24 6/14/2021 9: 24 AM

Slide 24 6/14/2021 9: 24 AM

Common Members of List Box and Combo Box Controls (continued) Slide 25 6/14/2021 9:

Common Members of List Box and Combo Box Controls (continued) Slide 25 6/14/2021 9: 24 AM

Slide 26 6/14/2021 9: 24 AM

Slide 26 6/14/2021 9: 24 AM

Code to Loading Combo Boxes Slide 27 6/14/2021 9: 24 AM

Code to Loading Combo Boxes Slide 27 6/14/2021 9: 24 AM

Clearing and Reloading a Combo Box Slide 28 6/14/2021 9: 24 AM

Clearing and Reloading a Combo Box Slide 28 6/14/2021 9: 24 AM

Retrieving Information from a Combo Box Slide 29 6/14/2021 9: 24 AM

Retrieving Information from a Combo Box Slide 29 6/14/2021 9: 24 AM

Slide 30 6/14/2021 9: 24 AM

Slide 30 6/14/2021 9: 24 AM

Slide 31 6/14/2021 9: 24 AM

Slide 31 6/14/2021 9: 24 AM

Slide 32 6/14/2021 9: 24 AM

Slide 32 6/14/2021 9: 24 AM

Slide 33 6/14/2021 9: 25 AM

Slide 33 6/14/2021 9: 25 AM

Slide 34 6/14/2021 9: 25 AM

Slide 34 6/14/2021 9: 25 AM

Slide 35 6/14/2021 9: 25 AM

Slide 35 6/14/2021 9: 25 AM

Slide 36 6/14/2021 9: 25 AM

Slide 36 6/14/2021 9: 25 AM

Adding Forms Slide 37 6/14/2021 9: 25 AM

Adding Forms Slide 37 6/14/2021 9: 25 AM

Slide 38 6/14/2021 9: 25 AM

Slide 38 6/14/2021 9: 25 AM

Slide 39 6/14/2021 9: 25 AM

Slide 39 6/14/2021 9: 25 AM

Slide 40 6/14/2021 9: 25 AM

Slide 40 6/14/2021 9: 25 AM

Slide 41 6/14/2021 9: 25 AM

Slide 41 6/14/2021 9: 25 AM

Slide 42 6/14/2021 9: 25 AM

Slide 42 6/14/2021 9: 25 AM

Slide 43 6/14/2021 9: 25 AM

Slide 43 6/14/2021 9: 25 AM

Dialog. Results and Tags Slide 44 6/14/2021 9: 25 AM

Dialog. Results and Tags Slide 44 6/14/2021 9: 25 AM

Slide 45 6/14/2021 9: 25 AM

Slide 45 6/14/2021 9: 25 AM

Slide 46 6/14/2021 9: 25 AM

Slide 46 6/14/2021 9: 25 AM

More on the Message Box Class Slide 47 6/14/2021 9: 25 AM

More on the Message Box Class Slide 47 6/14/2021 9: 25 AM

Displaying a Dialog Box Slide 48 6/14/2021 9: 25 AM

Displaying a Dialog Box Slide 48 6/14/2021 9: 25 AM

Slide 49 6/14/2021 9: 25 AM

Slide 49 6/14/2021 9: 25 AM

Slide 50 6/14/2021 9: 25 AM

Slide 50 6/14/2021 9: 25 AM

Slide 51 6/14/2021 9: 25 AM

Slide 51 6/14/2021 9: 25 AM

Slide 52 6/14/2021 9: 25 AM

Slide 52 6/14/2021 9: 25 AM

Slide 53 6/14/2021 9: 25 AM

Slide 53 6/14/2021 9: 25 AM

Slide 54 6/14/2021 9: 25 AM

Slide 54 6/14/2021 9: 25 AM

Slide 55 6/14/2021 9: 25 AM

Slide 55 6/14/2021 9: 25 AM

Slide 56 6/14/2021 9: 26 AM

Slide 56 6/14/2021 9: 26 AM

Slide 57 6/14/2021 9: 26 AM

Slide 57 6/14/2021 9: 26 AM

Code for the Customer Form (continued) private void Save. Data() { cbo. Names. Selected.

Code for the Customer Form (continued) private void Save. Data() { cbo. Names. Selected. Index = -1; lbl. Payment. Text = "“; is. Data. Saved = true; cbo. Names. Select(); } // end Save. Data Slide 58 6/14/2021 9: 26 AM

Slide 59 6/14/2021 9: 26 AM

Slide 59 6/14/2021 9: 26 AM

Slide 60 6/14/2021 9: 26 AM

Slide 60 6/14/2021 9: 26 AM

Slide 61 6/14/2021 9: 26 AM

Slide 61 6/14/2021 9: 26 AM

Slide 62 6/14/2021 9: 26 AM

Slide 62 6/14/2021 9: 26 AM

Slide 63 6/14/2021 9: 26 AM

Slide 63 6/14/2021 9: 26 AM

Slide 64 6/14/2021 9: 26 AM

Slide 64 6/14/2021 9: 26 AM

Slide 65 6/14/2021 9: 26 AM

Slide 65 6/14/2021 9: 26 AM

Slide 66 6/14/2021 9: 26 AM

Slide 66 6/14/2021 9: 26 AM

Slide 67 6/14/2021 9: 26 AM

Slide 67 6/14/2021 9: 26 AM