Showing posts with label visual basic. Show all posts
Showing posts with label visual basic. Show all posts

Wednesday, May 11, 2011

VB Practice 7 - Methods II - Yahtzee


Public Class Form1

Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
Dim WithEvents butRoll As New Button
Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox

Dim rnd As New Random


Private Sub addDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
lbl.Text = 0
lbl.Location = New Point(x, y)
lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
lbl.Height = 40
lbl.Width = 40

Me.Controls.Add(lbl)

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

addDice(lblDice1, 10, 20)
addDice(lblDice2, 70, 20)
addDice(lblDice3, 130, 20)
addDice(lblDice4, 190, 20)
addDice(lblDice5, 250, 20)
resultscount(lblYatzee, "Yahtzees: 0", 20, 140)
resultscount(lblFourOfAKind, "Four of a Kinds: 0", 20, 180)
resultscount(lblThreeOfAKind, "Three of a Kinds: 0", 20, 220)
AddButton(butRoll, "Roll", 100, 90)

End Sub

Private Sub resultscount(ByRef results As TextBox, ByRef Name As String, ByVal x As Integer, ByVal y As Integer)
results.Text = Name
results.Location = New Point(x, y)
results.Width = 150

Me.Controls.Add(results)

End Sub

Private Sub RollDice() Handles butRoll.Click
Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}

RollSingleDice(lblDice1)
RollSingleDice(lblDice2)
RollSingleDice(lblDice3)
RollSingleDice(lblDice4)
RollSingleDice(lblDice5)

GetStats()

lblYatzee.Text = "Yatzees: " & nYatzee
lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind

End Sub
Private Sub AddButton(ByRef results As Button, ByRef Name As String, ByVal x As Integer, ByVal y As Integer)
results.Text = Name
results.Location = New Point(x, y)
results.Width = 150

Me.Controls.Add(results)

End Sub
Private Sub RollSingleDice(ByRef lblDice As Label)
lblDice.Text = rnd.Next(1, 7)
End Sub
Private Sub GetStats()
Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
For Each lbl As Label In Me.Controls.OfType(Of Label)()
arrNumbers(lbl.Text - 1) += 1
Next

For Each i As Integer In arrNumbers
If i = 5 Then
nYatzee += 1
ElseIf i = 4 Then
nFourOfAKind += 1
ElseIf i = 3 Then
nThreeOfAKind += 1

End If
Next
End Sub
End Class

VB Practice 6 - Methods


Public Class Form1
Dim chara, word, sentVal, space As Integer
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
'nchar = GetCharCount(TextBox1.Text)
chara = TextBox1.Text.Length.ToString
word = getWordCount(TextBox1.Text)
space = getSpacesCount(TextBox1.Text)
sentVal = getSentenceCount(TextBox1.Text)
updatestats()
End Sub
Private Function getWordCount(ByVal value As String) As Integer
Dim word As String
word = System.Text.RegularExpressions.Regex.Matches(value, "\S+").Count
Return word
End Function
Private Function getSentenceCount(ByVal value As String) As Integer
Dim sentVal As String = 1
sentVal = System.Text.RegularExpressions.Regex.Matches(value, "\.").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\!").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\?").Count
Return sentVal
End Function
Private Function getSpacesCount(ByVal value As String) As Integer
For Each s As String In value
If s = " " Then
space += 1
End If
Next
Return space
End Function
Private Sub updatestats()
txtChara.Text = chara
txtword.Text = word
txtspaces.Text = space
txtSent.Text = sentVal
End Sub

End Class

Monday, April 4, 2011

Visual Basic Methods

The following is an example of how to use a sub and a function in order to reuse code.


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String = TextBox1.Text
Dim strFormatted As String = ""

strFormatted = digitsOnly(str1)
checkNumberLength(strFormatted)
TextBox1.Text = strFormatted

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFormatted As String = "("
TextBox1.Text = digitsOnly(TextBox1.Text)
checkNumberLength(TextBox1.Text)

Dim x As Integer = 1
For Each c As String In TextBox1.Text
If x = 4 Then
strFormatted += ") "
End If
If x = 7 Then
strFormatted += "-"
End If

strFormatted += c
x += 1
Next
TextBox1.Text = strFormatted
End Sub

Private Function digitsOnly(ByVal str As String) As String
Dim strFormatted As String = ""

For Each c As String In str
If Char.IsDigit(c) Then
strFormatted += c
End If
Next
Return strFormatted
End Function

