Or This or Even This

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Tool Bar It consists of the dropdown menus: File, Edit, View, Project, Build, Debug, Data, Tools, Window,

, and Help. Tool Box The Tool box contains the tools you use to place various controls on your forms. It displays all the standard Visual Basic controls plus any custom controls and objects you have added to your project with the Custom Controls dialog box. There are 47 basic tools in the Toolbox Properties Window The Properties window contains the list of design-time properties for a selected form, control, class, module, or menu. These properties can be changed at design time, and the Properties window shows each property's current setting. Consists of: Object Box - identifies the currently selected form or the currently selected control on the form Properties List - two-column list shows all the properties that can be changed at design time, as well as their current settings. Data Types Boolean - true or false Byte - 8 bit unsigned integer Char - 16 bit Unicode character Date - date and time of day Decimal - decimal number Double - 64 bit floating-point number Short - 16 bit signed integer Integer- 32 bit signed integer Long - 64 bit signed integer SByte - 8 bit signed integer Single - 32 bit floating-point number Declaring Variables Dim <varName> as <data type> Dim - short for Dimension - It's a type of variable For Loop For index=start to end[Step step] [statements] [Exit For] [statements] Next[index]

Sample Code Sub Main() Dim intLoopIndex As Integer For intLoopIndex = 0 To 3 System.Console.WriteLine("Hello Basic") Next intLoopIndex End Sub While Loop While condition [statements] End While Sample Code Dim num As Integer While num > 4 System.Console.WriteLine(Hello!") End While Do Loop Do[{while | Until} condition] [statements] [Exit Do] [statements] Loop Sample Code Dim index As Integer = 0 Do While index <= 10 System.Console.WriteLine(Hello!") index += 1 Loop If..Else If condition Then [statements] [ElseIf condition-n Then [elseifstatements] ] [Else [elsestatements]] End If Sample Code
If intInput = 1 Then System.Console.WriteLine("Thank you.") ElseIf intInput = 2 Then

Else End If

System.Console.WriteLine("That's fine.") System.Console.WriteLine("Not a number I know.")

What is an Array? (sample code) Dim MyNumber As Integer MyNumber = 5

or this

Dim MyText As String MyText = "A String is really just text"

or even this

Dim MyNumber As Integer = 5 Holding more than one number at a time: Dim MyNumbers(4) As Integer MyNumbers(0) = 1 MyNumbers(1) = 2 MyNumbers(2) = 3 MyNumbers(3) = 4 MyNumbers(4) = 5 How do arrays work? Start a new VB project. Add a Button to your Form. Set the Text property of the Button to "Integer Array" Put the following code behind your button: Dim MyNumbers(4) As Integer MyNumbers(0) = 1 MyNumbers(1) = 2 MyNumbers(2) = 3 MyNumbers(3) = 4 MyNumbers(4) = 5 MsgBox("First Number is: " & MyNumbers(0)) MsgBox("Second Number is: " & MyNumbers(1)) MsgBox("Third Number is: " & MyNumbers(2)) MsgBox("Fourth Number is: " & MyNumbers(3)) MsgBox("Fifth Number is: " & MyNumbers(4)) In the code, we first set up an Integer array with 5 items in it. Dim MyNumbers(4) As Integer We then assigned values to each position in the array. MyNumbers(0) = 1

To get at the values in the array, and display them in messages boxes, we just used the array name, followed by the position in the array. MsgBox("First Number is: " & MyNumbers(0)) What if the array is already out of bounds? Solution: Dim MyNumbers(4) As Integer Dim i As Integer MyNumbers(0) = 10 MyNumbers(1) = 20 MyNumbers(2) = 30 MyNumbers(3) = 40 MyNumbers(4) = 50 For i = 0 To 4 ListBox1.Items.Add(MyNumbers(i)) Next i Arrays and Strings of text Put another Button on your Form Set the Text property to "String Array" Put the following code behind your Button Dim MyText(4) As String Dim i As Integer MyText(0) = "This" MyText(1) = "is" MyText(2) = "a" MyText(3) = "String" MyText(4) = "Array" For i = 0 To 4 ListBox1.Items.Add(MyText(i)) Next i

What if

we had an array with a hundred items in it? First, add another Button to your form. Set the Text Property to "Times Table Array" Add a Textbox to your Form Set the Text Property to a blank string (in other words, delete Textbox1 from the Text property) Add a Label near the Textbox Set the Text property of the Label to "Which Times Table do you want?" Now double click your new button to get at the code window. Add the following code: Code:

Machine Exercises: #1 Create a simple calculator which allows the user to input single digit numbers and add them up.

Dim numbers(10) As Integer Dim times As Integer Dim StoreAnswer As Integer Dim i As Integer ListBox1.Items.Clear() times = Val(TextBox1.Text) For i = 1 To 10 StoreAnswer = i * times numbers(i) = StoreAnswer ListBox1.Items.Add(times & " times " & i & " = " & numbers(i)) Next i

When a number (button) is clicked, its value will be transferred to the display area, which is a Textbox. Once a number is transferred to the Textbox, we can click on the Plus button and then click on another number. To get the answer, click on the equals sign. Finally, to clear the display and memory a Clear button is to be used.
10 buttons for the 10 digits (0-9) o When one of them is clicked, its value will be displayed in the textbox + button o Displays the + sign = button o Displays the final answer Clear button o Clears the display (textbox) a textbox

How can we set up a non-fixed size array? First set up an array with empty brackets Dim numbers( ) As Integer Next, pass the values from the Text Boxes to some variables times = Val(Textbox1.Text) startAt = Val(Textbox2.Text) endAt = Val(Textbox3.Text) We can then use these values to reset the array. How do we reset an array? ReDim ReDim numbers(endAt) Our original array did not have its size set - Dim numbers( ) As Integer. So we've got the end number from the Textbox. When we reset an array, we can use this new value. Since our user entered the value 12 for the end number, our array is now really this: Dim numbers(12) As Integer We can use the same variables for our For Loop. Then we can go round and round the loop assigning values to our array.

#2 Add a menu bar with About and Exit menus.

Assigning values to an array You can assign values straight from a Textbox into the position of your array. Like this: MyNumbers(0) = Val(Textbox1.Text) MyNumbers(1) = Val(Textbox2.Text) With that code, whatever you typed into the Textboxes on your Form would be stored into the positions of your array. The same would be true of a String Array: MyNumbers(0) = Textbox1.Text MyNumbers(1) = Textbox2.Text

When the About menu is clicked, a message box appears which contains details about the VB project. When the Exit menu is clicked, a thank you message is displayed and then the program terminates.

You might also like