Fundamentals of Programming in VB: Examples List Box Example The following example shows the VB codes, its design view, and run-time results of using a listbox, where results of math calculations are added t othe list box line by line. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click lstResults.Items.Clear() lstResults.Items.Add(3 + 2) lstResults.Items.Add(3 - 2) lstResults.Items.Add(3 * 2) lstResults.Items.Add(3 / 2) lstResults.Items.Add(3 ^ 2) lstResults.Items.Add(2 * (3 + 4)) End Sub Declaring and Assigning Numerical Variables Using a List Box The example below shows how variables are declared. The screen shows the results before and after clicking the button. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim a As Double Dim b As Double = 3 lstResults.Items.Clear() lstResults.Items.Add(a) lstResults.Items.Add(b) a = 5 lstResults.Items.Add(a * (2 + b)) End Sub 1 Fundamentals of Programming in VB: Examples Built-in Math Function and List Box The following codes show the use of some Math functions. It also shows how the use of “ With…..End With” block in a List Box can reduce the amount of repeated typing. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim n As Double Dim root As Double n = 6.76 root = Math.Sqrt(n) With lstResults.Items .Clear() .Add(root) .Add(Int(n)) .Add(Math.Round(n, 1)) End With End Sub Use of String Variables in a List Box The screens show the results before and after clicking the button. Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim today As String today = "Monday" With lstOutput.Items .Clear() .Add("today") .Add(today) End With End Sub 2 Fundamentals of Programming in VB: Examples Concatenation of Strings as variables The following codes show how two strings declared as variables can be joined together. The screen displays the result upon clicking on the button. Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim quote1, quote2, quote As String quote1 = "The ballgame isn't over, " quote2 = "until it's over." quote = quote1 & quote2 txtOutput.Text = quote & " Yogi Berra" End Sub Reading from and Writing to Text Boxes The following code reads data as text from two text boxes and converts into a number and then adds and places the result in another text box. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim num1, num2, sum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) sum = num1 + num2 txtSum.Text = CStr(sum) End Sub 3 Fundamentals of Programming in VB: Examples Use of IndexOf, Substring, and Length Properties of a String The following program parses a name consisting of two parts, finds the position of an empty string, determines the parts of the name, and then finds the number of characters in the last part of the name. Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click Dim fullName, firstName, lastName As String Dim n As Integer fullName = txtName.Text n = fullName.IndexOf(" ") firstName = fullName.Substring(0, n) lastName = fullName.Substring(n + 1) With lstResults.Items .Clear() .Add("First name: " & firstName) .Add("Your last name has " & lastName.Length & " letters.") End With End Sub Using Properties and Methods of a Text Box The following codes show how to enable and disable a text box, change color, and bring focus on a text box. Private Sub btnDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisable.Click txtBox.Enabled = False txtBox.BackColor = Color.BurlyWood End Sub Private Sub btnEnable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnable.Click txtBox.Enabled = True txtBox.Focus() End Sub 4 Fundamentals of Programming in VB: Examples Using the Editor to Automatically Locate a Property of a Control such as Text Box and also the Correct Assignments 5 Fundamentals of Programming in VB: Examples Using Properties such as Background Color of a Text Box The following codes show how properties of text box such as background color of a text box can be changed. As shown, all code sections are fired when Enter event of a text box is executed. Private Sub txtGreen_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGreen.Enter txtGreen.BackColor = Color.Green txtYellow.BackColor = Color.DarkGray txtRed.BackColor = Color.DarkGray End Sub Private Sub txtYellow_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtYellow.Enter txtGreen.BackColor = Color.DarkGray txtYellow.BackColor = Color.Yellow txtRed.BackColor = Color.DarkGray End Sub Private Sub txtRed_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRed.Enter txtGreen.BackColor = Color.DarkGray txtYellow.BackColor = Color.DarkGray txtRed.BackColor = Color.Red End Sub Problem: A motorist starts his car with a full tank of gas (14.1 gallons) and with a odometer reading of 23,340 miles. He stops to fill the tank again at an odometer reading of 23,695 miles. How many miles per gallon did he get in his car? Write your answer in a list box. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim miles, gallonsUsed, milesPerGallon As Double miles = 23695 - 23340 gallonsUsed = 14.1 milesPerGallon = Math.Round(miles/gallonsUsed) lstOutput.Items.Add(milesPerGallon) End Sub 6 Fundamentals of Programming in VB: Examples Problem: It is found in a survey that Americans use an average of 1600 gallons of water per person per day, including industrial use. How many gallons of water are used each year in the United States? Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim waterPerPersonPerDay, people, days, waterUsed As Double waterPerPersonPerDay = 1600 people = 300000000 days = 365 waterUsed = waterPerPersonPerDay * people * days lstOutput.Items.Add(waterUsed) End Sub Problem: Write a program to request the name of baseball team, the number of games won, and the number of games lost as input, and then display the name of the team and the percentage of games won. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim numberOfGames As Integer Dim percent, percentage As Double numberOfGames = CInt(txtWon.Text) + CInt(txtLost.Text) percent = CDbl(txtWon.Text) / numberOfGames percentage = 100 * Math.Round(percent, 5) txtPercent.Text = txtTeam.Text & " won " & percentage & " percent of its games." End Sub Note the highlighted code for rounding the percent upto three decimals only. 7