Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Getting Started with Visual Basic .NET
Learning Target: Make a start with VB .NET
Hands-on Exercises:
Launch your Visual Basic .NET or Visual Studio software. When the software first loads, you'll see a screen something like this one, if you have the 2008 version:
Or this one, for VB NET 2010 users:
There's a lot happening on the start page. But basically, this is where you can start a new project, or open an existing one. The first Tab, Projects, is selected. At the moment, the area labelled "Open an Existing Project" is blank. This is what you'll see when you run the software for the first time (because you haven't created a project yet). When you create a project, the Name you gave it will be displayed on this page, as a hyperlink. Clicking the link will open the project.
At the bottom of the screen, there are two buttons: "New Project" and "Open Project". To get started, click the "New Project" button. When you do, you'll see this dialogue box appear in the version 2008:
Or this one for version 2010:
As a beginner, you'll normally want the option selected: "Windows Application", in the "Visual Basic Projects" folder. This means that you're going to be designing a programme to run on a computer running the Microsoft Windows operating system.
If you look in the Name textbox at the bottom, you'll see it says "WindowsApplication1". This is the default name for your projects. It's not a good idea to keep this name. After all, you don't want all of your projects to be called "WindowsApplication1", "WindowsApplication2", etc. So click inside this textbox and change this Name to the following:
My First Project
Keep the Location the same as the default. This is a folder inside of your "My Documents" folder called "Visual Studio Projects". A new folder will then be created for you, and its name will be the one you typed in the "Name" textbox. All of your files for your first project are then saved in this folder.
Click the OK button, and the Visual Basic NET design time environment will open. It will look like the following (version 2008):
Or this, in version 2010:
That's a very daunting piece of software, hey? Well, don't worry. We'll break it down bit by bit in the next few sections, and pretty soon you'll be zipping your way around it like a pro!
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Visual Basic .NET Forms
Learning Target: Run the default form
Hands-on Exercises:
In the Visual Basic NET design time environment, the first thing to concentrate on is that strange, big square in the top left. That's called a form. It's actually the pretty bit of your programme, the part that others will see when they launch your masterpiece. Granted, it doesn't look too attractive at the moment, but you'll soon discover ways to lick it into shape.
To run the form, try this:
• From the menu bar, click Debug
• From the drop down menu, click Start
• Alternatively, press the F5 key on your keyboard
• Your programme is launched
Congratulations! You have now created your very first programme. It should look like this:
Click the Red X on the form to stop it from running. You will then be returned to the software environment.
If you compare the first form with the one above, you'll see that they look very similar. But the one above is actually a real programme, something you could package and sell to unsuspecting village idiots
So what's going on? Why the two different views? Well, Visual Basic has two distinct environments, a Design environment and a Debug environment. Design Time is where you get to play about with the form, spruce it up, add textboxes, and buttons, and labels (and code, of course ); Debug is where you can test your programme and see how well it performs. Or doesn't perform, as is usually the case.
But don't worry about the terminology, for the time being. Just be aware that there's a two step process to VB programming: designing and debugging.
So, let's get on and do some designing! Before we can start designing a form, though, we need some tools. And where are tools kept? In a toolbox!
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Adding Controls Using the Toolbox
Learning Target: Adding a Control to a Form
Hands-on Exercises:
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.
The Toolbox can be found on the left of the screen. In the picture below, you can see the toolbox icon next to Form1 (VB Net 2008):
VB Net 2010:
To display all the tools, move your mouse over the toolbox icon. You'll see the following automatically appear:
There are seven categories of tools available. The toolbox you'll be working with first is the Common Controls toolbox. To see the tools, click on the plus symbol next to Common Controls. You'll see a long list of tools:
As you can see, there are an awful lot of tools to choose from! For this first section, we'll only be using the Button, the TextBox and the Label.
If you want to keep the toolbox displayed, click the Pin icon next to the X. To close the toolbox, simply move your mouse away.
In the next part, we'll see how to add a textbox to the form.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Adding a Tool (Control) to your Form
Learning Target: Add a Control to your VB .NET Forms
Hands-on Exercise:
Let's start by adding a textbox to our form. With the tools displayed, do the following:
• Locate the TextBox tool
• Double click the icon
• A textbox is added to your form
The textbox gets added to the top left position of your form. To move it down, hold your mouse over the textbox and drag to a new position:
Notice the small squares around the textbox. These are sizing handles. Move your mouse over one of them. The mouse pointer turns into an extended line with arrowheads. Hold your left mouse button down and drag outwards. The textbox is resized. Play around with the sizing handles until you're happy with the size of your textbox.
One thing you will notice is that you can't make the size any higher, but you can make it wider. The reason why you can't make it any higher is because the default action of a textbox is to have it contain only a single line of text. If it's only going to contain one line of text, Microsoft reasoned, there's no reason why you should be able to change its height. A textbox can only be made higher if it's set to contain multiple lines of text. You'll see how to do this soon.
• Create two more textboxes by double clicking on the textbox icon in the toolbar (Or Right-click on the selected textbox and choose Copy. Then Right-click on the Form and choose Paste.)
• Resize them to the same size as your first one
• Line them up one below the other with space in between
• Try to create something that looks like the one below
Adding a Label to your Form
Let's add some labels near the textboxes so that your users will know what they are for.
• Locate the label control in the toolbox
• Double click the label icon
• A new label is added to your form
• It should look like the one below
Click on the label to select it. Now hold your left mouse button down on the label. Keep it held down and drag it to the left of the textbox.
Create two more labels, and position them to the left of the textboxes. You should now have a form like this one:
To see what your Form looks like as a programme, click Debug > Start from the menu bar. Or press F5 on your keyboard:
To stop the programme from running, you can do one of the following:
1. Click the Red X at the top right of your Form
2. Click Debug > Stop Debugging from the menu bar
3. Press Shift + F5 on your keyboard
You can also click the Stop button on the VB toolbars at the top, as in the image below:
All right, we're getting somewhere. We now have a form with textboxes and labels, something that looks like a form people can fill in. But those labels are not exactly descriptive, and our textboxes have the default text in them. So how can we enter our own text for the labels, and get rid of that default text for the textboxes?
To do those things, we need to discuss something called a Property.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Visual Basic.NET and Properties
Learning Target: Discuss VB .NET Properties
Hands-on Exercise:
You've probably noticed the area to the right of the design environment, the area with all the textboxes in a grid, the one that has those daunting names like "AccessibleDescription", "AccessibleName", "AccessibleRole". That's the Properties box.
Click anywhere on the form that is not a label or a textbox, somewhere on the form's grey areas. The form should have the little sizing handles now, indicating that the form is selected.
On the right of the design environment there should be the following Properties box:
If your Properties box says "Textbox1 Textbox" or "Label1 Label" then you haven't yet selected the Form. Click away from the textbox or label until the Properties box reads "Form1 Form"
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. We're going to change the value of the Text property.
First, you might want to display the list of Properties in a more accessible form. You can display the list properties alphabetically. To do that, click the Alphabetic icon at the top of the Properties box, as in the image below:
This will make the properties easier to find.
Before we change any in the Properties box, let's clear up what we mean by "Property".
What is a Property?
Those controls you added to the form (textboxes and labels), and the form itself, are called control objects. You can think of controls as things, something solid that you can pick up and move about. Controls (things) have properties. If your television were a control, it too would have properties: an On/Off button property, a colour property, a volume property, and a ... well, what other properties would your television have? Think about it.
The properties of your television will have values. The On/Off button would have just two values - On or Off. The volume property could have a range of values, from zero to ten, for example. If the value of the volume property was set to ten, the loudest value, then you'd probably have some very angry neighbours!
In VB.NET, you can change a property of a control from the Properties Box. (You can also change a property using code, which you'll do quite a lot.) If we go back to our Form object, and the properties and values it has, we can see how to change them using the Properties Box. We'll change only one of these values for now - the value of the Text property . So, do this:
• Locate the word "Text" in the Property box, as in the image below
"Text" is a Property of Form1. Don't be confused by the word "Form1" next to the word "Text". All this means is that the current value of the Text property is set to the word "Form1". This is the default.
To change this to something of your own, do this:
• Click inside the area next to "Text", and delete the word "Form1" by hitting the backspace key on your keyboard
• When "Form1" has been deleted, type the words "My First Form"
• Click back on the form itself (the one with the labels and textboxes), or hit the return key on your keyboard
• The words "My First Form" will appear as white text on a blue background at the top of the form
When you've correctly changed the Text property, your Form will then look like this one:
As you can see, your new text has gone at the top of the form, in white on the blue background.
So the Text Property of a form is for setting the caption you want to display in the title bar at the top.
In the next part, we'll take a look at how to change the text property of labels and textboxes.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: The Text Property
Learning Target: Change the Values of Text Property of a Control
Hands-on Exercise:
Changing the values of some properties is fairly easy. We'll now change the Text properties of our labels, and the Text properties of our Textboxes.
Click on Label1 so that it has the sizing handles, and is therefore selected. Examine the Property box for the Label:
You can see that the Label control has quite a few different properties to the Form control. Think back to your television as an control. It will have different buttons and knobs to your DVD Player control. A label has different "buttons and knobs" to the Form's "buttons and knobs".
But the Label has a lot of properties that are the same. The Text property of a Label does what you'd expect it to do: adds text to your label. We'll do that now:
• With label1 selected, click inside the area next to "Text", and delete the word "Label1" by hitting the backspace key on your keyboard
• Type in the words "First Name"
• Click back onto the grey form, or hit the return key on your keyboard
• Label1 has now changed its text caption to read "First Name"
• If you've made a typing error, go back to the first step above and try again
• Your form should now look like this:
Now, change the Text property of the other two labels. Change them to these values:
Label2: Last Name
Label3: Telephone Number
What you should notice is that the labels resize themselves, after you press the enter key to commit the changes. You may need to drag your labels to the left a bit. But when you're finished, your form should look like ours below:
The form might look a little squashed, though. Is there anything we can do to make it bigger? Well, it just so happens there is.
The Form can be resized just like the Label and the textboxes. Click anywhere on the form that is not a textbox or a label. If you look closely around the Form's edges, you'll notice our old friends the sizing handles. To make the form bigger, just stretch them like you did the labels and the textboxes. Play around with the size of the form until you're happy with it. You can now reposition and resize the textboxes and labels so that things don't look too squashed. Your form might look like this one:
Click on Debug > Start to have a look at your programme. Or Press F5 on your keyboard. Click Debug > Stop Debugging to get back to the design environment. (Or press Shift + F5, or just click the red X at the top right of the form.)
Now, lets add a splash of colour to our form.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Adding a Splash of Colour
Learning Target:
Hands-on Exercise:
At the moment, our form looks a little bland. Time to liven it up with a splash of colour.
Changing the colour of the Form means we have to change one of its properties - the BackColor property.
So click anywhere on the form that is not a textbox or a label. If you do it right, you should see the sizing handles around the edges of the grey form. The Property Box on the right will read "Form1", and that indicates that you have indeed selected the form. When the Form is selected you can change its properties.
To change the colour of the Form, click the word "BackColor" in the Property Box. Next, click the black down-pointing arrow to the right. A drop-down box will appear.
The default colour is the one selected - Control. This is on the System Tab. The System colours are to set whatever colour scheme the user has opted for when setting up their computers. For example, you can use the Display Properties dialogue box in Windows XP to change how things like menus and buttons look. Someone who is colour-blind might have changed his or her settings in order to see things better on the computer screen. If you stick with the System colours then a colour-blind user of your programme would not have any problems seeing your master work.
As you can see in the image above, you can choose the colour of the Active Caption. The Active Caption is the one you set earlier when you changed the text to "My First Form". The Active Caption is blue on my computer, and the Active Caption Text is white. It might be different on yours.
If you want to choose a colour that is not a System colour, click the Custom Tab. You'll then see this:
Click on any of the Colours in the colour palette and the background colour of your form will change.
You can also select the Web Tab. When you do, you'll see a list of Web-Safe colours to choose from. A Web-Safe colour is one that displays correctly in a web browser, regardless of which computer being used (that's the theory, anyway). You might want to use a Web-Safe colour if you're designing a project for the internet. But you can choose one even if you're not.
To change the colour of the labels, click on a label to select it. Look in the Property box to see if it reads Label. If so, you can now go ahead and change the BackColor property of the Label in exactly the same way that we changed the BackColor property for our Form.
Change the colour of the other two labels to anything you like. To change the colour of more than one Label at a time, click on one Label to select it. Now, hold down the "Ctrl" key on your keyboard and click another Label. You'll see that two Labels now have sizing handles around them. Click the third Label with the "Ctrl" key held down, and all three Labels will be selected. You can change the BackColor property of all three at once.
If you want to change the Font size of the Labels and Textboxes, select a control. Let's start with Label1.
• So click on Label 1
• Scroll down the Property Box until you see Font
• Click on the word "Font" to highlight it
• MS Sans Serif is the default Font
Notice that the Font property has a cross next to it. This indicates that the property is expandable. Click the cross to see the following:
Notice that the Font property has a cross next to it. This indicates that the property is expandable. Click the cross to see the following:
As you can see, you can change a lot of Font properties from here: the Name of the font, its Size, whether is should be Bold or not, etc. You can also click the square box with the three dots in it. This brings up a dialogue box where you can change the font properties in the same place.
Make the following changes to the three labels:
Font: Arial
Font Style: Bold
Font Size: 10
Change the Font of the three Textboxes so that they are the same as the Labels.
When you're finished, you should have a form that looks a little more like a real programme. Time now to save your work.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Saving work in VB.NET
Learning Target: Save work in VB.NET
Hands-on Exercise:
f you have a look in the top right of the Design Environment, you'll see the Solution Explorer. (If you can't see it, click View > Solution Explorer in version 2008 and View > Other Windows > Solution Explorer in version 2010.)
The Solution Explorer shows you all the files you have in your project (Notice that the name of your project is at the top of the tree - "My First Project").
At first glance, it looks as though there are not many files in the project. But click the Show All Files icon, circled below:
When you click Show All Files, the Solution Explorer will look something like this:
When you save your project, you are saving all these files.
To save your work, click File > Save All and you'll see the following dialogue box (we've chopped ours down a bit):
The files are usually saved in the My Document folder in XP (Document folder in Vista and Windows 7), under Visual Studio. If you want to save your projects elsewhere, click the Browse button.
To actually save your work as you go along, just click File > Save All from the menu bar. Or press Ctrl + Shift + S on your keyboard. Or click the icon in the Toolbar (the stack of floppy disks). If you save often then you won't lose any of your work if anything goes wrong with your computer.
In the next section, we'll get down and do a bit of actual programming. It's a gentle introduction, so nothing to get too worried about!
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Creating a New Project
Learning Target: Create a new project
Hands-on Exercise:
Time to get our hands dirty with a bit of programming. There's no putting it off any longer, I'm afraid! We'll create a new project for the next section.
So, if you already have you VB NET software open, you can get rid of the current project by clicking File from the menu bar. From the drop down menu, choose Close Project. You will be returned to the Start Page. Click File > New Project from the menu bars. When you get the dialogue box popping up, choose Windows Forms Application at the top. Then change the Name from WindowsApplication to Variables:
Version 2008
Version 2010
When you click the OK button, a new form will appear.
If you look at the Solution Explorer at the top, you'll see the name of the project has changed to the Name you gave it.
The name of the Project is now Variables - the same name as the folder that is created for you to hold all your project files.
Now that you have a new Project started, we can discuss Variables. We'll do that in the next section.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: How to Create Variables in VB .NET
Learning Target: Create variables in VB.NET
Why are we discussing variables? And what is a variable?
With Visual Basic, and most programming languages, what you are doing is storing things in the computer's memory, and manipulating this store. If you want to add two numbers together, you put the numbers into storage areas and "tell" Visual Basic to add them up. But you can't do this without variables.
So a variable is a storage area of the computer's memory. Think of it like this: a variable is an empty cardboard box. Now, imagine you have a very large room, and in this room you have a whole lot of empty cardboard boxes. Each empty cardboard box is a single variable. To add two numbers together, write the first number on a piece of paper and put the piece of paper into an empty box. Write the second number on a piece of paper and put this second piece of paper in a different cardboard box.
Now, out of all your thousands of empty cardboard boxes two of them contain pieces of paper with numbers on them. To help you remember which of the thousands of boxes hold your numbers, put a sticky label on each of the two boxes. Write "number1" on the first sticky label, and "number2" on the second label.
What have we just done? Well, we've created a large memory area (the room and the cardboard boxes), and we've set up two of the boxes to hold our numbers (two variables). We've also given each of these variables a name (the sticky labels) so that we can remember where they are.
Now examine this:
Dim number1 As Integer
Dim number 2 As Integer
number1 = 3
number2 = 5
That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.
Here's a breakdown of the variable Declaration:
Dim
Short for Dimension. It's a type of variable. You declare (or "tell" Visual Basic) that you are setting up a variable with this word. We'll meet other types of variables later, but for now just remember to start your variable declarations with Dim.
number1
This is the cardboard box and the sticky label all in one. This is a variable. In other words, our storage area. After the Dim word, Visual Basic is looking for the name of your variable. You can call your variable almost anything you like, but there are a few reserved words that VB won't allow. It's good practice to give your variables a name appropriate to what is going in the variable.
As Integer
We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to Integer later.
Number1 = 3
The equals sign is not actually an equals sign. The = sign means assign a value of. In other words, here is where you put something in your variable. We're telling Visual Basic to assign a value of 3 to the variable called number1. Think back to the piece of paper going into the cardboard box. Well, this is the programming equivalent of writing a value on a piece of paper
Now that you have a basic idea of what variables are, let's write a little piece of code to test them out. First, though, let's have our first look at the coding window.
To make life easier, we're going to put a button on our form. When our button is clicked, a little message box will pop up. Fortunately, there's no coding to write for a button, and very little at all for a message box.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Adding a Button to a Form
Learning Target: Add a Button to your VB .NET Forms
Hands-on Exercise:
Instead of double clicking the Button tool in the toolbox to add the control to the form, we'll explore another way to do it.
With your Form displayed in the Visual Basic Design environment, do the following:
• Click on the Button tool in the toolbox with the left hand mouse button, but click only once
• Move your mouse to a blank area of your form - the mouse pointer will turn into a cross
• Press and hold down the left mouse button
• Drag across the form with the button held down
• Let go of the mouse button when you're happy with the size
• A Button is drawn
You can use the above method to draw most of the controls onto the form - labels, Buttons, textboxes, etc.
The Button control, just like all the other controls we've seen so far, has a list of properties. One of these properties is the Text property. At the moment, your button will say "Button 1". You can change that to anything you like.
• Click on the Button to highlight it
• Click on Text in the Property Box
• Click in the box next to the word "Text"
• Delete the word "Button 1"
• Type "Add two numbers"
• Click back on the Form
Now add a Textbox to your form using one of the methods outlined (either double-click, or draw).
Your Form should now look something like this:
The Font property of the Button has also been changed, here, in exactly the same way as we changed the Font property of the Label and Textbox previously. The Text for the Textbox control has had its default Text (Textbox 1) deleted.
To get our first look at the code window, double click your Button control. The code window will appear, and will look like this:
Notice that we've used the underscore character ( _ ) to spread the code over more than one line. You can do this in your own code, too, if it becomes to long. But you don't have to.
The part to concentrate on for the moment is where your cursor is flashing on and off. Because you double-clicked the Button control, the cursor will be flashing between the lines Private Sub … and End Sub.
Here's the part we're concentrating on:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
End Sub
The part of the code we're interested in is highlighted in red in the code above. Notice, too, that the underscore character ( _ ) has been used to spread the code over more than one line. You can do this in your own code, too, if it becomes to long:
Private
Private means that no other part of the programme can see this code except for our button
Sub
Short for Subroutine. The "Sub" word tells VB that some code follows, and that it needs to be executed
Button1
This is the name of our button. You might think that we've just erased the word "Button1" when we changed the Text property, so why does VB insist that it's still called Button1? We'll, the Name property of the control is the important one. If you change the Name property, VB will change this button name for you
_Click ( )
This is something called an Event. In other words, when the button is clicked, the Click Event will fire, and the code we're going to write will be executed
End Sub
The subroutine ends right here. This signifies the end of our code
Don't worry if you don't understand all of that. It will become clearer later. Let's add our code, which we'll do on the next page.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Writing your first .NET code
Learning Target: Write a .NET code
Hands-on Exercise:
In the previous activity, you just designed a form and had your first look at the code window. We'll add some code right now.
Click your mouse on the blank line after Private Sub Button1_Click, etc, but before End Sub. Type the following code:
Dim number1 As Integer
Dim number 2 As Integer
Dim answer As Integer
number1 = 3
number2 = 5
answer = number1 + number2
MsgBox answer
After typing all that, your code window should now look like this:
Before we explore what's happening here, save your work and then click Debug > Start from the Visual Basic Menu, or press F5 on your keyboard. This will launch your programme. Click the Button once, and you should get the following:
Stop your programming, and return to the Design Environment. If you can't see your code, you can click the Tabs at the top of the Window, as in the image below:
Click the "Form1.vb [Design]" tab to see your Form.
OK, what happened there? Well, what happened is we've just wrote a programme to add two numbers together, and we displayed the result by using a Message Box - you very first real programme! But let's break that code down a little more.
• First, we started with the Dim word, indicating to Visual Basic that we wanted to set up a variable
• Then we gave the variable a name (number1)
• Next, we "told" VB that what is going inside the variable is a number (As Integer)
• Two more variable were set up in the same way, number2 and answer
After setting up the three variables, here's what we did:
• Told Visual Basic that what is going into the first variable was the number 3, and what is going into the second variable was the number 5. To put something into a variable, you use the equals ( = ) sign. But it's not really an equals sign - it's an assignment operator. You are assigning the value of 3 to the variable called number1
number1 = 3
number2 = 5
The next part is a little bit more complicated, but not too complicated. What we wanted to do was to add two numbers together. So we said
number1 + number2
Visual Basic already knows how to add up: all we need to do is "tell" it to add up. We do the "telling" in the traditional, mathematical way - with the plus sign (+). What Visual Basic will do is to look at what we've stored inside number1, and look at what's inside number2. It's sees the 3, sees the five, and also sees the plus sign. Then Visual basic adds them up for you.
Except we also did something else. We said to Visual Basic "When you've finished adding up the two variables number1 and number2, store the result in that other variable we set up, which is called answer."
So, the whole line
answer = number1 + number2
means: "Add the variable called number1 to the variable called number2. Then store the result in the variable called answer."
Think of it as working from the right-hand side of the equals sign first. Then when you have the answer, assign it the variable on the left of the equals sign.
The final part of the programme used Visual Basic's in-built Message Box. We'll learn a more about the Message Box later. For now, think of it as a handy way to display results.
Message boxes are quite handy when you want to display the result of some code. But we have a textbox on the form, and we might as well use that.
So delete the line: MsgBox answer. Type the word Textbox1, then type a full stop. You should see a drop-down box appear. This is a list of the Properties and Methods that the Textbox can use.
Scroll down until you see the word "Text". Double click the Text property and the drop-down box will disappear. (This drop-down box is known as IntelliSense, and is very handy. It means you can just select a property or method from the list without having to type anything.)
The Text property you have chosen is the same Text property that you set from the Properties Window earlier. Here, we're setting the property with our code; before, we set it at design time. But the result is the same - the Text property of the textbox will be set to a value of our choosing.
To set a value, type an equals sign, then type a value for the Text property. We want the contents of the variable called answer to appear in the textbox. So the rest of the code is just this:
Textbox1.Text = answer
Your code window should then look like this:
Run your code again, and press the Button on the form. You should see the number 8 appear in the textbox.
OK, time for your first exercises. They're not too painful, and hopefully they'll giver you a better idea of what variables are. And besides, programming is about doing, not talking. So off we go!
Exercise
Delete the values "3" and "5" and replace them with numbers of your own
Exercise
Delete the plus sign in between number1 and number2, and replace them with each of the following in turn
- (the minus sign)
* (the multiplication sign in VB is the asterisk sign)
/ (the divide sign in VB is the forward slash)
Exercise
Set up another Integer variable. Give it the name number3. Assign a value of 10 to this new variable. Multiply the value of your new variable by the variable called answer. Display the result in your textbox.
(Another way to assign values to variables is when you first set them up. You can do this:
Dim number3 As Integer = 10
This is exactly the same as saying:
Dim number3 As Integer
number3 = 10
It's up you which method you use. But the objective is the same - to assign a value to a variable.)
In the next part, we'll about a different kind of variable - a string variable.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: String Variables - An Introduction to Programming Strings
Learning Target:
Hands-on Exercise:
So we've learnt something about variables, what they are and how to set one up. We learnt about the word "integer", and that integer variables held numbers. But what if we don't want numbers? After all, our first Form asked users to type in their First Name and Last Name. Names are not numbers, so what do we do then? Well that's where Strings come in.
What is a String? Actually a string is nothing more than text. And if we want Visual Basic to store text we need to use the word "String". To set up a variable to hold text we need to use As String and not As Integer. If the information we want to store in our variables is a First Name and a Last Name, we can set up two variables like this.
Dim FirstName As String
Dim LastName As String
Again we've started with the Dim word. Then we've called the first variable FirstName. Finally, we've ended the line by telling Visual Basic that we want to store text in the variable - As String.
So we've set up the variables. But there is nothing stored in them yet. We store something in a variable with the equals sign ( = ). Let's store a first name and a last name in them
FirstName = "Bill"
LastName = "Gates"
Here, we said to Visual Basic "Store the word 'Bill' into the variable FirstName and store the word 'Gates' into the variable called LastName. But pay attention to the quotation marks surrounding the two words. We didn't say Bill, we said "Bill". Visual Basic needs the two double quotation marks before it can identify your text, your String.
So remember: if you're storing text in a variable, don't forget the quotation marks!
To test all this out, add a new Button to your Form. Set the Text property of the Button to "String Test". Your Form would then look like this:
Double click your new button, and add the following code:
Dim FirstName As String
Dim LastName As String
Dim FullName As String
FirstName = "Bill"
LastName = "Gates"
FullName = FirstName & LastName
Textbox1.Text = FullName
Your code window should now look like this (some of the first line has been cropped in the image below):
There's a line there that needs explaining
FullName = FirstName & LastName
In the two lines of code above that one, we stored the string "Bill" and the string "Gates" into two variables. What we're doing now is joining those two variables together. We do this with the ampersand symbol ( & ). The ampersand is used to join strings together. It's called Concatenation.
Once Visual Basic has joined the two strings together (or concatenated them), we're saying "store the result in the variable called FullName". After that, we tell VB to display the result in our Textbox.
So, once you've typed the code, start your programme and test it out.
Once the programme is running, Click the Button and see what happens. You should have a Form that looks something like this one:
The textbox displays the text stored in our variables, "Bill" and "Gates". We joined them together with the ampersand ( & ). But as you can see, the two words are actually joined as one. We can add a bit of space between the two words by using another ampersand. Change this line FullName = FirstName & LastName to this:
FullName = FirstName & " " & LastName
What we're saying here is join this lot together: the variable called FirstName and a single blank space and the variable called LastName. When you've finished concatenating it all, store the result in the variable FullName.
Notice that we don't surround FirstName and LastName with quotation marks. This is because these two are already string variables; we stored "Bill" into FirstName and "Gates" LastName. So VB already knows that they are text.
Exercise
Remove one of the ampersand symbols (&) from this line in your code:
FullName = FirstName & " " & LastName
Move your cursor down a line or two. You should see that part of your code has a wiggly blue line under it:
VB is telling you that it has problems with this line of code. If you hold your mouse over the wiggly blue line, VB tries to provide an explanation:
The explanations VB provides are sometimes enigmatic. But you will know that there is a problem. If you run the code, you'll get this popping up at you:
Click the NO button. Put the ampersand back in, and all will be well.
Exercise
Amend your code so that the textbox reads Gates Bill when the Command button is clicked.
Exercise
Add another string variable to your code. The variable should hold a middle name. Display the first name, the middle name and the last name in the textbox.
Points to remember:
• Your variable names cannot include spaces. So the variable MiddleName would be all right, but Middle Name will get you an error message
• When you're putting text into your new variable, don't forget the two double quotes
• Remember to put in enough ampersands in your FullName = line of code
In the next part, we'll take a look at how to assign text from a textbox into our string variables.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Working with Textboxes
Learning Target: Assign textbox text to variables
Hands-on Exercise:
Instead putting direct text into your variables, such as "Bill" or "Gates", you can get text from a textbox and put that straight into your variables. We'll see how that's done now. First, do this:
• Add a new textbox to your form
• With the textbox selected, locate the Name property in the Properties area:
The current value of the Name property is Textbox2. This is not terribly descriptive. Delete this name and enter txtLastName. Scroll down and locate the Text property. Delete the default text, and just leave it blank.
Click on your first textbox to select it. Change the Name property from Textbox1 to txtFirstName.
What we've done is to give the two textboxes more descriptive names. This will help us to remember what is meant to go in them.
Unfortunately, if you view your code (click the Form1.vb tab at the top, or press F7 on your keyboard), you'll see that the blue wiggly lines have returned:
If you hold your cursor of the Textbox1, you'll see this:
It's displaying this message because you changed the name of your Textbox1. You now no longer have a textbox with this name. In the code above, change Textbox1 into txtFirstName and the wiggly lines will go away. (Change it in your Button1 code as well.) Your code should now read:
txtFirstName.Text = FullName
Run your programme again. If you see any error messages, stop the programme and look for the wiggly lines in your code.
We'll now change our code slightly, and make use of the second textbox. You'll see how to get at the text that a user enters.
Locate these two lines of code
FirstName = "Bill"
LastName = "Gates"
Change them to this
FirstName = txtFirstName.Text
LastName = txtLastName.Text
Remember: the equals ( = ) sign assigns things: Whatever is on the right of the equals sign gets assigned to whatever is on the left. What we're doing now is assigning the text from the textboxes directly into the two variables.
Amend your code slightly so that the Whole Name is now displayed in a message box. Your code should now be this:
Dim FirstName As String
Dim LastName As String
Dim WholeName As String
FirstName = txtFirstName.Text
LastName = txtLastName.Text
WholeName = FirstName & " " & LastName
MsgBox(WholeName)
Run your programme. Enter "Bill" in the first textbox, and "Gates" in the second textbox. Then click your "String Test" button. You should get this:
Before we changed the code, we were putting a person's name straight in to the variable FirstName
FirstName = "Bill"
But what we really want to do is get a person's name directly from the textbox. This will make life a whole lot easier for us. After all, not everybody is called Bill Gates! In the line FirstName = txtFirstName.Text that is what we're doing - getting the name directly from the textbox. What we're saying to Visual Basic is this
• Look for a Textbox that has the Name txtFirstName
• Locate the Text property of the Textbox that has the Name txtFirstName
• Read whatever this Text property is
• Put this Text property into the variable FirstName
And that's all there is too reading values from a textbox - just access its Text property, and then pop it into a variable.
Exercise
• Add a third textbox to your form
• Change its Name property to txtWholeName
• Add labels to your form identifying each textbox (A quick way to add more labels is to use the toolbox to add one label. Then right click on that label. Choose Copy from the menu. Right click on the form, and select Paste.)
• Write code so that when the "String Test" button is clicked, the whole of the persons name is displayed in your new textbox
When you complete this exercise, your form should look like this one (we've deleted the first button and its code, but you don't have to):
In the next part, we'll explore some more variable types you can use.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: More about Variables in VB NET
Learning Target: Use more variables in VB NET
Hands-on Exercise:
We've met two variable types so far - As String and As Integer. But there are quite a few more you can use. Let's start by examining number variables.
Start a new project for this. If you have the old one displayed, you can click File > Close Solution from the menu bar. You will then be returned to the Start Page. Click the New Project button at the bottom. In the dialogue box, give your project a name.
Put a textbox and a Button on your new form. Change the Properties of the Textbox to the following
Name: txtNumbers
Font: MS Sans Serif, Bold, 10
Text: just delete the default Textbox1, and leave the textbox blank
Change the Properties of the Button to the following:
Text: Answers
Font: MS Sans Serif, Bold, 10
Click on the Form itself, and change it's Text property to "Testing Types". Your Form should look something like this:
Double click on the Button to bring up the code window. Type the following code for your Button (The Button1_Click part is spread over three lines only for ease-of-reading on this web page. You can keep yours on one line in your code):
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim testNumber As Short
testNumber = txtNumbers.Text
MsgBox testNumber
End Sub
Notice that there is a new Type of variable declared - As Short. This means "Short Integer". We'll see what it does.
Run your programme. While it's running, do the following:
• Enter the number 1 into the textbox, and click the Answers button
• The number 1 should display in the Message Box
• Add the number 2 to the textbox and click the Button
• The number 12 should display in the Message Box
• Add the number 3 to the textbox and click the Button
• The number 123 should display in the Message Box
• Keeping adding numbers one at a time, then clicking the button
How many numbers did you get in the textbox before the following error message was displayed? (Click Break to get rid of it.)
You should have been able to enter 12345 quite safely. When you entered 123456 and clicked the button, that's when the error message displayed.
When you click the Break button, you are returned to the coding environment. You'll see the problem line highlighted in yellow:
But your programme will still be running. So click Debug > Stop Debugging to return to the normal code window.
An Overflow happens when you try to put too much information into a variable that can't handle it.
The reason we got an error message after just 6 numbers was because of the variable type. We had this
Dim testNumber As Short
And it's As Short that is causing us the problems. If you use As Short you're only allowed numbers up to a certain value. The range for a Short variable is -32 768 to 32 767. When we entered 6 numbers, Visual Basic decided it didn't want to know. If you run your programme again, and then enter 32768, you'll get the same Overflow error message. If you change it once more to -32769, you'll get the error message as well. So it's not just 6 numbers a Short Type can't handle - it's 5 numbers above or below the values specified.
So what's the solution? Change the variable Type, of course!
Change the variable to this
Dim testNumber As Integer
Now start the programme and try again, adding numbers one at a time to the textbox, and then clicking the Command button. How far did you get this time?
If you started at 1 and added the numbers in sequence, you should have been allowed to enter 1234567890. One more number and Visual Basic gave you the Overflow error message, right? That's because variable types with As Integer also have a limitation. The range you can use with the As Integer variable type is -2,147,483,648 to 2,147,483,647. If you want a really, really big number you can use As Long.
Dim testNumber As Long
But these will get you whole numbers. Leave your number on As Integer. Run your programme again and enter a value of 123.45 into your textbox. Press the button and see what happens.
VB will chop off the point 45 bit at the end. If you want to work with floating point numbers (the .45 bit), there are three Types you can use:
Dim testNumber As Single
Dim testNumber As Double
Dim testNumber As Decimal
Single and Double mean Single-Precision and Double-Precision numbers. If you want to do scientific calculations, and you need to be really precise, then use Double rather than Single: it's more accurate.
The As Decimal Type is useful when you want a precise number of decimal places. It's not as accurate as the Double Type, though.
In terms of the space used in the computer's memory, Short Types use 2 Bytes, Integer Types use 4 Bytes, Long Types use 8 Bytes, Single Types use 4 Bytes, Double Types use 8 Bytes, and Decimal Types use 16 Bytes.
Exercise
Write a programme to calculate the following sum.
0.123345678 * 1234
Use the Single Type first, then change it to As Double. Use a Message box to display the answer. Was the number rounded up or rounded down for the Single Type?
In the next part, we'll get some more practise with variables.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: Using Variables in your NET Code
Learning Target: Use variables in NET code
Hands-on Exercise:
In this next section, we're going to learn how to transfer the contents of one textbox to another textbox. We'll also learn to transfer the text from a label to a textbox, and whatever was in the textbox we'll transfer it to a label. This will get us a little more practise with variables, and how to use them.
Ok, start a new Visual basic project. You should know how to do this by now, and what the design environment looks like. But you should have a plain grey Form on your screen. By default it will be called Form1.
Make sure the Form is selected (has it got the white squares around it?), and the click the Name property in the Properties window. Change the Name of the form to frmVariables.
Set the Text property of the Form to "Transferring information". You can choose any background colour you like for the form, or leave it on the default.
Put the following controls on the Form, and change their properties to the one's specified below (NOTE: lbl is short for label):
Textbox
Name: txtVariables
Font: MS Sans Serif, Bold, 10
Text Delete the default text "Text1" and leave it blank
Label
Name: lblTransfer
BackColor: A colour of your choice
Text: Label Caption
Font: MS Sans Serif, Bold, 10
Button
Name: btnTransfer
Text: Transfer
The height of your controls is entirely up to you.
If you double click your Button to bring up the code window, you will see that the first line of the code no longer says Button1_Click (etc). The first line should say this
Private Sub btnTransfer_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnTransfer.Click
End Sub
The reason it has changed is because you changed the Name property of the Button. The button now has the Name btnTransfer. If you wanted to, you could change the Name property back to Button1. Then when you double clicked the button, the code window would pop up and the first line would be Button1_Click(etc ).
What we're going to do now is to transfer the Text on the label ("Label Caption") to our empty textbox. And all with the click of a button.
As you'll see, there isn't much code.
Type the following into your code window:
Dim LabelContents As String
LabelContents = lblTransfer.Text
txtVariables.Text = LabelContents
Your code window should now look something like this:
Now Run your programme and test it out. When you click on the "Transfer" button, you should see that the contents of the label will be inserted into the textbox:
But let's break the code down and see what's going on.
Dim LabelContents As String
Here is where we set up a variable called LabelContents. Because it will be holding text, we've used the variable type As String.
LabelContents = lblTransfer.Text
Here is where we put something into our empty variable. We changed the Name property of our Label from the default Label1 to lblTransfer. A Label has lots of properties you can manipulate. One of those properties is the Text property. After you typed the word "lblTransfer" and then typed a full stop, you probably saw a drop down box appear. Inside the box is a list of all the properties and Methods that a Label has. We wanted to manipulate the Text property of our label so we selected the word Text after the full stop. So we were saying "Access the value of the Text property of the label called lblTransfer, and put this value into the variable called LabelContents." Because our Text was ""Label Caption", the variable LabelContents now holds the text "Label Caption."
txtVariables.Text = LabelContents
Finally, we want to transfer whatever is in the variable LabelContents to the Textbox. Our Textbox is called txtVariables. Again, after typing the full stop the drop down box would appear, showing you a list of all the properties that a Textbox has. The one we're interested in is the Text Property. So we're saying, "Take whatever text is in the variable LabelContents, and transfer it to the Text property of the Textbox called txtVariables.
And with three lines of code we can transfer text from a label to a Textbox. But can we do it the other way round? Can we transfer whatever is in a Textbox to a Label? Well, sure we can.
Add another button to your form. Change its Name property from Button1 to cmdTransferToLabel, and change the Caption property to "Transfer To Label". Again, there's just three lines of code.
So double click your new button to bring up the code window. Then type in the following code:
Dim TextBoxContents As String
TextBoxContents = txtVariables.Text
lblTransfer.Text = TextBoxContents
Now, see if you can work out how it works. It's the same thing as the first three lines of code: set up a variable, transfer the Text property of the Textbox to the variable, transfer the variable to the Text property of the Label. Run your programme and test it out. Type something into the Textbox, and then click the "Transfer To Label" button.
Exercise
A button also has a Text Property. Write code to transfer the Text property of a button to the Textbox. It's probably better for this exercise to create a new Button. Set its Name property to whatever you like. And give its Text Property a new value (The Text property will be Button1 by default) .
But the process is exactly the same as the two bits of code above - you should only need 3 lines of code for this exercise.
• Set up a Variable
• Transfer the Text property of the button to the variable
• Transfer the variable to the Textbox
In the next part, we'll start a Calculator project. This will get you some more practical experience of working with variables.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: A Calculator Project in VB NET
Learning Target: Design a calculator
Hands-on Exercise:
In the next few pages, you're going to create a Calculator. It won't be a very sophisticated calculator, and the only thing it can do is add up. What the project will give you is more confidence in using variables, and shifting values from one control to another. So create a new project, call it Calculator, and let's get started.
Designing the Form
Let's design the form first. What does a calculator need? Well numbers, for one. A display area for the result. A plus sign button, an equals sign button, and a clear the display button.
Here's how our calculator is going to work. We'll have 10 button for the numbers 0 to 9. When a button is clicked its value will be transferred to a display area, which will be a Textbox. Once a number is transferred to the Textbox we can click on the Plus button. Then we need to click back on another number. To get the answer, we'll click on the equals sign. To clear the display, we'll have a Clear button.
If you haven't already, create a new project. Save it as Calculator. To your new form, first add ten Buttons (You can add one, then copy and paste the rest). The Buttons should have the following Properties:
Name: btn Plus a Number (btnOne, btnTwo, btnThree, etc)
Text: A number from 0 to 9. A different one for each button, obviously
Font: MS Sans Serif, Bold, 14
Next, add a Textbox. Set the following properties for the Textbox:
Textbox
Name: txtDisplay
Font: MS Sans Serif, Bold, 14
Text: Erase the default, Textbox1, and leave it blank
Three more Command buttons need to be added
Plus Button
Name cmdPlus
Font MS Sans Serif, Bold, 14
Text +
Equals Button
Name cmdEquals
Font MS Sans Serif, Bold, 14
Text =
Clear Button
Name cmdClear
Font MS Sans Serif, Bold, 14
Text Clear
When your form design is finished, it might look something like this:
So if you wanted to add 5 + 9, you would click first on the 5. A 5 would appear in the textbox. Then you would click the + symbol. The 5 would disappear from the textbox. Next, click on the 9. The number 9 would appear in the textbox. Finally, click on the = symbol. The 9 would disappear from the textbox, and the answer to our sum would replace it. We would then click the Clear button to clear the display.
In the next section, we'll make a start on the VB NET code for the all those buttons.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: The Code for the VB NET Calculator
Learning Target: Code for the VB NET calculator
Hands-on Exercise:
In the previous part, you designed the NET form for your calculator. We'll now make a start on the code.
You might be thinking that all this is terribly complicated at such an early stage. But it isn't really. All we are doing is transferring the Text Properties from the Buttons to the textbox. And you already know how to do that. The number buttons don't do anything else. All the work is done with the Plus button and the Equals buttons. And there's only two lines of code needed for the Plus button, and three for the Equals button.
For this to work, though, a little word about Scope in VB NET.
So far, when you've set up a variable, you've set them up behind a Private Subroutine. Like this:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click
Dim MyVariable As String
End Sub
Suppose you had another button on the form, Button2, and the code was this
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click
Dim MyOtherVariable As String
End Sub
How can you access what's in MyVariable from Button2? The answer is, you can't. It's like two people sitting at desks in cubicles. Each person has written something on a piece of paper. They can't see into the other person's cubicle, only whatever is their own cubicle. So how do they share their information?
Well suppose there is a screen in front of them. A big screen. They can both see the screen in front of them; it's each other they can't see. What they could do is project their information onto the screen. Then one person could see what the other has written.
Similarly, in VB you can set up your variable declarations outside of the code for a Button. That way, more than one Button can see the code.
You can place your variable declarations right at the top of the code window, just beneath the line that begins "Public Class Form1". We'll set up two Integer variables there, total1 and total2:
Now all the controls on your form can see these two variables. Those Buttons you set up can put something in them, and every button has the ability to see what's inside them.
The 0 to 9 Buttons
The Buttons with the Text 0 to 9 only need to do one thing when the button is clicked - have their Text Properties transferred to the Textbox. You've already wrote code to do that.
So double click the 0 Button and enter the following code:
Private Sub btnZero_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnZero.Click
txtDisplay.Text = btnZero.Text
End Sub
This code will transfer the Text Property of a Button called btnZero to the Text Property of a Textbox called txtDisplay.
Run your programme and try it out. When the programme is running, click the 0 button to see that it does indeed transfer the Text on the Button to the textbox
Except, there's a problem with that code. If you wrote similar code for all ten of your number buttons, the calculator wouldn't be right. Why is that? Have you spotted what's wrong? It's a good idea to set this book aside for a while and think about why this code on it's own wouldn't work. In fact you could write code for a few more of the number buttons and test it out.
What happens when you transfer the number 2 to the Textbox, and then click the number 3? The number 2 will disappear, to be replaced by the number 3. Which is all right if all you wanted to do was add up single numbers, but not much good if you actually wanted the number 23 in the Textbox. With this code, you could have either the number 2 in the Textbox or the number 3, but not both!
So how do we solve this problem? How do we fix it so that we can have two or more numbers appearing in our Textbox?
What we need is a way to get whatever is in the Textbox to stay where it is, and not disappear on us when we click a different number. It's quite easy. It's this:
txtDisplay.Text = txtDisplay.Text & btnZero.Text
So now we're saying the textbox doesn't just contain the Text on the Button. It must also keep whatever is inside the textbox as well.
So what you need to do now is to add that code to all of your ten number Buttons. Obviously it won't be exactly the same. For the button called btnOne the code would be this:
txtDisplay.Text = txtDisplay.Text & btnOne.Text
When you've finished coding all ten buttons, run the programme and click all ten number button to see if they do indeed transfer the numbers on the caption to the textbox. Not only that, but test to see if you can have more than one number in the textbox.
Now that we can get numbers into our Textbox display area, we'll write code to do something with those numbers - add them together, in other words. We'll do that in the next part.
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title: The Message Box in VB .NET
Learning Target: Use the message box in VB NET
Hands-on Exercise:
The message box function you have used so far is the old message box function. It is the one left over form VB6:
MsgBox("Your Message Here")
The new VB.NET message box function is very similar, but the way you use it is slightly different. It's this:
MessageBox.Show("Your Message Here")
So you type the word "MessageBox" then a full stop. Double click the "Show" method on the menu the appears. Then type a round bracket. You should get a rather long and complex tool tip appearing. In fact, it's too long to even fit on this page!
What it all means is there are options you can use with your message box. The first one is "text As String". The text in question is the text that will appear for your message - the message itself, in other words. The next one is "caption As String". This sets the white caption at the top of the message box.
So if your message box function was this:
MessageBox.Show("This menu will Undo an Operation", "Undo")
You would get this message box popping up:
Each option for your message box is separated by a comma. If you type a comma after the "Undo" in the code above, you'll get another pop-up menu. On this menu, you can specify which buttons you want on your message box:
If you only need the OK button on your message boxes, then double click this item, then type a comma. Yet another pop-up menu will appear. On this menu, you can specify the symbol that appear in the message box:
It's up to you which symbol you choose. Experiment with all of them and see what they look like. In the image below, we've gone for the Information symbol:
Compare the message box above with the one we had earlier:
In a real programme, you should use the new MessageBox.Show( ) function, rather than the MsgBox() we used (and will again because it saves on typing and space!)
In the next section, we'll move on to Conditional Logic
Technology and Livelihood Education IV
(Information and Communication Technology II)
School Year & Section
Name Date
Activity Title:
Learning Target:
Hands-on Exercise:
What is Microsoft Access?
MS Access is a Database Management System. It handles data management tasks the same way as MS Word handles document management and MS Excel handles statistics ...
A database is a collection of objects that allow you to store data, organize it and retrieve it in any way you want.
What this means is that, with Microsoft Access you create structures called tables that allow you to organize the data so that it's easy to find later, you create forms that let you input the data into the tables and then you create reports that print selected information from the tables.
For example, if you run a store, you would create a Customers table, a Products table and an Invoices table. Then, when you open an account for a new customer you would have a Customer form to input a customer's data into the Customers table and an Order form to input the purchase information. Later, you could print any number of Sales reports, grouping and arranging the information from the Invoices, Customers and Products tables to analyze daily or weekly or monthly sales in all kinds of combinations.
To help you along, Access contains a whole series of Wizards to guide you through the process.
Course description
This Microsoft Access course follows a step-by-step approach to the creation and development of a commercial database application.
We'll start with database modeling. That means that you have to design the database before you actually start to write it. There are several basic techniques that must be learned to ensure that the database structure will be solid. Design is an absolutely essential part of creating a database.
If you're already past the rookie stage and you want to delve deeper into the database modeling aspect, even before you start with Access, you might want to take a look at our Database design and SQL language tutorial. In a normal sequence of courses, SQL would be the next database subject you would learn after Access. Whereas Access is meant for the smaller user, SQL lets you into the domain of the power-user.
Once we're done with the design we'll develop the objects one by one and learn how to use them.
As we go along we'll use a simple application to illustrate the power of Microsoft Access in business. The application is a Video Rental Store and it's well suited to showing how a small businees could put into practice all these notions of management with databases.
After this Microsoft Access tutorial, you may want to go on to bigger and better databases such as Oracle, SQL Server or MySQL. The training you get from this tutorial will qualify you for the more advanced stuff.
You may also want to look at connecting your Microsoft Access database to a Visual Basic application.
By the end of this Microsoft Access database course, you will have made a good start towards becoming a full-fledged business applications programmer.
This course is on Application Development with Microsoft Access. It's not just about writing code. It's about finding the best solutions for data storage and retrieval requirements in organizations.
Basic concepts
Let's define what we will be doing:
• Application: a series of programs or computer codes that execute tasks that a user wants done.
Maintaining a list of your friends' addresses and phone numbers is a personal application.
Producing the weekly payroll for 100 employees of a business is a commercial application.
Running a computer model to forecast tomorrow's weather is a scientific application.
We will look at commercial applications only. We'll leave the scientific stuff to the people at M.I.T.
• Development: design, create, make, build all the parts of the application.
Analyze: talk to the client (the user) to find-out what he wants. If you don't have a user handy, use your imagination and prior knowledge to guess what a user would want.
Design: create a model of the system. A model is like a blueprint to a builder. It is a drawing or a description of some kind of what the system will look like when finished. When you determine how things will work and how problems will be solved. You do that before you start to write computer code.
Create: write the application using the software - Access, in this case. And while you're creating you're also testing and debugging to make sure that what you create works the way it's supposed to work.
• Microsoft Access: Access is part of Microsoft Office. However, it is not included in the basic suite with Word and Excel. You need to get Microsoft Office Professional Edition to have Access.
Obviously, if you intend to do this tutorial we have to assume that you have access to Access. It really is impossible to do otherwise.
We are using Access 2000. You could do just as well with Access 97. Any application created in Access 2000 can be converted to the previous version of Access and any application created in Access 97 can be used in Access 2000. The sample applications are all in Access 2000.
____________________________________________________________________________________
Visual Basic .NET Programming for Beginners
This computer course is an introduction to Visual Basic.NET programming for beginners. This course assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby!
You don't need to buy any software for this course! You can use the new FREE Visual Basic Express Edition from Microsoft. You can download it here: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express
One - Getting Started
1. Getting started with VB.NET
2. Visual Basic .NET Forms
3. Adding Controls using the Toolbox
4. Adding a Textbox to the Form
5. Visual Basic .NET and Properties
6. The Text Property
7. Adding a splash of colour
8. Saving your work
9. Create a New Project
Two- Write your first .NET code
1. What is a Variable?
2. Add a coding button to the Form
3. Writing your first .NET code
4. String Variables
5. How to get at Text in a textbox
6. More about VB .NET variables
7. Using variables in your .NET code
8. A VB NET Calculator Project
9. The code for the calculator
10. The Message Box in VB .NET
Three - Conditional Logic
1. If Statements
2. Select Case Statements
3. Add a Combo Box to a VB .NET form
4. The Conditional Operators
5. Section Three Exercises
Four- Loops
1. An Introduction to Loops in VB .NET
2. For Loops
3. Do Loops
4. A Times Table Programme
5. The Code for the Time Table Programme
6. The Basic Math Symbols in VB .NET
Five - Adding menus to Forms
1. Add a menu to a VB .NET Form
2. How to add code to a Menu
3. How to add a Sub Menu to your Form
4. How to add Shortcuts to your Menu Items
5. A VB .NET menu Project
6. The Open File Dialogue Box
7. Filter files with the Open File Dialogue Box
8. The Open File Dialogue Box
9. The Save File Dialogue Box
10. Cut, Copy, Paste and Undo menus
11. How to Show and Hide Controls
12. Insert Images into a Picture Box
13. Add a Checkbox to a VB .NET form
14. Writing code for Checkboxes
15. Add Option Buttons to a VB .NET form
Six - Debugging your code
1. Error Handling and Debugging in VB .NET
2. Design Time Errors
3. RunTime Errors
4. Try ... Catch in VB .NET
5. Logic Errors
6.Breakpoints and Debugging tools
Seven - Mastering Arrays
1. What is an Array?
2. Arrays and the Index Number
3. Assigning Values to an Array
4. Arrays where boundaries are not known
Eight - String Manipulation
1. The String Variable Type
2. How to use the Trim Method
3. The difference between Char and Chars()
4. How to use the InStr Method
5. How to use the Substring Method
6. Equals, Replace and Insert Methods
7. How to use Split and Join in VB .NET
Nine - Working with Text Files
1. What is a Text File?
2. How to Open a Text File in VB .NET
3. How to Read a Text File Line by Line
4. How to Write to a Text File in VB .NET
5. Appending Text to a File in VB .NET
6. How to Copy a File
7. How to Move a File
8. How to Delete a File
Ten - Functions and Subs
1. An Introduction to Functions and Subs
2. How to Create your own Subs in VB .NET
3. Using Parameters in your Subs
4. ByVal and ByRef in VB .NET
5. How to Create a Function in VB.NET
6. How to use Parameters with Functions
7. Standard Modules - Part One
8. Standard Modules - Part Two
Eleven - Events
1. The Click Event
2. The MouseDown Event
3. The KeyDown Event
4. The Form Load Event
Twelve - Classes and Objects
1. An Introduction to Classes and Objects
2. Create your own Classes in VB .NET
3. How to Create Methods in your Classes
4. More about Creating Methods
5. Create Properties in your Classes
3. How to Use your New Property
Thirteen - VB .NET and Databases
1. VB NET Express and Databases
2. The Database Wizard in VB NET Express
3. Write your own VB .NET database code
4. Learn about DataSets and Data Adaptors
5. Display the Data in the DataSet
6. Navigate a Database with VB .NET
7. How to Move through the Database
8. Add, Update and Delete Records
9. Add a New Record using VB .NET
10. Delete a Record using VB .NET
Fourteen - VB NET and Forms
1. Anchor and Dock Controls on a Form
2. Add a Toolbar to a Form
3. Creating Multiple Forms in VB .NET
4. Modal and Non Modal Forms
5. Getting at Values on Other Forms
INTRODUCTION TO MICROSOFT VISUAL BASIC
Microsoft Visual Basic is a “visual programming” environment for developing Windows applications. It is a high level programming language that evolved from the earlier D.O.S. ( Disk Operating System) version called B,A.S.I.C. (Beginners All- purpose Symbolic Instruction Code). “BASIC” uses a text environment and the program executes sequentially . “Visual Basic” uses a graphical environment (Graphical User Interface or GUI) that makes it easy to develop applications. It provides an interesting set of tools where the programmer may “drag and drop controls” and spend lesser time in coding. It uses common English phrases, making it one of the most popular programming languages today.
Visual Basic is also an “event driven” programming language, whereby procedures are automatically called whenever the end user chooses menu items, clicks the mouse, moves objects on screen, and other functions that a programmer may define during coding. What could take days to create applications in other programming languages may take only hours for Visual Basic because of this feature.
The First Edition of Visual Basic was released by Microsoft in 1987, while C, Pascal and C++ programming languages were already in existence. It was one of the first products to provide a graphical programming environment. It has three editions, namely, the Standard Edition, the Professional Edition, and the Enterprise Edition. The standard Edition, also called the learning edition, gives you a complete development environment but lacks tools that the Professional Edition has, including add-in tools help file compiler, a better database access tools, and better web programming support. The Enterprise Edition provides extended and advanced tools for client/ server platform. For beginners who are learning how to develop programs, the Standard or the Professional Edition may be used.
In the real world application, you can see a lot of business processes where programming is being applied. For instance, majority of private schools are using computerized registration system. Another application could be a typical payroll system that is used to compute for the compensation of employees or a bank transaction whereby a client deposits or withdraws money from his/her account. All these can be done using any GUI-based application such as Visual Basic.
References:
http://www.homeandlearn.co.uk/NET/vbNet.html
HTML QUESTIONS AND ANSWERS
1) Is it possible to set up a browser so it refuse pages that does not have a content rating meta tag?
a) Yes - many companies and parents actually do
b) No
c) Only with a cybersitter add on
2) When images are used as links they get a blue border.
a) Always
b) Never
c) Unless border is set to zero
3) A 6 digit Hex color (#FF9966) defines values of Red, Blue and Green in which order ?
a) #BBRRGG
b) #BBGGRR
c) #RRGGBB
4) When you count to 15 using hexadecimal numbers, the highest number is what ?
a) F
b) B
c) 15
5) The and tags are special in what way ?
a) They can be repeated
b) They work on anything
c) They are for images only
6) What does vlink mean ?
a) visited link
b) very good link
c) active link
7) Banners, buttons, dividers, clipart and other simple images usually work best as ?
a) fonts
b) gif
c) jpg
8) Which format usually works best for photos ?
a) JPG
b) HTML
c) GIF
9) and are the tags used for ?
a) Audio-voiced text
b) Adding links to your page
c) Aligning text
10) What does the GENERATOR meta tag tell ?
a) What type of server your page is on
b) Which program was used to produce the page
c) Who designed the page
11) What tag is used to add columns to tables ?
a)
b)
c)
12) Use and to add what to your tables?
a) columns
b) rows
c) steps
13) What is the REFRESH meta tag used for ?
a) Refresh your keywords
b) Allow search engines to relist your page
c) Redirect to a new domain
14) Screen colors are defined by which colors ?
a) Green, Blue, and Yellow
b) Crayola Colors
c) Red, Green and Blue
15) What tag can prevent sites with adult content from being seen on MSIEbrowsers ?
a) tag
b) tag
c) icra.org registered
16) To specify a font for your whole page add which tag ?
a)
b)
c)
17) Increasing the cellpadding value will what ?
a) Increase the distance between cell and content
b) Increase the space between cells
c) Increase the softness of your site
Show Answer
________________________________________
18) Which of the following is NOT true of metatags use ?
a) Which of the following is NOT true of metatags use ?
b) Do not include words that are not present on your pages
c) Use as many keywords as you can
Show Answer
________________________________________
19) To change the size of an image in HTML use what ?
a) pliers
b) height and width
c) bigger and smaller
Show Answer
________________________________________
20) Hex-colors are the only way to define colors on the web?
a) True for Internet Explorer, False for Netscape browsers
b) False, colors can also be specified with names and CSS
c) True, computer screens only understands hexadecimal values.
Show Answer
________________________________________
21) Is it a common myth that meta tags seriously improve search engine rankings ?
a) no
b) only in Nepal
c) yes
Show Answer
________________________________________
22) Choose the correct HTML tag to make a text italic
a)
b)
Show Answer
________________________________________
23) Choose the correct HTML tag to make a text bold
a)
b)
Show Answer
________________________________________
24) If the background image is smaller than the screen, what will happen ?
a) It will be stretched
b) It will leave a blank space at the bottom of your page
c) It will be repeated
Show Answer
________________________________________
25) HTML defines colors using hexidecimal values, while graphics programs most often use what ?
a) Names
b) Normal numbers
c) RGB Code
Show Answer
________________________________________
26) The
a) Head
b) Arm
c) Body
Show Answer
________________________________________
27) If you don’t want the frame windows to be resizeable, simply add what to the lines ?
a) save
b) dontresize
c) noresize
Show Answer
________________________________________
28)How can you make a list that lists the items with numbers?
a)
b)
c)
d)
Show Answer
________________________________________
29) Most search engines give serious importance to meta tags when ranking websites in their listings ?
a) True
b) It depends on the keywords
c) False
Show Answer
________________________________________
30) Which colors consist of equal amounts of all basic colors ?
a) purple, green, and red
b) black, blue, and gray
c) white, black and gray
Show Answer
________________________________________
31) Which has higher priority, cell settings or table settings ?
a) Neither
b) Cell settings
c) Table settings
Show Answer
________________________________________
32)What does HTML stand for?
a) Hyper Text Markup Language
b) Hyperlinks and Text Markup Language
c) Home Tool Markup Language
Show Answer
________________________________________
33) What is the correct HTML for adding a background color?
a)
b)
c)
Show Answer
________________________________________
34) To make the appearance of the colors more powerful on your site do which of the following ?
a) Do not use colors
b) Limit their use
c) Splash them all over
Show Answer
________________________________________
35) When is the content of a table shown ?
a) In pieces as it loads
b) Before the border loads
c) After the table is loaded
Show Answer
________________________________________
36) To add rows to your tables use which tags?
a) and
b)
c) and
Show Answer
________________________________________
37) Gif and jpg are the two main types of what ?
a) animated effects
b) outlines
c) images
Show Answer
________________________________________
38) WYSIWYG stands for ?
a) What You See Is What You Get
b) When You Start Is When You Go
c) What You See Is What You Gain
Show Answer
________________________________________
39) Choose the correct HTML tag for the largest heading
a)
b)
c)
d)
Show Answer
________________________________________
40) Choose the correct HTML to left-align the content inside a tablecell
a)
b)
c)
d)
Show Answer
________________________________________
41) To create a bulleted list use ?
a)
b)
c)
Show Answer
________________________________________
42) Besides, another way to make text bold is what ?
a)
b)
c)
Show Answer
________________________________________
43) Hexadecimal numbers are numbers based on the value of what ?
a) 10
b) 8
c) 16
Show Answer
________________________________________
44) Which is NOT a predefined target for links ?
a) _son
b) _parent
c) _blank
Show Answer
________________________________________
45) If you do not include a DESCRIPTION meta tag, most search engine will simply list what ?
a) Nothing
b) All text above the fold of your page
c) The first few words on the page
Show Answer
________________________________________
46) How many characters can be written with 1 Kilobyte ?
a) 1024
b) 1
c) Depends on the font used
Show Answer
________________________________________
47)To start a list at the count of 3, use which ?
a)
b)
c)
Show Answer
________________________________________
48) Who is making the Web standards?
a) The World Wide Web Consortium
b) Mozilla
c) Microsoft
Show Answer
________________________________________
49) Which tag can set the background color for your page?
a)
b)
c)
Show Answer
________________________________________
50) What is the correct HTML tag for inserting a line break?
a)
b)
c)
Show Answer
________________________________________
51) The attribute used to choose the type of font in HTML is ?
a) Character
b) Face
c) Text-type
Show Answer
________________________________________
52) colspan=n can be added to only what tag?
a)
b)
c)
Show Answer
________________________________________
53) Rather than using Hspace and Vspace you can use which of the following to add spacing to your image ?
a) height and width
b) 1x1 pixel transparent image
c) align=+2
Show Answer
________________________________________
54) What is the correct HTML for creating a hyperlink?
a)Qualitypoint Technologies
b) Qualitypoint Technologies
c) http://www.qualitypointtech.com
d) Qualitypoint Technologies
Show Answer
________________________________________
55) In the code