Download presentation
Presentation is loading. Please wait.
Published byDaniella Cross Modified over 8 years ago
1
Programming with Visual Basic.NET
2
Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons
3
Programming with Visual Basic.NET An application contains program code which are instructions that tell a computer how to perform a specific task Each line of code is called a statement Most programs you use contain millions of statements and are usually written by teams of programmers VB.NET writes a lot of code for you! This code generates the initial form and sets the default properties of the form. Program Code
4
Programming with Visual Basic.NET Program Code Cont’d Also, when you draw in a control object in the design window, such as a button, VB.NET generates code automatically to create that object; the code for the object’s functionality is left to you You can get to the code window by using the Solution Explorer Window or by double clicking any object in the form, including the form itself
5
Programming with Visual Basic.NET The Code Window The code window below shows the code for a blank form Blocks of code can be expanded to show all the statements in the block (+) or collapsed to hide the statements (-)
6
Programming with Visual Basic.NET The Code Window Cont’d For example, the Windows Form Designer Generated Code is collapsed. If you want to see how much code is generated for you by VB.NET just to create a form, expand this block (click on the plus sign)
7
Programming with Visual Basic.NET The Event Procedure A procedure is a block of code written to perform a specific task Most control objects require an event procedure, also called event handler The corresponding event procedure is run in response to the event generated by the user Example: When the user clicks a button, the code in the event procedure for that button is run
8
Programming with Visual Basic.NET Event Procedure Example Below is a program we designed in the Design Window On the left, we have the properties for the ‘Ok’ button; note the Name is btnOK
9
Programming with Visual Basic.NET Event Procedure Example Now we double-click on the ‘Ok’ button to move from the design window to the code window Now we can add code that will be run when the user clicks on the OK button
10
Programming with Visual Basic.NET Event Procedure Example An event procedure starts with ( Private ) Sub and ends with End Sub ; what goes between these lines is the body with statements that execute when the event occurs “Sub” is short for subroutine which is another name for procedure
11
Programming with Visual Basic.NET Good Programming Style Body statements are indented for better readability and to help organize blocks of code There are other rules to follow to have good programming style but we will talk about these in due time
12
Programming with Visual Basic.NET Assignment Statements Assignment statements are used in a procedure to change a value at run-time You can use assignment to change the value of an object property at runtime In this case, assignment takes the form: ObjectName.Property = Value ObjectName is the control object name, Property is the property of the object we wish to change, and Value is a valid property setting We’ll do an example…
13
Programming with Visual Basic.NET Assignment Statements Example The application below has a menu and a label Using the menu, the user can control what is displayed in the label
14
Programming with Visual Basic.NET Assignment Statements Example How does it work? Let’s first look at the Names of our objects lblHelloWorld mnuInSpanish mnuInEnglish
15
Programming with Visual Basic.NET Assignment Statements Example Now let’s look at the event procedures for the menu commands, ‘In Spanish’ and ‘In English’ Assignment ObjectName.Property = Value
16
Programming with Visual Basic.NET Using AutoList In the last example, we saw the following statement: lblHelloWorld.Text = “Hola Mundo!” If we can’t remember the name of the property we want to change (in this case Text), VB.NET helps us As soon as we type lblHelloWorld. (note the dot), VB.NET recognizes this name and knows it is the name of a label. So it displays a list of properties for a label for you to choose from.
17
Programming with Visual Basic.NET Using AutoList So, just in case we forgot the name of the property we want to change, the autolist pops up to help us find what we want
18
Programming with Visual Basic.NET The RadioButton Control We can use a group of radio buttons to allow the user to select one option out of a group In the program below, the user can choose the language using radio buttons and the label will change to that language
19
Programming with Visual Basic.NET The RadioButton Control The RadioButton control has the properties Name: identifies the control for the programmer. It is good programming style to begin RadioButton object names with rad. Example, radSpanish, radFrench, radEnglish Text: the text that is displayed with the radio button; what the user sees Checked: Can be set to True or False. True means that radio button is selected, False means it is not. Since only one option can be set to True in a group, setting one radio button to True sets all other buttons in the group to false
20
Programming with Visual Basic.NET Creating RadioButtons A GroupBox object is used to group related radio buttons We first draw a GroupBox on the form and then we add radio buttons to the GroupBox by selecting the RadioButton on the toolbox and clicking in the GroupBox GroupBox has the properties Name (good style to prefix with grp) and Text Then you can double-click each radio button to add assignment statements to change the label text
21
Programming with Visual Basic.NET Operators and Expressions VB.NET includes built-in operators: addition (+), multiplication (*), division (/), subtraction (-) and exponentiation (^) Operators can be used in conjunction with numbers to form expressions Expressions follow order of operations or operator precedence. You can use brackets to change the order of operation. Example: lblHelloWorld.Text = (5 + 2) * 3 will display 21 in the label; lblHelloWorld.Text = “(5 + 2) * 3” will display (5 + 2) * 3 in the label
22
Programming with Visual Basic.NET The Button Control Windows applications usually include buttons The button control has the Name (good style to prefix with btn) and Text (appears on the button) properties Double-clicking on a created button allows you to add code to the event procedure for the button; that is, code that will run when the button is clicked
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.