Private Sub checkNumberLength(ByVal num As String)
If num.Length <> 10 Then
MsgBox("Sorry, you need 10 numbers")
End If
End Sub
End Class

Wednesday, March 30, 2011

Visual Basic Arrays Part 3 Queue and Hashtables

3. Create a form that will work as a printer queue. This form will have the following inputs:
a.Add the title and pages to a hashtable
b.Add the hashtable to a queue which is a class variable
c.Clear the title and set the pages back to one
Refresh the queue list.
d.When submitted the click event will do the following:
a.remove the next item in the queue
b.Refresh the queue list box



Public Class Form1
Dim qJobs As New Queue

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim list As New Hashtable
list.Add("Title", txtTitle.Text)
list.Add("Copies", NumericUpDown1.Value)
qJobs.Enqueue(list)
refreshjobs()
NumericUpDown1.Value = 1
txtTitle.Clear()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If qJobs.Count > 0 Then
qJobs.Dequeue()
End If
refreshjobs()
End Sub
Private Sub refreshjobs()
ListBox1.Items.Clear()
For Each h As Hashtable In qJobs
ListBox1.Items.Add(h.Item("Title") & h.Item("Copies"))

Next
End Sub

End Class

Hashtables in Visual Basic - Arrays Assignment Number 2

2. Create a form with textboxes for the following:-first name-last name-email. Add labels to describe each box.Add a button for adding the user. The add user button click event will do the following: -add the user information into a hashtable -clear the text from the text boxes. We want to add the information into the hashtable using three keys: FirstName, LastName, Email. Add First Name, Last Name, and Email buttons to the bottom of the form. Each of these buttons is going to fire off a message box that will show the information that was just added in.

 Public Class Form1  

Public Class Form1
Dim User As New Hashtable

Private Sub btnUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUser.Click
User.Clear()
User.Add("FirstName", txtFirst.Text)
User.Add("LastName", txtLast.Text)
User.Add("Email", txtEmail.Text)
txtFirst.Clear()
txtLast.Clear()
txtEmail.Clear()
End Sub

Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
MsgBox(User.Item("FirstName"))
End Sub

Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
MsgBox(User.Item("LastName"))
End Sub

Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
MsgBox(User.Item("Email"))
End Sub
End Class

Monday, March 7, 2011

Make a playlist of artists in Visual Basic using an ArrayList

Use a textbox to add Artists to a combobox then clear it out when you get all the Artists added.

 Public Class Form1  
Dim ArtistList As New ArrayList
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'take stuff out of text box, put stuff into the combobox, clear out textbox
ArtistList.Add(txtArtistTitle.Text)
ComboBox1.DataSource = Nothing
ComboBox1.DataSource = ArtistList
txtArtistTitle.Clear()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ComboBox1.Text = Nothing
ArtistList.Clear()
ComboBox1.DataSource = Nothing
End Sub
End Class




* Add a textbox, combobox, and a button to a form.
* When you type something in the textbox, it will add the text to an arraylist. The arraylist will be defined at the class level, not in the click event.
* When the button is clicked, you also want to set your arraylist to the datasource property of the combobox.
* In order to refresh the combobox each time something is added, you need to set the datasource literally to the word Nothing and then set your datasource again to your arraylist on the next line. This will refresh the combobox contents
* Also add a button that when clicked, it will remove all the contents from the arraylist and refresh the combobox to be empty.

Visual basic arrays, arraylists, hashtables and queues.

Here are some examples of arrays, arraylists, hashtables, and queues. (From Teeps)

  Public Class frmJuke  
Dim strDahArray(0 To 2) As String
Dim arrTestMyList As New ArrayList
Dim hashTable As New Hashtable
Dim qQueue As New Queue
Private Sub btnAddCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
strDahArray(0) = "This is cool"
strDahArray(1) = "This is cooler"
strDahArray(2) = "This is the coolest"
For x As Integer = 0 To 2
MsgBox(strDahArray(x))
Next
lstArray.DataSource = strDahArray
cboArray.DataSource = strDahArray
End Sub
Private Sub btnArrayList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArrayList.Click
arrTestMyList.Add("this is not cool")
arrTestMyList.Add("not even this is cool")
arrTestMyList.Add("this sux")
For Each Str As String In arrTestMyList
MsgBox(Str)
Next
lstArray.DataSource = arrTestMyList
cboArray.DataSource = arrTestMyList
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
arrTestMyList.Clear()
For y As Integer = 0 To (strDahArray.Length - 1)
strDahArray(y) = ""
Next
lstArray.DataSource = Nothing
cboArray.DataSource = Nothing
End Sub
Private Sub btnAddToArrayList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToArrayList.Click
arrTestMyList.Add(txtArrayList.Text)
txtArrayList.Clear()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
hashTable.Add("box1", "asdf")
hashTable.Add("box2", "jld;fkajdf;lasdf")
For Each obj As DictionaryEntry In hashTable
MsgBox(obj.Key & "=" & obj.Value)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim str As String
qQueue.Enqueue("third")
qQueue.Enqueue("second")
qQueue.Enqueue("first")
Do While qQueue.Count > 0
str = qQueue.Dequeue()
MsgBox(str)
Loop
End Sub
End Class

