Presentation on theme: "Getting started Loading it up"— Presentation transcript:
1Visual Basic.NET http://www.homeandlearn.co.uk/NET/vbNet.html Getting startedLoading it upWhere to save and how to organize your projects.
22. Forms From the menu bar, click Debug From the drop down menu, click StartOr press the F5 key on your keyboardClick the Red X on the form to stop it from running.
33. ControlsThings 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.
44. 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 upStart your program to view
55. VB.NET PropertiesWhat 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.
6Properties continued Locate the word "Text" in the Property box "Text" is a Property of Form1.Change the Text property to read “My First Project”
7Control PropertiesClick label1and then click inside the area next to "Text” property, and delete the word "Label1" by hitting the backspace key on your keyboardType in the words "First Name”Change Label2 to read “Last name.Change Label 3 to read “Phone number”
86. Consistent Programming Change the Name properties into:frmName for FormslblName for labelstxtName for textboxescmdName for command buttonspicName for picture boxes
97. 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.A variable can only store one value at a time.Dim dblX As DoubledblX = 5.5dblX = 10The value stored would be 10 as it was assigned last.
108. Option ExplicitIn the General section of each form, ensure that you turn Option Explicit On.This will catch many of your spelling errors.
119. Data Types Type Prefix Used to Represent Short sho Integer with no decimals up to 32, 767LonglngInteger with no decimals which go over 2,147,438,648DecimaldecDecimal of 29 digitsStringstrRepresents a set of charactersIntegerintInteger with no decimals up to 2,147,438,648
1210. Declaring Variables Dim number1 As Integer Dim number 2 As Integer What does each part mean?Dim?As Integer?
13Question What would be the final value of the variable dblResult? dblNumber = 10dblNumber = 2 * 3dblResult = dblNumber * 2
1411. Adding a Command Button New adding projectClick 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
15Double 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.
16Type the following in the command button click event. Dim intNumber1 As IntegerDim intNumber 2 As IntegerDim intAnswer as IntegerintNumber1 = 3intNumber2 = 5intAnswer = intNumber1 + intNumber 2lblTotal.Text = intAnswer
1712. ConstantsIn 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 = 3Declare constants before Dim statements
18The Done Command Button Most programs have a “Done” button that ends the program.The code isUnload Me
1913. 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 = ^2Use brackets
20Questions What is the value of intX? Dim intX As Integer intX = 20 Mod 7What is the problem with the following code?Dim intX As Integer = 12
2114. 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 textboxExampledblNumber1 = txtNumber1.Text
22Add 2 textboxes to the form. Label then txtNumber1 and txtNumber2Ensure that the Text property is blank.Have 2 variables = to the corresponding textboxes.Add the 2 variables together and equal to the intAnswer variableDisplay intAnswer in the lblTotal
2315. Change Event Procedures The change event occurs when the user begins to type in a textbox.Private Sub txtSide_Change()lblAnswer.Caption = “”
2416. Adding Images From the toolbar click on the picture button Either draw or double click on this buttonCopy 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
2517. Form Load Event Procedure Double click on the white part of the form.Enter the codepicSmile.value = FalseWhat do you think will happen?
26To the command button, add the following code picSmile.value = True
27Review 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.
2818. Message Box Code for a pop up box Msgbox.Show(“Hello”) Or if you have a variableMsgbox.show(strName)
3019. 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
31Review 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.
32Double 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 codelblMessage.Caption = “Hola Mundo”Double click on the optFrench option button and add the following codelblMessage.Caption = “Bonjour le monde”Add a form load event procedureoptEnglish.checked = True
34Part II Conditional Logic If…Then…End IfIf…Then…Else…End IfIf…Then…ElseIf…End IfSelect Case
3520. If…Then statements These are conditional logic statements. If I eat the cake Then my diet will be ruinedIf I don't eat the cake Then I will be on course for a slimmer figureOrElseMy diet is not ruined
36Consider 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.
37Dim strFirstName as String strFirstName = "Bill"If firstname = Bill Then MsgBox.Show("firstname is Bill“) Else MsgBox.Show("firstname is not Bill“) End IfNote that the line after the If statement is indented. This is good programming.
38Guessing 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.
39Check 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 TVClick on CheckBoxes and draw 2 inside the GroupBoxUsing a GroupBox allows you to move all the checkboxes as once.
40If a checkbox has been selected, the value for the CheckState property will be 1; if it hasn't been selected, the value is zero
41If CheckBox1.CheckState = 1 Then MsgBox("Checked")End If
42Select CaseUsed when there is only a limited number of things that could be chosen.Dim strCreamCake As StringDim strDietState As StringstrCreamCake = 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
43Ampersand The symbol “&” connects 2 bits of information Dim strFirstName As String Dim strLastName As String Dim strFullName As StringstrFirstName = "Bill" strLastName = "Gates"strFullName = FirstName & LastNametxtTextbox1.Text = FullName
47Combo BoxesDouble 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.
49Whatever is in the Textbox will be transferred to the variable Dim strItems as StringstrItems = cboItems.TextMsgbox strItems
50LoopsYou want to add up the numbers 1 to 4: You could do it like thisDim intAnswer As IntegerintAnswer =MsgBox intAnswerFairly 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.
51For Loops Dim intAnswer As Integer Dim intStartNumber As Integer For intStartNumber = 1 To 4 intAnswer = intAnswer + intStartNumber Next intStartNumberMsgBox intAnswerUse the following code to halt the program if there is nothing in a textbox.Exit Sub
52Do Loops Dim intNumber as Integer intNumber = 1 Do While intNumber < 5 MsgBox intNumber intNumber = intNumber + 1Loop
55Coding 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
56Run the program and see what happens when you click this in the menu Rename the menu itemsUse mnu as the prefixRename the ExitToolStripMenuItem to mnuExitBring up the code window by doubling clicking on the mnuExit on the formAdd Me.Close( )Run the program and see what happens when you click this in the menu