structures and more controls chapter microsoft visual basic.net: reloaded 1

54
Structures and More Controls Chapter Microsoft Visual Basic .NET: Reloaded 1

Upload: arthur-wilkins

Post on 13-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

Structures and More Controls

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Create a structure

• Declare and manipulate a structure variable

• Differentiate between a structure variable and member variables

• Create an array of structure variables

• Include a radio button in an interface

Page 3: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Include a check box in an interface

• Include a list box in an interface

• Code a text box’s KeyPress event

• Code a text box’s Enter event

• Associate a procedure with different events

Page 4: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

Structures

• You can create your own data types using the Structure statement

• Referred to as “user-defined data types” or “structures”

• Structures contain “member variables” between Structure clause and End Structure clause

• Member variables are declared using Public keyword followed by the variable name and then the desired data type

Page 5: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 6: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

Using a Structure to Declare a Variable

• Variables declared using a structure are often referred to as “structure variables”

Page 7: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 8: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure

• The personnel manager at Johnsons Lumber wants an application that he can use to save each manager’s employee number, name, and salary in a sequential access file• Figure 10.4 shows a sample run of the

Johnsons Lumber application

• Figure 10.5 shows how you can code the application without using a structure

• Figure 10.6 shows coding the application using an Employee structure to group together the employee data

Page 9: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 10: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 11: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 12: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 13: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 14: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

Passing a Structure Variable to a Procedure (continued)

Page 15: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

Creating an Array of Structure Variables

• An array of structures can be used in place of two or more parallel arrays

• Figures 10.7 and 10.8 demonstrate using an array of structure variables for the Takoda Tapahe application first introduced in chapter 9

Page 16: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

Creating an Array of Structure Variables (continued)

Page 17: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

Creating an Array of Structure Variables (continued)

Page 18: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

Adding a Radio Button to the Form

• Use Radio Button Tool in the toolbox

• Radio Button allows you to limit user to only one choice in a group of two or more related and mutually exclusive choices.

• Radio buttons are placed in a group box control or panel control to be mutually exclusive

• Group box or panel control must be added to form before adding radio button

• Set default button within group by setting Checked property to True

Page 19: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 20: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

Adding a Radio Button to the Form (continued)

Page 21: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

Adding a Radio Button to the Form (continued)

Page 22: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

Adding a Radio Button to the Form (continued)

Page 23: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

Adding a Check Box Control to the Form

• Check boxes allow user to select any number of choices from a group of one or more independent and nonexclusive choices

Page 24: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

Adding a Check Box Control to the Form (continued)

Page 25: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

Adding a Check Box Control to the Form (continued)

Page 26: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Adding a Check Box Control to the Form (continued)

Page 27: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

Adding a List Box to an Interface• Use a list box to display a list of choices from

which the user can select zero, one, or more choices

Page 28: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

Adding a List Box to an Interface (continued)

Page 29: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

Adding Items to a List Box

• Items in a list box belong to Items Collection

• Collection is group of one or more individual objects treated as one unit

• Items in the collection identified by an index

• An index is a unique number determined by the item’s order of placement in the collection starting with zero

• Add method adds items to the list box

• The item selected when interface first appears is referred to as the “default list box item”

Page 30: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 31: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

The SelectedItem and SelectedIndex Properties

Page 32: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

The SelectedItem and SelectedIndex Properties (continued)

Page 33: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

The SelectedItem and SelectedIndex Properties (continued)

Page 34: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

The SelectedItem and SelectedIndex Properties (continued)

Page 35: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

Using the KeyPress Event

• A control’s KeyPress event occurs when the user presses a key while the control has focus

• KeyPress event has two parameters sender and e

• Use e.KeyChar property to determine which key was pressed

• Use e.Handled property to cancel if key was inappropriate for desired action

Page 36: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 37: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

Selecting the Existing Text in a Text Box

• Use SelectAll method in appropriate Sub such as the Enter event for text box • Enter event occurs when user tabs to control

and focus is received

Page 38: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

Associating a Procedure with Different Events

• List each event, separated by commas, in the Handles section of the procedure header

Page 39: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

Programming Example – Glovers Application

• Glovers Industries stores the item numbers, wholesale prices, and retail prices of the items it sells in a sequential access file named items.txt.

• Opal Jacoby, the company’s sales manager, wants an application that allows her to enter an item number and then display either the wholesale price or the retail price

Page 40: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

40Microsoft Visual Basic .NET: Reloaded

Programming Example – Glovers Application (continued)

• Data is stored in items.txt, one item per line

• Item number is first,

• then wholesale price, and

• then retail price

Page 41: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

41Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 42: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

42Microsoft Visual Basic .NET: Reloaded

User Interface

Page 43: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

43Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 44: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

44Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 45: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

45Microsoft Visual Basic .NET: Reloaded

Pseudocode

frmGlovers Load event procedure

Try

open items.txt for input

repeat until no more characters to read or end of array

read an item number, wholesale price, and retail price and

assign it to the mitmItems array

add the item number to the lstNumber control

add 1 to the variable that keeps track of the array subscripts

end repeat

close the items.txt file

select the first item in lstNumbers control

Catch (use general catch statement to handle any errors)

if error occurs, display exception description in a messagebox

Page 46: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

46Microsoft Visual Basic .NET: Reloaded

Pseudocode (continued)

btnExit Click event procedureclose application

btnDisplay click Event procedureassign item number selected in lstNumbers to strSearchFornumber variablerepeat until item number is located in mitmItems array

add 1 to the variable that keeps track of array subscriptsif radWholesale control is select

display in lblPrice wholesale price stored in mitmItems arrayelse

display in lblPrice retail price stored in mitmItems arrayradRetail_Click, radWholesale_Click event procedures and lstNumbers.SelectedIndex Changed procedure

clear the contents of the lblPrice control

Page 47: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

47Microsoft Visual Basic .NET: Reloaded

Code

Page 48: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

48Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 49: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

49Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 50: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

50Microsoft Visual Basic .NET: Reloaded

Summary• You can use the Structure statement to define

a user-defined data type (or structure) in Visual Basic .NET

• Typically enter the Structure statement in the form’s Declarations section

• A structure variable contains one or more member variables

• You access a member variable using the structure variable’s name, followed by the dot member access operator and the member variable’s name

Page 51: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

51Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Use an assignment statement to assign a value to a member variable• Data type of value must match data type of

member variable

• Structure variable can be passed to procedures

• You access a member variable in an array element using the array’s name, followed by the element’s subscript enclosed in parentheses, the dot member access operator, and the member variable’s name

Page 52: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

52Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Use radio buttons when you want to limit the user to one of two or more related and mutually exclusive choices

• Two is the minimum number of radio buttons in a group recommended maximum is seven

• The label in the radio button’s Text property should be entered using sentence capitalization

• Assign a unique access key to each radio button in an interface

Page 53: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

53Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Use a group box control (or a panel control) to create separate groups of radio buttons• Only one button in each group can be selected

at any one time

• Designate a default radio button in each group of radio buttons

• Use check boxes when you want to allow the user to select any number of choices from a group of one or more independent and nonexclusive choices

Page 54: Structures and More Controls Chapter Microsoft Visual Basic.NET: Reloaded 1

54Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• A list box should display minimum of three selections and maximum of eight selections at a time

• Default item in list box should be either the most used selection or the first selection in the list

• You can use the e parameter’s KeyChar and Handled properties to prevent a text box from accepting inappropriate characters

• You can enter more than one event after the Handles keyword in a procedure