Visual Basic.NET 1.Getting started Loading it up Where to save.
-
Upload
kenneth-olwell -
Category
Documents
-
view
225 -
download
4
Transcript of Visual Basic.NET 1.Getting started Loading it up Where to save.
![Page 1: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/1.jpg)
Visual Basic.NEThttp://www.homeandlearn.co.uk/NET/vbNet.html
1. Getting started• Loading it up• Where to save and
how to organize your projects.
![Page 2: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/2.jpg)
2. Forms• From the menu bar,
click Debug• From the drop
down menu, click Start
• Or press the F5 key on your keyboard
• Click the Red X on the form to stop it from running.
![Page 3: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/3.jpg)
3. Controls• Things like buttons,
textboxes, and labels are all things that you can add to your Forms. They are know as Controls, and are kept in the Toolbox for ease of use.
• You can pin the toolbox to keep it in view.
![Page 4: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/4.jpg)
4. Adding controls to a form• Double click the textbox
tool (make 3 textboxes - what are textboxes used for?)
• Double click the label tool (make 3 labels - when would you use a label and when would you use a textbox tool?)
• Resize and line up
• Start your program to view
![Page 5: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/5.jpg)
5. VB.NET Properties• What you are
looking at is a list of the properties that a form has: Name , BackColor, Font, Image, Text, etc.
• Just to the right of these properties are the values for them. These values are the default values, and can be changed.
![Page 6: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/6.jpg)
Properties continued• Locate the word
"Text" in the Property box
• "Text" is a Property of Form1.
• Change the Text property to read “My First Project”
![Page 7: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/7.jpg)
Control Properties• Click label1and then
click inside the area next to "Text” property, and delete the word "Label1" by hitting the backspace key on your keyboard
• Type in the words "First Name”
• Change Label2 to read “Last name.
• Change Label 3 to read “Phone number”
![Page 8: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/8.jpg)
6. Consistent Programming
• Change the Name properties into:– frmName for Forms– lblName for labels– txtName for textboxes– cmdName for command buttons– picName for picture boxes
![Page 9: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/9.jpg)
7. Variables• a variable is a storage area of the computer's
memory• Think of it like this: a variable is an empty
cardboard box.• The use of variables allows values to be represented by
meaningful names that make the program code easy to read.
1. A variable can only store one value at a time.Dim dblX As DoubledblX = 5.5dblX = 10• The value stored would be 10 as it was assigned last.
![Page 10: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/10.jpg)
8. Option Explicit
• In the General section of each form, ensure that you turn Option Explicit On.
• This will catch many of your spelling errors.
![Page 11: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/11.jpg)
9. Data Types
Type Prefix Used to RepresentShort sho Integer with no decimals up to 32, 767
Long lng Integer with no decimals which go over 2,147,438,648
Decimal dec Decimal of 29 digits
String str Represents a set of characters
Integer int Integer with no decimals up to 2,147,438,648
![Page 12: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/12.jpg)
10. Declaring VariablesDim number1 As IntegerDim number 2 As Integer
number1 = 3number2 = 5
What does each part mean?Dim?As Integer?
![Page 13: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/13.jpg)
Question
• What would be the final value of the variable dblResult?
dblNumber = 10
dblNumber = 2 * 3
dblResult = dblNumber * 2
![Page 14: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/14.jpg)
11. Adding a Command Button
• New adding project
• Click on the button tool and change its text to read “Add Two Numbers”
• Change its property name to “cmdAdd”
• Make a label and clear its text.
• Change its property name to lblTotal
![Page 15: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/15.jpg)
• Double click on the command button.• Private means that no other part of the program
can see this code except for our button.• The "Sub” (subroutine) word tells VB that some
code follows.• _Click ( ) The Click Event will runt the code.• End Sub The subroutine ends right here.
![Page 16: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/16.jpg)
• Type the following in the command button click event.
Dim intNumber1 As IntegerDim intNumber 2 As IntegerDim intAnswer as Integer
intNumber1 = 3intNumber2 = 5
intAnswer = intNumber1 + intNumber 2
lblTotal.Text = intAnswer
![Page 17: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/17.jpg)
12. Constants
• In the previous project, both intNumber1 and inNumber 2 did not have values that changed…so they would be constants.
• If they are constants, they should be declared as such.
• Declaring a constant variable is as simple asConst intNumber1 As Integer = 3• Declare constants before Dim statements
![Page 18: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/18.jpg)
The Done Command Button
• Most programs have a “Done” button that ends the program.
• The code is
Unload Me
![Page 19: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/19.jpg)
13. Mathematical Expressions
• Add = +• Subtract = –• Multiply = *• Divide = /• Integer Division = \ (how many times one number
goes into another)• Modular division = mod (returns the remainder
resulting from division only)• Power of 2 = ^2• Use brackets
![Page 20: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/20.jpg)
Questions
• What is the value of intX?
Dim intX As Integer
intX = 20 Mod 7
• What is the problem with the following code?
Dim intX As Integer = 12
![Page 21: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/21.jpg)
14. Dealing with Textboxes
• You have coded for 2 constants to be added together, when they are clicked.
• We want a person to be able to put in two numbers and then the computer to add then up and put the result in the label.
• Always have a variable assigned to a textbox• Example• dblNumber1 = txtNumber1.Text
![Page 22: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/22.jpg)
• Add 2 textboxes to the form.• Label then txtNumber1 and txtNumber2• Ensure that the Text property is blank.• Have 2 variables = to the corresponding
textboxes.• Add the 2 variables together and equal to
the intAnswer variable• Display intAnswer in the lblTotal
![Page 23: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/23.jpg)
15. Change Event Procedures
• The change event occurs when the user begins to type in a textbox.
Private Sub txtSide_Change()
lblAnswer.Caption = “”
![Page 24: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/24.jpg)
16. Adding Images• From the toolbar click on the picture button• Either draw or double click on this button• Copy an image and paste into your folder.• Using the picture properties, find the picture on
your computer.• The image is True for being shown and False for
being invisible.• Add a smiley picture to your Adding form.• Call this picSmile in the properties of the picture
![Page 25: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/25.jpg)
17. Form Load Event Procedure
• Double click on the white part of the form.
• Enter the code
picSmile.value = False
• What do you think will happen?
![Page 26: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/26.jpg)
• To the command button, add the following code
picSmile.value = True
![Page 27: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/27.jpg)
Review 2 Area Calculation
• Create a new project that calculates the area of a rectangle with 1 side 5 cm and the other side is 8 cm.
• Display this in a label.• Put a picture of an rectangle on the form
and have this form only show until a command button for the answer being clicked.
![Page 28: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/28.jpg)
18. Message Box• Code for a pop up box
• Msgbox.Show(“Hello”)
• Or if you have a variable
• Msgbox.show(strName)
![Page 29: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/29.jpg)
![Page 30: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/30.jpg)
19. Option (Radio) Buttons• Adding Radio Buttons to a Form is exactly
the same process as adding a Checkbox.
• You must draw the option buttons in a group. Draw the group first.
• optName.Checked = True
![Page 31: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/31.jpg)
Review 3
• Create a new project.
• You will need a label (lblMessage), and a command button (cmdDone).
• Add 3 option buttons inside the group. Rename these buttons optEnglish, optSpanish and optfrench. The captions should say each language.
![Page 32: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/32.jpg)
• Double click on the optEnglish option button and add the following code
lblMessage.Caption = “Hello World”• Double click on the optSpanish option button and
add the following code
lblMessage.Caption = “Hola Mundo”• Double click on the optFrench option button and
add the following code
lblMessage.Caption = “Bonjour le monde”
• Add a form load event procedure
optEnglish.checked = True
![Page 33: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/33.jpg)
Exercises 1-5
![Page 34: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/34.jpg)
Part II Conditional Logic
• If…Then…End If
• If…Then…Else…End If
• If…Then…ElseIf…End If
• Select Case
![Page 35: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/35.jpg)
20. If…Then statements• These are conditional logic statements.• If I eat the cake Then my diet will be ruined • If I don't eat the cake Then I will be on course for a
slimmer figure Or• If I eat the cake Then my diet will be ruined• Else• If I don't eat the cake Then I will be on course for a
slimmer figureOr• If I eat the cake Then my diet will be ruined• Else• My diet is not ruined
![Page 36: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/36.jpg)
• Consider a number guessing game.• I pick a number between 1-10.If you guess the number then you are rightElse you are wrong.• Something should happen in both conditions,
either to let you know you are right or that you are wrong.
• For every If you need an End If to complete the condition.
![Page 37: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/37.jpg)
• Dim strFirstName as String
• strFirstName = "Bill"
• If firstname = Bill ThenMsgBox.Show("firstname is Bill“)
ElseMsgBox.Show("firstname is not
Bill“)End If
• Note that the line after the If statement is indented. This is good programming.
![Page 38: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/38.jpg)
Guessing Game Review • Make a new project.• Make a label that says “Guess a number between 1
– 10”.• Make a label that says “My Guess”• Beside the “My Guess” label make an empty
textbox for people to put there guesses.• Make a label that will say if you are correct or
incorrect.• Make a command button that will check your
number.• Make a command button that is a done button.
![Page 39: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/39.jpg)
Check Boxes
• Select “GroupBox” from the Containers in the toolbox.
• Draw a rectangle on the form and rename to grpTV and change the text to say TV
• Click on CheckBoxes and draw 2 inside the GroupBox
• Using a GroupBox allows you to move all the checkboxes as once.
![Page 40: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/40.jpg)
• If a checkbox has been selected, the value for the CheckState property will be 1; if it hasn't been selected, the value is zero
![Page 41: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/41.jpg)
If CheckBox1.CheckState = 1 Then
MsgBox("Checked")
End If
![Page 42: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/42.jpg)
Select Case
• Used when there is only a limited number of things that could be chosen.
Dim strCreamCake As StringDim strDietState As String
strCreamCake = txtTextBox1.Text (what is put in the textbox is stored)
Select Case strCreamCake (check variable)Case "Eaten“
strDietState = "Diet Ruined"Case "Not Eaten"
strDietState = "Diet Not Ruined"Case Else
strDietState = "Didn't check"
End SelectMsgBox strDietState
![Page 43: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/43.jpg)
Ampersand
• The symbol “&” connects 2 bits of information• Dim strFirstName As String
Dim strLastName As StringDim strFullName As String
• strFirstName = "Bill"strLastName = "Gates"
• strFullName = FirstName & LastName• txtTextbox1.Text = FullName
![Page 44: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/44.jpg)
Conditional Operators
![Page 45: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/45.jpg)
![Page 46: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/46.jpg)
![Page 47: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/47.jpg)
Combo Boxes
• Double click the icon to add a Combo Box to your form. Or click once with the left hand mouse button, and then draw one on the form.
• A combo box is a way to limit the choices your user will have. When a black down-pointing arrow is clicked, a drop down list of items appears. The user can then select one of these options.
![Page 48: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/48.jpg)
![Page 49: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/49.jpg)
• Whatever is in the Textbox will be transferred to the variable
Dim strItems as String
strItems = cboItems.Text
Msgbox strItems
![Page 50: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/50.jpg)
Loops
• You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this
Dim intAnswer As IntegerintAnswer = 1 + 2 + 3 + 4MsgBox intAnswer• Fairly simple, you think. And not much code, either. But
what if you wanted to add up a thousand numbers? Are you really going to type them all out like that? It's an awful lot of typing. A loop would make life a lot simpler.
• But don't get hung up too much on the name of the Loop. Just remember what they do: go round and round until you tell them to stop.
![Page 51: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/51.jpg)
For LoopsDim intAnswer As IntegerDim intStartNumber As Integer
intAnswer = 0For intStartNumber = 1 To 4
intAnswer = intAnswer + intStartNumber
Next intStartNumberMsgBox intAnswer
• Use the following code to halt the program if there is nothing in a textbox.
Exit Sub
![Page 52: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/52.jpg)
Do Loops
Dim intNumber as Integer
intNumber = 1
Do While intNumber < 5 MsgBox intNumberintNumber = intNumber + 1
Loop
![Page 53: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/53.jpg)
Do Until
Do Until intNumber < 5MsgBox intNumberintNumber = intNumber + 1
Loop
![Page 54: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/54.jpg)
Adding Menu’s
![Page 55: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/55.jpg)
Coding for Menu’s
• Press F7 on your keyboard to go to the code window • Click the black arrow at the top, where it says General
![Page 56: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/56.jpg)
• Rename the menu items• Use mnu as the prefix• Rename the ExitToolStripMenuItem to mnuExit• Bring up the code window by doubling clicking
on the mnuExit on the form
• Add Me.Close( )
• Run the program and see what happens when you click this in the menu
![Page 57: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/57.jpg)
Open File Dialogue Box
![Page 58: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/58.jpg)
Save File Dialogue Box
![Page 59: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/59.jpg)
Show and Hide Controls
![Page 60: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/60.jpg)
View Images Menu Button
![Page 61: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/61.jpg)
Arrays
![Page 62: Visual Basic.NET 1.Getting started Loading it up Where to save.](https://reader031.fdocuments.in/reader031/viewer/2022020105/551b3d00550346ae7a8b481a/html5/thumbnails/62.jpg)
Text Files