Friday, March 4, 2011

Arrays in VB.net


Dim arrX (0 to 2) as String
arrX is almost a box with 0-2 as designators (placeholders) for the numbers 0-2.
arrX(0)="Y"
arrX(2)="Z"

Dim arrL as New ArrayList
arrL.add("Y")
arrL.add("Z")


You cannot do a .add with arrX, you are stuck with the original number of placeholders that you designate.

Friday, February 25, 2011

Practice 4 Visual Basic Loops

1. Put a textbox, a listbox, and a button on the form. The user must enter a number and press the button. When the button is pressed, it will add that many items to the listbox on the form.


   Private Sub theButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles theButton.Click  
Dim times As Double
times = CDbl(TextBox1.Text)
Dim i, sum As Integer
sum = 0
For i = 1 To times
sum = sum + 1
'show items in listbox
ListBox1.Items.Add(sum & " items")
Next
End Sub
End Class


2. If you were to put away a certain amount of money every month, how many years would it take you to save up $10,000. Use a textbox to gather the amount that is to be put away every month. Use a label to display the number of years it would take.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pay, months, total, goal As Integer
Dim years As Decimal
pay = CInt(txtPay.Text)
goal = 10000
For total = 1 To goal
total += pay
months = months + 1
years = months / 12
Label3.Text = ("It will take " & years.ToString("f1") & " years to make the $10,000 goal.")
Next
End Sub
End Class


3. Write a program that will create a times table.

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
…..
You could use labels or buttons to hold the numbers. Generate these dynamically and add them to the Me.Controls collection.
You will have to use two loops, one inside of the other to get this to work.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, y As Integer
'Button = 20 pixels
x = 1
Button1.Visible = False
For x = 1 To 10
For y = 1 To 10
Dim newbutton As New Button
newbutton.Location = New Point(50 * x, 50 * y)
newbutton.Width = 40
newbutton.Text = x * y
Me.Controls.Add(newbutton)
Next
Next
End Sub
End Class


A loop within a loop operates in the inside loop until that loop is finished, then goes into the outer loop to get to the next level to run the inner loop again till it is finished.

Friday, February 18, 2011

VB notes 2-18-11 Loops and Do Whiles

To count the number of check boxes checked on the form. 3 Check boxes on the form.


Dim nCount as integer = 0
for each element as object in me.controls
If TypeOf element Is CheckBox Then
If CType(element, CheckBox).Checked = True Then
nCount +=1
End If
End If
Next
txtCount.Text = nCount

Dim nControls As Integer = me.Controls.Count
Dim nCurrent As Integer = 0
Dim obj As Object
Do While nCurrent < nControls

obj = Me.Controls.Item(nCurrent)
If TypeOf ojb Is CheckBox Then
If CType(obj, CheckBox).Checked Then
nCount +=1
End If
End If
nCurrent +=1
txtCount.Text = nCount
If nCount > 1 Then
Exit Do
Loop

Friday, February 4, 2011

Adding code for controls in Visual Basic

# Research and blog the following:

