VB notes unit 1

advertisement
Visual Basic.NET
http://www.homeandlearn.co.uk/NET/vbNet.html
1. Getting started
• Loading it up
• Where to save and
how to organize your
projects.
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.
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.
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
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.
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”
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”
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
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 Double
dblX = 5.5
dblX = 10
• The value stored would be 10 as it was assigned last.
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.
9. Data Types
Type
Prefix
Used to Represent
Short
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
10. Declaring Variables
Dim number1 As Integer
Dim number 2 As Integer
number1 = 3
number2 = 5
What does each part mean?
Dim?
As Integer?
Question
•
What would be the final value of the
variable dblResult?
dblNumber = 10
dblNumber = 2 * 3
dblResult = dblNumber * 2
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
• 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.
• Type the following in the command button click event.
Dim intNumber1 As Integer
Dim intNumber 2 As Integer
Dim intAnswer as Integer
intNumber1 = 3
intNumber2 = 5
intAnswer = intNumber1 + intNumber 2
lblTotal.Text = intAnswer
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 as
Const intNumber1 As Integer = 3
• Declare constants before Dim statements
The Done Command Button
• Most programs have a “Done” button that
ends the program.
• The code is
Unload Me
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
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
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
•
•
•
•
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
15. Change Event Procedures
• The change event occurs when the user
begins to type in a textbox.
Private Sub txtSide_Change()
lblAnswer.Caption = “”
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
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?
• To the command button, add the following
code
picSmile.value = True
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.
18. Message Box
•
•
•
•
Code for a pop up box
Msgbox.Show(“Hello”)
Or if you have a variable
Msgbox.show(strName)
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
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.
• 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
Exercises 1-5
Part II Conditional Logic
•
•
•
•
If…Then…End If
If…Then…Else…End If
If…Then…ElseIf…End If
Select Case
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 figure
Or
• If I eat the cake Then my diet will be ruined
• Else
• My diet is not ruined
• Consider a number guessing game.
• I pick a number between 1-10.
If you guess the number then you are right
Else 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.
• Dim strFirstName as String
• strFirstName = "Bill"
• If firstname = Bill Then
MsgBox.Show("firstname is Bill“)
Else
MsgBox.Show("firstname is not Bill“)
End If
• Note that the line after the If statement is
indented. This is good programming.
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.
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.
• 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
If CheckBox1.CheckState = 1 Then
MsgBox("Checked")
End If
Select Case
• Used when there is only a limited number of things that could be
chosen.
Dim strCreamCake As String
Dim 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 Select
MsgBox strDietState
Ampersand
• The symbol “&” connects 2 bits of information
• Dim strFirstName As String
Dim strLastName As String
Dim strFullName As String
• strFirstName = "Bill"
strLastName = "Gates"
• strFullName = FirstName & LastName
• txtTextbox1.Text = FullName
Conditional Operators
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.
• Whatever is in the Textbox will be
transferred to the variable
Dim strItems as String
strItems = cboItems.Text
Msgbox strItems
Loops
• You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You
could do it like this
Dim intAnswer As Integer
intAnswer = 1 + 2 + 3 + 4
MsgBox 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.
For Loops
Dim intAnswer As Integer
Dim intStartNumber As Integer
intAnswer = 0
For intStartNumber = 1 To 4
intAnswer = intAnswer + intStartNumber
Next intStartNumber
MsgBox intAnswer
• Use the following code to halt the program if there is
nothing in a textbox.
Exit Sub
Do Loops
Dim intNumber as Integer
intNumber = 1
Do While intNumber < 5
MsgBox intNumber
intNumber = intNumber + 1
Loop
Do Until
Do Until intNumber < 5
MsgBox intNumber
intNumber = intNumber + 1
Loop
Adding Menu’s
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
•
•
•
•
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
Open File Dialogue Box
Save File Dialogue Box
Show and Hide Controls
View Images Menu Button
Arrays
Text Files
Download