* add a button to a page programmatically
* add a textbox to a page programmatically
* add a label to a page programmatically
* add an item list to a page programmatically
* understand how to place these in a certain location on the form
* understand how to change the width and height
* understand how to set readonly and disabled
* understand how to set the text value


 Public Class Form1  
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' create button
Dim btn As New Button
' set some button properties
btn.Text = "go"
btn.Top = 45
btn.Left = 190
'add button to form
Me.Controls.Add(btn)
'create textbox
Dim dynamicText As TextBox = Nothing
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "TimeTextBox"
dynamicText.Location = New System.Drawing.Point(8, 8)
dynamicText.Size = New System.Drawing.Size(232, 20)
dynamicText.TabIndex = 0
Me.Controls.Add(dynamicText)
'create label
Dim lbl As New Label
lbl.SetBounds(10, 50, 100, 25)
lbl.Text = "Hello World!"
Me.Controls.Add(lbl)
'create listbox
Dim lstEmails As ListBox
lstEmails = New System.Windows.Forms.ListBox()
'
'lstEmails
'
lstEmails.IntegralHeight = False
lstEmails.Location = New Point(16, 75)
lstEmails.Name = "lstEmails"
lstEmails.Size = New System.Drawing.Size(264, 224)
lstEmails.TabIndex = 0
Me.Controls.Add(lstEmails)
'
End Sub
End Class

Wednesday, February 2, 2011

Word Problems 2 Spacely's Sprockets, Heat problem, Socks

1.) Mr. Spacely has been rethinking the company's shipping schemes. As an employee for Spacely's Sprockets, Spacely has asked you to write a quick program that will be used to determine shipping costs.
A total purchase of items totaling under $50, we charge $5 shipping.
A total purchase of items totaling $50 and over, there is no shipping charge.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim purchase, shipping, total As Double
purchase = CDbl(txtpurchase.text)
shipping = 5
'Calculate total
If purchase >= 50 Then
total = FormatCurrency(purchase)
ElseIf purchase < 50 Then
total = purchase + shipping
End If
'Display total
MessageBox.Show("Your total is: " & FormatCurrency(total), "Please Pay")
End Sub
End Class


2.) You are looking for something to control your heat in your apartment and you discover there is NOT an app for that. It's about time that someone created one. You decide that you are the one to do it. Here's what you want to do.
-You want to turn the heat on when the temp has dropped below 72
-You also want to turn the AC on when the temp gets above 76
Your app should display in a message box if the heat is on, the AC is on, or if the system is idle.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim temp As Double
temp = CDbl(txtpurchase.Text)
'Calculate total
If temp <= 72 Then
MessageBox.Show("The heat is on.", "Heat")
ElseIf temp > 72 And temp < 76 Then
MessageBox.Show("The system is idle", "Idle System")
ElseIf temp >= 76 Then
MessageBox.Show("The A/C is on.", "A/C")
End If
End Sub
End Class


3.) You are working on a clothing website where people can buy kids socks. It's really hard for the customers to know what size they should buy for what age. It would be a good idea for the customer to have a tool to input their child's age and have the website suggest a size for them. Write a tool where you can set the age as a variable and have it suggest on of the sizes below:
a.0-2 yrs -XS
b.3-4 yrs -S
c.5-8 yrs -M
d.9-12 yrs -L
e.13+ yrs -XL

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim age As Double
age = CDbl(txtpurchase.Text)
'Calculate total
If age <= 2 Then
MessageBox.Show("Your child's size is XS.", "XS")
ElseIf age >= 3 And age <= 4 Then
MessageBox.Show("Your child's size is Small", "Small")
ElseIf age >= 5 And age <= 8 Then
MessageBox.Show("Your child's size is Medium", "Medium")
ElseIf age >= 9 And age <= 12 Then
MessageBox.Show("Your child's size is Large", "Large")
ElseIf age >= 13 Then
MessageBox.Show("Your child's size is X-Large.", "X-Large")
End If
End Sub
End Class

Tuesday, February 1, 2011

Case Statement in Visual Basic

A case statement in Visual Basic:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim num As Double
Select Case num
Case 5
txtResults.Text = "case 1"
Case 5 To 7
txtResults.Text = "case 2"
Case 7 To 12
txtResults.Text = "case 3"
End Select
End Sub
End Class

If Statements in Visual Basic

An if statement that makes one decision:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim num1, num2, largerNum As Double
'Convert text box to double
num1 = CDbl(txtFirstNum.Text)
num2 = CDbl(txtSecondNum.Text)
'Find results
If num1 > num2 Then
largerNum = num1
Else
largerNum = num2
End If
'Display results
txtResults.Text = "The larger number is " & largerNum & "."
End Sub
End Class


An if statement where it's either or:


 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim message As String
message = "On a bad day, I have mood swings - but on a good day, I have the whole mood playground – Charles Rosenblum"
If txtmessage.Text.ToUpper = "Y" Then
MessageBox.Show(message, "Quote")
End If
MessageBox.Show("At first, I only laughed at myself. Then I noticed that life itself is amusing. I've been in a generally good mood ever since. – Marilyn vos Savant", "Bad Mood Quote")
End Sub
End Class


An if statement where many decisions are made:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim gpa As Double = CDbl(txtGpa.Text)
Dim honors As String
If gpa >= 3.9 Then
honors = " summa cum laude."
ElseIf gpa >= 3.6 Then
honors = " magna cum laude."
ElseIf gpa >= 3.3 Then
honors = " cum laude."
ElseIf gpa >= 2 Then
honors = "."
End If
txtResults.Text = "You graduated" & honors
End Sub
End Class

Monday, January 31, 2011

Word Problem 6 - Quiz scores

a.) Given the table of quiz marks out of 25 in each subject calculate How many marks did Brian totally obtain in Mathematics and Science?
Write a program in Visual Basic to calculate the marks Brian obtained in Mathematics and Science.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
'Declare variables
Dim M, S, marks As Integer
'Set values
M = 7 'Marks in Mathmetics
S = 22 'Marks in Science
marks = M + S
'Display results
MessageBox.Show("Brian has " & marks & " marks in Mathematics and Science.")
End Sub
End Class


b.) How many more marks does Andrew need for a perfect score in Mathematics?

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
'Declare variables
Dim M, S, marks As Integer
'Set values
M = 18 'Brian's actual marks
S = 25 'total possible
marks = S - M
'Display results
MessageBox.Show("Brian needs " & marks & " marks for a perfect score in Mathematics.")
End Sub
End Class


c.) What is Andrew's percentage for all of the quizzes together?

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
'Declare variables
Dim M, S, En, G As Integer
Dim marks As Decimal
'Set values
M = 18 'marks in Mathematics
S = 7 'marks in Science
En = 12 'marks in English
G = 15 'marks in Geography
marks = (S + M + En + G) / 100
'Display results
MessageBox.Show("Brian has " & FormatPercent(marks) & " marks for all the quizzes together.")
End Sub
End Class

Word Problem 5 - Inglebert's Apples and Oranges with variable

Inglebert has 15 apples and 3 times as many oranges. How may pieces of fruit does she have?
Create a program in Visual Basic to calculate the number of pieces of fruit that Inglebert has. Enter the initial value for apples in a text box.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
'Declare variables
Dim A, O, pieces As Integer
'Set values
A = txtInitial.Text
O = A * 3
pieces = A + O
'Display results
MessageBox.Show("Inglebert has " & pieces & " pieces of fruit.")
End Sub
End Class

Word Problem 5 - Inglebert's Apples and Oranges

Inglebert has 15 apples and 3 times as many oranges. How many pieces of fruit does she have?
Create a program in Visual Basic to calculate the number of pieces of fruit Inglebert has with the given numbers.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
'Declare variables
Dim A, O, pieces As Integer
'Set values
A = 15
O = A * 3
pieces = A + O
'Display results
MessageBox.Show("Inglebert has " & pieces & " pieces of fruit.")
End Sub
End Class

Word Problem 4 - Horses in the field part 2

There are 33 horses in a field. 15 horses go into the barn. Then 7 of them come back out. How many horses are standing in the field?
Write a program in Visual Basic to find out how many horses are standing in the field.
Gather the initial value in a text box.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  

'Declare variables
Dim I, O, B, horses As Integer
'Set values
O = 15
I = txtInitial.Text
B = 7
horses = I - O + B
'Display results
MessageBox.Show("There are " & horses & " horses in the field.")
End Sub
End Class

Word Problem 4 - Horses in the field

There are 33 horses in a field. 15 horses go into the barn. Then 7 of them come back out. How many horses are standing in the field?
Write a program in Visual Basic to find out how many horses are standing in the field.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim I, O, B, horses As Integer
'Set values
O = 33
I = 15
B = 7
horses = O - I + B
'Display results
MessageBox.Show("There are " & horses & " horses in the field.")
End Sub
End Class

Friday, January 28, 2011

Word Problem 3 - Apples and Oranges

Ole has 15 apples and 12 oranges. How many pieces of fruit does he have?

Write a program in Visual Basic to display the number of pieces of fruit Ole has.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim A, O, fruit As Integer
'Set values
A = 15
O = 12
fruit = A + O
'Display results
MessageBox.Show("Ole has " & fruit & " pieces of fruit.")
End Sub
End Class