Tuesday, November 30, 2010

Tuesday 11/30/2010


Dim numbers(,) As Integer = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
Dim numberOfRows As Integer = numbers.GetLength(0)
Dim numberOfColumns As Integer = numbers.GetLength(1)
Dim sumOfFirstRow As Integer = numbers(0, 0) + numbers(0, 1)

Dim numbersString As String = " "
For i As Integer = 0 To numbers.GetLength(0) - 1
For j As Integer = 0 To numbers.GetLength(1) - 1
numbersString &= numbers(i, j) & " "
Next
numbersString &= vbCrLf
Next
MessageBox.Show(numbersString, "Numbers Test")


Here is a tutorial dealing with ticks


Dim ticksBefore As Long
Dim ticksAfter As Long
Dim tickSeconds As Double

Do
ticksBefore = Now.Ticks
Console.WriteLine("Press enter to see elapsed time")
Console.ReadLine()
ticksAfter = Now.Ticks
tickSeconds = (ticksAfter - ticksBefore) / 10000000.0
Console.WriteLine("Elapsed time: " & tickSeconds.ToString)
Console.WriteLine()
Loop


Here is a tutorial I wrote on a rectangular array containing strings


Dim recArray(4, 4) As String
Dim wholeString As String = ""

recArray(0, 0) = "Mary"
recArray(0, 1) = "Thompson"
recArray(1, 0) = "John"
recArray(1, 1) = "Martin"
recArray(2, 0) = "Kirstin"
recArray(2, 1) = "Newbury"

For y = 0 To recArray.GetUpperBound(0)
For x = 0 To recArray.GetUpperBound(1)
wholeString = recArray(y, 0) & " " & recArray(y, 1)
Next
Console.WriteLine(wholeString)
Next

Console.ReadLine()

Sunday, November 28, 2010

Sunday 11/28/10

Continuing with the Jesse Liberty Console Tutorials


Option Strict On
Imports System

Public Class Time
'Private variables
Private AYear As Integer
Private AMonth As Integer
Private ADate As Integer
Private AHour As Integer
Private AMinute As Integer
Private ASecond As Integer

'public methods
Public Sub displayCurrentTime()
System.Console.WriteLine("{0} / {1} / {2} " & vbTab & "{3} : {4} : {5}", AMonth, ADate, AYear, AHour, AMinute, ASecond)
End Sub ' display current time

Public Sub New( _
ByVal theYear As Integer, _
ByVal theMonth As Integer, _
ByVal theDate As Integer, _
ByVal theHour As Integer, _
ByVal theMinute As Integer, _
ByVal theSecond As Integer)

AYear = theYear
AMonth = theMonth
ADate = theDate
AHour = theHour
AMinute = theMinute
ASecond = theSecond

End Sub
End Class 'Time

Module Module1

Sub Main()
Dim timeObject As New Time(2005, 3, 25, 9, 35, 20)
timeObject.displayCurrentTime()
Console.ReadLine()
End Sub
End Module


Here is a tutorial I wrote for getting the current time in seconds


Sub Main()
Dim second1 As Integer
Dim second2 As Integer

Dim keepGoing As Boolean = True


While keepGoing = True
Call printSeconds(second1, second2)
keepGoing = stopOrNot()
End While

End Sub

Sub printSeconds(ByRef second1, ByRef second2)

Call getSeconds(second1, second2)
Console.WriteLine("second1 is: " & second1 & " and second2 is: " & second2)
End Sub

Function stopOrNot()
Dim inputString As String = ""
inputString = Console.ReadLine
If inputString.ToUpper = "STOP" Then
Return False
Else
Return True
End If
End Function

Sub getSeconds(ByRef second1, ByRef second2)
Dim d1 As DateTime = DateTime.Now
Dim d2 As String = d1.ToString
second1 = (Val(d2.Chars(16)))
second2 = (Val(d2.Chars(17)))
End Sub


Here is another variant of the tutorial by Jesse Liberty


Imports System
Public Class Time
'private variables
Private aYear As Integer
Private aMonth As Integer
Private aDate As Integer
Private anHour As Integer
Private aMinute As Integer
Private aSecond As Integer = 30

'Public methods
Public Sub DisplayCurrentTime()
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", _
aMonth, aDate, aYear, anHour, aMinute, aSecond)

End Sub

Public Sub New(ByVal theYear As Integer, _
ByVal theMonth As Integer, _
ByVal theDate As Integer, _
ByVal theHour As Integer, _
ByVal theMinute As Integer, _
ByVal theSecond As Integer)

aYear = theYear
aMonth = theMonth
aDate = theDate
anHour = theHour
aMinute = theMinute
aSecond = theSecond
End Sub

Public Sub New(ByVal existingObject As Time)
aYear = existingObject.aYear
aMonth = existingObject.aMonth
aDate = existingObject.aDate
anHour = existingObject.anHour
aMinute = existingObject.aMinute
aSecond = existingObject.aSecond
End Sub
End Class 'Time





Module Module1
Sub Main()
Dim timeObject As New Time(2008, 3, 25, 10, 25, 11)
Dim t2 As New Time(timeObject)
timeObject.DisplayCurrentTime()
t2.DisplayCurrentTime()
Console.ReadLine()
End Sub



End Module


Here is another tutorial



Dim fileName As String = "C:\Users\Alfred\My Documents\VBPractice\newStocks.txt"

Console.WriteLine("Created: " & _
System.IO.File.GetCreationTime(fileName))
Console.WriteLine("Last Modified: " & _
System.IO.File.GetLastWriteTime(fileName))
Console.WriteLine("Last accessed: " & _
System.IO.File.GetLastAccessTime(fileName))
Console.ReadLine()

Friday, November 26, 2010

Friday, 11/26/10


Public Sub main()
Dim testObject As New Tester()
testObject.run()
Console.ReadLine()
End Sub

Public Class dog
Public weight As Integer
Public color As String
End Class

Public Class Tester


Public Sub run()
'create an integer
Dim firstInt As Integer = 5

'create a second integer
Dim secondInt As Integer = firstInt

'display the two integers
Console.WriteLine("fistInt: {0}, secondInt: {1}", firstInt, secondInt)

'modify the second integer
secondInt = 8

'display the two integers
Console.WriteLine("firstInt: {0}, secondInt {1}", firstInt, secondInt)

'create a dog
Dim milo As New dog()

'assign a value to weight
milo.weight = 5
milo.color = "brown"

'create a second reference to the dog

Dim fido As dog = milo

'display their values
Console.WriteLine("Milo:{0}, {1}; Fido: {2}, {3}", milo.weight, milo.color, fido.weight, fido.color)
'assign a new weight to the second reference
fido.weight = 9
fido.color = "white"

'display the two values
Console.WriteLine("Milo:{0}, {1}; Fido:{2}, {3}", milo.weight, milo.color, fido.weight, fido.color)
End Sub


Here is another tutorial by Jesse Liberty


Option Strict On
Imports System
Public Class TestClass

Sub SomeMethod(ByVal firstParam As Integer, ByVal secondParam As Single)
Console.WriteLine("Here are the parameters recieved: {0}, {1}", _
firstParam, secondParam)
End Sub
End Class

Module Module1
Sub main()
Dim howManyPeople As Integer = 5
Dim pi As Single = 3.14

Dim tc As New TestClass()
tc.SomeMethod(howManyPeople, pi)
Console.ReadLine()
End Sub
End Module



I get the impression that when you code using vb.net you need to have an understanding of Common Intermediate Language going on in the back of your mind.

Here is the final tutorial of the day


Module Module1

Sub main()
Dim array As Integer() = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35}

Dim total As Integer = 0, i As Integer = 0

For i = 0 To array.GetUpperBound(0)
total += array(i)
Next

Console.WriteLine("Total of array elements: " & total)
Console.ReadLine()

End Sub

End Module

Wednesday, November 24, 2010

Wednesday, 11, 24, 2010

From the Jesse Liberty book - not the first one I got, which was great but moved a little fast, but the second one I got as a iPhone app (4.95 - cheap!)



Sub main()
Dim temp As Integer

temp = Console.ReadLine()

If temp > 32 Then
Console.WriteLine("Temperature above freezing. Watch out for water on the road.")
Else ''This else is very important, you must add it.
If temp <= 32 Then
Console.WriteLine("Warning! Ice on Road!")
If temp = 32 Then
Console.WriteLine("Temp exactly freezing, beware of water.")
Else
Console.WriteLine("Watch out for black ice!" & " Temp: {0} ", temp)

End If
End If
End If
End Sub


Here is another tutorial from that book, I modified it a little with a do - loop while


Dim keepGoing As Boolean = True
Dim targetInteger As Integer

Do
targetInteger = Console.ReadLine()

Select Case targetInteger
Case 5
Console.WriteLine("5")
keepGoing = False
Case 10
Console.WriteLine("10")
keepGoing = False
Case 15
Console.WriteLine("15")
keepGoing = False
Case 20
Console.WriteLine("20")
keepGoing = False
Case 25
Console.WriteLine("25")
keepGoing = False
Case Else
Console.WriteLine("Value not found. Please try again.")
End Select
Loop While keepGoing = True

Console.WriteLine("Information accepted")


Here is a final case select tutorial


Dim target As String = "Milo"

Select Case target
Case "Alpha" To "Lambda"
Console.WriteLine("Alpha to Lamba Executed")
Case "Lamba" To "Zeta"
Console.WriteLine("Lamba to Zeta Executed")
Case Else
Console.WriteLine("Else executed")
End Select

Here is an online tutorial on arrays from http://www.java2s.com/Tutorial/VB/0160__Collections/0020__Array.htm


Sub main()
Dim sourceArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8}

Dim result As New System.Text.StringBuilder
Dim counter As Integer

result.Append("{")
For counter = 0 To sourceArray.Length - 1
result.Append(sourceArray(counter).ToString())
If (counter < (sourceArray.Length - 1)) Then result.Append(", ")

Next counter
result.Append("}")
Console.WriteLine(result.ToString())

Console.ReadLine()




End Sub


Here is another tutorial



Dim arySalaries(15) As Integer
Dim aryNames(15) As String
Dim intCounter As Integer

aryNames(0) = "JZ"
arySalaries(0) = 36000
aryNames(1) = "BT"
arySalaries(1) = 63000
aryNames(2) = "LS"
arySalaries(2) = 45000

' set other array names here

aryNames(15) = "PX"
arySalaries(15) = 18000

'show the elements of the array
For intCounter = 0 To 15
Console.WriteLine("Array element: " & intCounter & vbCrLf & "Salary: " & arySalaries(intCounter) & vbCrLf)
Next
Console.ReadLine()


Here is another Jesse Liberty console tutorial



Dim counterVariable As Integer = 0

Do While counterVariable < 10
Console.WriteLine("CounterVariable: {0}", counterVariable)
counterVariable = counterVariable + 1
Loop
Console.ReadLine()


Here I wrote a quick count-controlled tutorial




Dim countVar As Integer = 0
Dim arrayHolder(15) As Integer

Do Until countVar = arrayHolder.Length - 1
arrayHolder(countVar) = 3 * (countVar + 1)
Console.WriteLine(arrayHolder(countVar))
countVar += 1
Loop
Console.ReadLine()


Here is a Jess Liberty tutorial using exit Do




'using exit Do

Dim counterVariable As Integer = 0
Do
Console.WriteLine("counterVariable: {0}", counterVariable)
counterVariable = counterVariable + 1
If counterVariable > 9 Then
Exit Do
End If
Loop
Console.ReadLine()


Here is a quick tutorial I wrote on using inner and outer loops


'using a for loop

Dim inner As Integer
Dim outer As Integer

For inner = 1 To 15
For outer = 1 To 15
Console.WriteLine("{0} * {1} = {2}", inner, outer, inner * outer)
Next
Next

Console.ReadLine()


Here is a Jesse Liberty tutorial on mods


'modulus operator to return remainders
Dim counter As Integer
'count from 1 to 100
For counter = 1 To 100
'display the value
Console.Write("{0} ", counter)
If counter Mod 10 = 0 Then
Console.WriteLine(vbTab & counter)
End If
Next counter
Console.ReadLine()

Tuesday, November 23, 2010

Tuesday November 23 2010

Here is a quick tutorial by Jesse Liberty on enumeration


Enum Temperatures
WickedCold = 0
FreezingPoint = 32
LightJacketWeather = 60
SwimmingWeather = 75
BoilingPoint = 212
End Enum

Sub Main()
System.Console.WriteLine("Freezing point of water: {0}", Temperatures.FreezingPoint)
System.Console.WriteLine("BoilingPoint of water: {0}", Temperatures.BoilingPoint)

End Sub

Monday, November 22, 2010

Monday 11/22/2010


Sub Main()
OneValue("Mr. Gates")
Console.WriteLine()
TwoValues(50, "Mr. Gates")
Console.WriteLine()
ThreeValues("Mr. Gates ", 50, 2500000.0)
Console.ReadLine()
End Sub

Sub OneValue(ByVal Name As String)
Console.WriteLine("Hello " & Name)
End Sub

Sub TwoValues(ByVal Age As Integer, ByVal Name As String)
Console.WriteLine("Age: " & Age)
Console.WriteLine("Name: " & Name)
End Sub

Sub ThreeValues(ByVal Name As String, ByVal Age As Integer, ByVal Salary As Double)
Console.WriteLine("Name: " & Name)
Console.WriteLine("Age: " & Age)
Console.WriteLine("Salary: " & Salary)
End Sub


Here is a tutorial that will return a string value from a function


Sub Main()
Dim iFirstDay As Integer
Dim iLastDay As Integer
Dim iCurrentDay As Integer

iFirstDay = Console.ReadLine()
iLastDay = Console.ReadLine()

For iCurrentDay = iFirstDay To iLastDay
System.Console.WriteLine(WeekdayName(iCurrentDay))
Next iCurrentDay
Console.ReadLine()

End Sub

Function WeekdayName(ByVal iDayNumber As Integer) As String
Dim sWeekdayName As String

Select Case iDayNumber
Case 1
sWeekdayName = "Sunday"
Case 2
sWeekdayName = "Monday"
Case 3
sWeekdayName = "Tuesday"
Case 4
sWeekdayName = "Wednesday"
Case 5
sWeekdayName = "Thursday"
Case 6
sWeekdayName = "Friday"
Case 7
sWeekdayName = "Saturday"
Case Else
sWeekdayName = ("Invalid Day Number")
End Select
Return sWeekdayName

End Function


Here is a tutorial looking at arrays


Sub Main()
Dim arr As Array
Dim int() As Integer = {12, 16, 20, 24, 28, 32, 36}
Dim int2() As Integer = {12, 16, 20, 24, 28, 32, 36}
arr = CType(int, Array)

For i = 0 To int.GetUpperBound(0)
Console.WriteLine("Int2(" & i & "): " & int2(i))
Next



Dim byFours() As Integer = {12, 24, 36, 38, 50}
Dim names() As String = {"k", "s", "s", "d", "n"}
Dim miscData() As Object = {"This", 12D, 16UI, "a"c}
Dim objArray() As Object

miscData = names

Dim myArray1a() As Integer
Dim myArray1b As Integer()
Dim myArray2(10) As Integer

Dim Employees1(,) As Object
Dim employees2(200, 2) As Object

Dim jagged1(9)() As String
Dim jagged2()() As String = New String(9)() {}

Dim myArray1arr As Array
Dim myArray2a As Array = Array.CreateInstance(GetType(Integer), 10)

Dim members() As Integer = {3, 10}
Dim myArray3a As Array = Array.CreateInstance(GetType(Integer), members)

Console.ReadLine()

End Sub



Here is another array


Dim i As Integer

Dim array As Integer() 'declare array variable
array = New Integer(9) {} 'allocate memory for array

Console.WriteLine("Subscript " & vbTab & "Value ")

For i = 0 To array.GetUpperBound(0)
Console.WriteLine(i & vbTab & array(i))
Next

Console.WriteLine("The array contains " & _
array.Length & " elements")


Console.ReadLine()


Here is a tutorial by Michael Halverston on managing windows forms and controls at run time



Dim form2 As New Form

form2.Text = "My New Form"
form2.FormBorderStyle = FormBorderStyle.FixedDialog

'specift that the position of the form will be set manually
form2.StartPosition = FormStartPosition.Manual

'declare a rectangle structure to hold the 4 dimensions
'upper left corner of the form (200, 100)
'width and height of the form (300, 250)

Dim form2Rect As New Rectangle(200, 100, 300, 250)

'set the bounds of the form using the rectangle object
form2.DesktopBounds = form2Rect

'display the form as a modal dialog box
form2.ShowDialog()

End Sub


Here is the last tutorial for the evening on initializing arrays


Dim array1, array2 As Integer() 'declare two arrays

'initializer list specifies number of elements
'and value of each element
array1 = New Integer() {2, 7, 4, 8, 5, 14, 90, 60, 70, 42}

'allocate array2 based on length of array1
array2 = New Integer(array1.GetUpperBound(0)) {}

'set vlues in array2 by a calculation
For i = 0 To array2.GetUpperBound(0)
array2(i) = 2 + 2 * i
Next

Console.WriteLine("Subscript " & vbTab & "Array1" & vbTab & "Array2")
Console.WriteLine("")
'display values for both arrays
For i = 0 To array1.GetUpperBound(0)
Console.WriteLine(i & " " & vbTab & vbTab & array1(i) & vbTab & array2(i))

Next
Console.ReadLine()

Tuesday, November 16, 2010

Tuesday November 16

Here is short tutorial on using a function in an if statement


Sub Main()

If (getBookPrice() > 49.99) Then
Console.WriteLine("Woah, that book is expensive")
Else
Console.WriteLine("That is not too expensive")
End If
Console.ReadLine()

End Sub

Function getBookPrice() As Double
Dim price As Double
Console.WriteLine("What is the price of the book?")
price = Console.ReadLine()
Return price
End Function


Here is a tutorial that finds the squares of 1-19 using a function


Sub main()
Dim i As Integer
Console.WriteLine("Number " & vbTab & "Square " & vbCrLf)

For i = 1 To 19
Console.WriteLine(i & vbTab & square(i))
Next
Console.ReadLine()
End Sub

Function square(ByVal y As Integer) As Integer
Return y ^ 2
End Function


Here is a tutorial on finding the maximum value out of three numbers


Sub main()
Dim value1, value2, value3 As Double

value1 = Console.ReadLine()
value2 = Console.ReadLine()
value3 = Console.ReadLine()

Console.WriteLine(Maximum(value1, value2, value3))
Console.ReadLine()

End Sub

Function maximum(ByVal valueOne As Double, ByVal valueTwo As Double, ByVal valueThree As Double) As Double
Return Math.Max(Math.Max(valueOne, valueTwo), valueThree)

End Function


Here is a tutorial from Michael Halverston on creating a new form


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

'define the text property and border style of the form
form2.Text = "My New Form"
form2.FormBorderStyle = FormBorderStyle.FixedDialog

'specify that the position of the form will be set manually
form2.StartPosition = FormStartPosition.Manual

'Declare a rectangle structure to hold the form dimensions
Dim form2Rect As New Rectangle(400, 200, 300, 350)

'set the bounds of the form using the Rectangle object
form2.DesktopBounds = form2Rect

'display the form as a modal dialog box
form2.ShowDialog()

End Sub

Monday, November 15, 2010

Monday 11/15/2010


Module Module1

Sub Main()
Dim I As Integer
Dim X() As Integer = {5, 2, 7, 4, 6, 1, 3, 9, 8}

Console.WriteLine("Unsorted array:")

For I = 0 To X.GetUpperBound(0)
Console.Write("{0} ", X(I))
Next I

insertion_sort(X, X.GetUpperBound(0))

Console.WriteLine("{0} Sprrted Array: ", Chr(10))

For I = 0 To X.GetUpperBound(0)
Console.Write("{0} ", X(I))
Next I
Console.ReadLine()


End Sub

Sub insertion_sort(ByRef x() As Integer, ByVal length As Integer)
Dim key As Integer, i As Integer, j As Integer

For j = 1 To length
key = x(j)
i = j - 1

Do While i >= 0
If x(i) > key Then
x(i + 1) = x(i)
i -= 1
Else
Exit Do
End If
Loop
x(i + 1) = key
Next j
End Sub


Here is a variation on a While loop for figuring out the powers of a number

Sub main()
Dim product As Integer

product = Console.ReadLine()
Dim multiplier As Integer = product
Dim original As Integer = product



While product < 1000
Console.Write("{0} ", product)
product = product * multiplier

End While
Console.WriteLine("The smallest power of " & original & " greater than 1000 is {0}", product)
Console.ReadLine()
End Sub

Here is a while loop that draws a square, kind of


Sub main()
Dim side As Integer
Dim row As Integer
Dim column As Integer

side = 12

If side <= 20 Then
While row <= side
column = 1
While column <= side
Console.Write("X")
column += 1

End While
Console.WriteLine()

row += 1
End While
End If
Console.ReadLine()
End Sub


I wrote another quick program of my own to make a square.

Sub main()
Dim iRow As Integer
Dim iColumn As Integer
Dim iWidth As Integer


iRow = Console.ReadLine()

For i = 1 To iRow
iColumn = 1
While iColumn <= iRow
Console.Write("X")
iColumn += 1
End While
Console.WriteLine()
Next

Console.ReadLine()
End Sub


Here we have another program for matching with a position in an array.


Dim iCounter As Integer = 0
Dim arrList(11) As String
Dim iMatch As Integer = -1
Dim sMatch As String

sMatch = Console.ReadLine()
sMatch = sMatch.ToUpper()


arrList(0) = "A"
arrList(1) = "B"
arrList(2) = "C"
arrList(3) = "D"
arrList(4) = "E"
arrList(5) = "F"
arrList(6) = "G"
arrList(7) = "H"
arrList(8) = "I"
arrList(9) = "J"
arrList(10) = "K"
arrList(11) = "L"


While iCounter <= 11 And iMatch = -1
If arrList(iCounter) Like sMatch Then
iMatch = iCounter
Else
iCounter = iCounter + 1
End If
End While

If iMatch <> -1 Then
System.Console.WriteLine("Matched with array" & iMatch)

End If
Console.ReadLine()

Friday, November 12, 2010

Friday, November 12 2010

Here is a program I wrote around a quick and easy way to get the required number of steps for a flight of stairs


Public Class Form1
Dim totalRise As Double
Dim idealNumberOfSteps As Double
Dim idealDivisor As Integer


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
totalRise = CDbl(TextBox1.Text)
idealDivisor = setDivisor()

idealNumberOfSteps = totalRise / idealDivisor

MessageBox.Show(idealNumberOfSteps & " / " & Math.Round(idealNumberOfSteps, 0))


End Sub

Private Function setDivisor()
If RadioButton1.Checked = True Then
idealDivisor = 6
Else
idealDivisor = 7
End If
Return idealDivisor

End Function
End Class


Here is an expanded version of the program



Public Class Form1
Dim totalRise As Double
Dim idealNumberOfSteps As Double
Dim idealDivisor As Integer
Dim idealHeight As Double
Dim idealRun As Double


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
totalRise = CDbl(TextBox1.Text) * 12

idealDivisor = setDivisor()

idealNumberOfSteps = totalRise / idealDivisor

idealHeight = setIdealHeight(totalRise, idealNumberOfSteps)


idealRun = 26 - (idealHeight * 2)
idealRun = Math.Round(idealRun, 2)


Call showIdeal(idealNumberOfSteps)

Call showIdealSpecifics(idealHeight, idealRun)



End Sub

Private Function setDivisor()
If RadioButton1.Checked = True Then
idealDivisor = 6
Else
idealDivisor = 7
End If
Return idealDivisor

End Function

Private Function setIdealHeight(ByVal totalRise, ByVal idealNumberOfSteps)
Dim ideal As Double
ideal = totalRise / Math.Round(idealNumberOfSteps, 0)
idealHeight = Math.Round(idealHeight, 2)
Return ideal

End Function

Private Sub showIdeal(ByVal idealNumberOfSteps)
showData.Text = ""

showData.Text += "In an ideal world, you would have " & idealNumberOfSteps & " stairs." & vbCrLf
showData.Text += "If the rise of the staircase is not an issue, we would recommend having " & Math.Round(idealNumberOfSteps, 0) & " stairs" & vbCrLf


End Sub

Private Sub showIdealSpecifics(ByVal idealHeight, ByVal idealRun)
showData.Text += vbCrLf & "If you use our recommended number of stairs, the height of each stair would be " & idealHeight & " inches."
showData.Text += "The ideal run, or width of each stair, would be " & idealRun & " inches"



End Sub
End Class

Thursday, November 11, 2010

Thursday, November 11 2010

Here is a short program I wrote, one button will transfer text from your clipboard, and the other button will count how many words you have.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
Me.RichTextBox1.Paste()
RichTextBox1.Text = RichTextBox1.Text & vbCrLf



End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim counter As Integer = 0
Dim bigString As String = RichTextBox1.Text
Dim textChar As Char
Dim test As Char
Dim wordCounter = 0

For counter = 0 To bigString.Length - 1
textChar = bigString.Chars(counter)
If textChar = " " Then
wordCounter += 1

End If
Next
MessageBox.Show("There are " & wordCounter & " words.")
End Sub

Wednesday, November 10, 2010

Wednesday 11/10/10

Here is a nested for loop


Sub Main()
For outer = 3 To 6
For inner = 10 To 12
Console.WriteLine("{0}*{1}={2}", outer, inner, outer * inner)
Next inner, outer
Console.ReadLine()
End Sub


Here is another tutorial


Sub Main()
Dim j As Integer



For i As Integer = 1 To 3
j = 0
Do While j < 3
j += 1

For k As Integer = 1 To 3
Dim test1 As Boolean = k = 2
If test1 Then Exit For

Dim test2 As Boolean = i = j
If test2 Then Exit Do
Console.WriteLine(i & ", " & j & ", " & k)

Console.ReadLine()
Next k
Loop
Next i
Console.ReadLine()

Monday, November 8, 2010

Monday November 8 2010

This is a tutorial that I got off of youtube that sort of works, dealing with classes.

Sunday, November 7, 2010

Sunday 11/7/2010


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = System.Environment.MachineName
TextBox2.Text = System.Environment.UserName
TextBox3.Text = My.Computer.Info.OSFullName
TextBox4.Text = My.Computer.Info.OSPlatform
TextBox5.Text = My.Computer.Info.OSVersion
TextBox6.Text = My.Computer.Info.InstalledUICulture.ToString

End Sub


Here is a quick tutorial on OpenFileDialog


Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
TextBox1.Text = OpenFileDialog1.FileName

End Sub

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


Here is a tutorial I did on drawing a line in a picture box.


Public Class Form1


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



Function getX1()
Dim S As Integer
If TextBox1.Text = "" Then
S = 0
Else
S = CInt(TextBox1.Text)
End If
Return S
End Function

Function getX2()
Dim S As Integer
If TextBox2.Text = "" Then
S = 0
Else
S = CInt(TextBox2.Text)
End If
Return S
End Function

Function getY1()
Dim S As Integer
If TextBox3.Text = "" Then
S = 0
Else
S = CInt(TextBox3.Text)
End If
Return S
End Function


Function getY2()
Dim S As Integer
If TextBox3.Text = "" Then
S = 0
Else
S = CInt(TextBox4.Text)
End If
Return S
End Function





Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim S(1) As Integer
Dim F(1) As Integer

Dim pen As Pen = Pens.Blue

S(0) = getX1()
S(1) = getX2()
F(0) = getY1()
F(1) = getY2()

e.Graphics.DrawLine(pen, S(0), S(1), F(0), F(1))
End Sub
End Class


Here is a variation I did on a tutorial by Michael Halvorson from his book Microsoft Visual Basic 2005 Step by Step which is by the way so far with of my favorite instructional books on using VB so far.



Imports System.IO

Public Class Form1

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

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'This is based on a tutorial by Michael Halvorson with some modifications of my own (in my version, the user can select the text to be uploaded on their own.
Dim fileLocation As String = ""
Dim streamToDisplay As StreamReader

Call getFileName(fileLocation)
TextBox1.Text = fileLocation


streamToDisplay = New StreamReader(fileLocation)
textDisplay.Text = streamToDisplay.ReadToEnd
streamToDisplay.Close()
textDisplay.Select(0, 0)

End Sub


Sub getFileName(ByRef fileLocation)
OpenFileDialog1.ShowDialog()
'I got this to work with notepad style text files, but not with document files. oh well.
OpenFileDialog1.Filter = "Text files (*.txt) |*.txt | Document files (*.doc) | *doc"
If OpenFileDialog1.FileName <> "" Then
fileLocation = OpenFileDialog1.FileName
lblFileName.Text = OpenFileDialog1.FileName
Else
MsgBox("You did not select a file")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "Text files (*.txt) | *.txt"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
PrintLine(1, textDisplay.Text) ' copy text to disk
FileClose(1)

End If


End Sub
End Class

Friday, November 5, 2010

Friday 11/05/10

I am going to be working with Michael Halvorsons tutorials on working with object collections

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctrl In Controls
ctrl.text = "click me!"


Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each ctrl In Controls
ctrl.Left = ctrl.Left + 25
Next
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each ctrl In Controls
If ctrl.Name <> "Button1" Then
ctrl.Left = ctrl.Left + 30
End If
Next
End Sub


Here is another tutorial


Dim URLSVisited As New Collection()



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

End Sub


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
URLSVisited.Add(TextBox1.Text)
System.Diagnostics.Process.Start(TextBox1.Text)

End Sub

Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim URLName As String = "", AllURLS As String = ""
For Each URLName In URLSVisited
AllURLS = AllURLS & URLName & vbCrLf
Next URLName
MsgBox(AllURLS, MsgBoxStyle.Information, "Web sites visisted")

End Sub

Thursday, November 4, 2010

Thursday, November 4th 2010

Here is a program using .toCharArray


Module Module1

Sub Main()
Dim string1 As String = "Hello!"
Dim stopNow As Boolean = False

Call printChar(string1)

Do Until stopNow = True
' I used two functions in this code, getString() and stopOrNot()
string1 = getString()
Call printChar(string1)
stopNow = stopOrNot()
Loop





End Sub

Sub printChar(ByVal string1)

Dim charArray() As Char = string1.ToCharArray
For i = 0 To charArray.Length - 1
Console.WriteLine(charArray(i))
System.Console.Beep(20 * i + 37, 150)
Next
End Sub

Function getString()
Dim stringHere As String
Console.WriteLine("Please enter a string.")
stringHere = Console.ReadLine
Return (stringHere)
End Function

Function stopOrNot()
Dim stringHolder As String
Console.WriteLine("Would you like to enter another string?")
Console.WriteLine("Press enter to continue or else 'Q' to quit")
stringHolder = Console.ReadLine()
stringHolder = stringHolder.ToUpper
If stringHolder = "Q" Then
Return (True)
Else
Return (False)
End If
End Function
End Module


I got this to work but it was very very painful.


Imports System.IO

Public Class Form1

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

Dim Di As New IO.DirectoryInfo("C:\users\Alfred\documents")
Dim filesList() As IO.FileInfo = Di.GetFiles("*.doc")

Dim fullText As String = ""

Dim fileInfo As IO.FileInfo


Dim fileNameArray(1000, 1) As String
Dim arrayCounter As Integer = 0

For Each fileInfo In filesList

fileNameArray(arrayCounter, 0) = fileInfo.Name

arrayCounter += 1
Next

Dim number As Integer = 1
For i = 0 To arrayCounter - 1
fileNameArray(i, 1) = number + i
Next




For i = 0 To arrayCounter - 1
fullText = fullText & fileNameArray(i, 1) & vbTab & fileNameArray(i, 0) & vbCrLf
Next

showScreen.Text = fullText


End Sub


Here is a program for saving a file



Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SaveFile As New SaveFileDialog
SaveFile.FileName = ""
SaveFile.Filter = "Text Files (*.txt) | *.txt"
SaveFile.Title = "Save"
SaveFile.ShowDialog()
Try
Dim writeFile As New System.IO.StreamWriter(SaveFile.FileName)
writeFile.Write(RichTextBox1.Text)
writeFile.Close()
MsgBox("File Saved!")


Catch ex As Exception

End Try
End Sub


Here is another For loop



Option Strict On
Imports System

Module Module1

Sub Main()
Dim loopCounter As Integer
For loopCounter = 0 To 9
Console.WriteLine("loopCounter: {0}", loopCounter)

Next
Console.ReadLine()
End Sub


Here is yet another FOR loop, with > 1 steps


Option Strict On
Imports System

Module Module1

Sub Main()
Dim sum = 0, number As Integer

For number = 2 To 100 Step 2
sum += number
Console.WriteLine(sum)
System.Console.Beep(55 + number + 2, CInt(75 + (number / 2)))


Next

Console.WriteLine("The sum is " & sum & " Even integers from 2 to 100")
Console.ReadLine()

End Sub

End Module

Tuesday, November 2, 2010

Tuesday, November 2, 2010


Dim targetString As String

targetString = Console.ReadLine()
targetString = targetString.ToUpper

Select Case targetString


Case "A" To "L"
Console.WriteLine("A to L Executed")
Case "M" To "Z"
Console.WriteLine("M to Z Executed")
Case Else
Console.WriteLine("Else executed")
End Select
Console.ReadLine()


The next tutorial deals with days of the week


Dim strMessage As String

Select Case Now.DayOfWeek
Case DayOfWeek.Monday
strMessage = "Welcome Back!"
Case DayOfWeek.Wednesday
strMessage = "Hump day!"
Case DayOfWeek.Friday
strMessage = "TGIF!"
Case Else
strMessage = "Keep on truckin'!"
End Select
Console.WriteLine(strMessage)
Console.ReadLine()


Here is a tutorial using both fixed and range values.


Dim intInput As Integer
intInput = Console.ReadLine
Select Case intInput
Case 1
Console.WriteLine("Thank you for entering 1")
Case 2 To 10
Console.WriteLine("You entered a number between 2 and 10")
Case Is > 10
Console.WriteLine("That number is greater than 10. Too much!")

End Select
Console.ReadLine()


Here is a tutorial using several cases at once



Dim animal As String
Console.WriteLine("Choose between a snake, a bird, a horse, a cat, a dog, and a centipede.")
animal = Console.ReadLine()
animal = animal.ToUpper

Select Case animal
Case "BIRD"
Console.WriteLine("it has two legs")
Case "HORSE", "CAT", "DOG"
Console.WriteLine("it has four legs")
Case "SNAKE"
Console.WriteLine("it has no legs")
Case "CENTIPEDE"
Console.WriteLine("it has 100 legs")
End Select
Console.ReadLine()


Here is a tutorial I got from you tube on with statements

Public Class Form1

Dim btn1 As New Button
Dim btn2 As New Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.Text = "Working with the With statement."
.Size = New Point(500, 300)
.Location = New Point(400, 400)
End With

With btn1
.Location = New Point(20, 20)
.Text = "Click it"
.Enabled = "True"
.Width = 40
End With

With btn2
.Location = New Point(20, 80)
.Text = "Click it number 2"
.Enabled = False
.Width = 70
End With

Me.Controls.Add(btn1)
Me.Controls.Add(btn2)

End Sub
End Class


Here is a tutorial deal with With and forms


Dim frm As New Form
With frm
.BackColor = Color.Blue
.ForeColor = Color.Red
.Text = "The with statement"
Dim fnt As Font = .Font
MsgBox(fnt.Name)
.Enabled = True
.TopMost = True
.ShowDialog()

End With


Here is the final tutorial for today using a nested With statement



Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1 : Inherits Form
Public Shared Sub Main()
Dim frm As New Form1
Application.Run(frm)
End Sub

Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.BackColor = Color.Gray
.ForeColor = Color.Red
.Text = "The with statement"
.Enabled = True
.TopMost = True
.MinimizeBox = False
For Each ctrl As Control In .Controls
With ctrl
.BackColor = SystemColors.Window
.AutoSize = False
End With


Next
End With
End Sub

End Class

Monday, November 1, 2010

Monday, November 1st

Here is a console application for writing a string backwards


Sub Main()
Dim InputString As String
Dim RevString As String = ""

Console.WriteLine("Enter a string.")
InputString = Console.ReadLine()

For X = InputString.Length - 1 To 0 Step -1
RevString = RevString & InputString.Substring(X, 1)
Console.WriteLine(RevString)
System.Console.Beep(100 * X + 50, 100)
Next

Console.WriteLine(RevString)
Console.ReadLine()

End Sub


Here is a program that prints information from a toolbox


Private Sub PrintText(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
ev.Graphics.DrawString(TextBox1.Text, New Font("Arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)
ev.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim PrintDoc As New PrintDocument
AddHandler PrintDoc.PrintPage, AddressOf Me.PrintText
PrintDoc.Print()

Catch ex As Exception
MessageBox.Show("Sorry, there was a problem.", ex.ToString())

End Try
End Sub


here is a program reviewing Case select


Module Module1

Sub Main()
Dim grade As String
Dim averageScore As Double
Dim numberOfTests As Single = 0
Dim keepGoing As Boolean = True
Dim booleanInput As String
Dim aCount, bCount, cCount, dCount, fCount As Integer


Do While keepGoing = True
Console.WriteLine("Enter a score or enter 'Q' to Quit")
grade = Console.ReadLine()
If grade = "Q" Or grade = "q" Then
keepGoing = False
Else
keepGoing = True
grade = CDbl(grade)
averageScore = averageScore + grade
numberOfTests = numberOfTests + 1
getGrade(grade, aCount, bCount, cCount, dCount, fCount)

End If


Loop
Console.WriteLine("The average score is " & getAverage(averageScore, numberOfTests))

Console.WriteLine("Number of As:" & aCount)
Console.WriteLine("Number of Bs:" & bCount)
Console.WriteLine("Number of Cs:" & cCount)
Console.WriteLine("Number of Ds:" & dCount)
Console.WriteLine("Number of Fs:" & fCount)
Console.ReadLine()





End Sub

Function getAverage(ByVal averageScore, ByVal numberOfTests)
averageScore = averageScore / numberOfTests
Return (averageScore)
End Function

Sub getGrade(ByVal grade As Double, ByRef aCount As Integer, ByRef bCount As Integer, ByRef cCount As Integer, ByRef dCount As Integer, _
ByRef fCount As Integer)
Select Case grade
Case 100
Console.WriteLine("Perfect score!" & vbCrLf & "Letter grade: A" & vbCrLf)
aCount += 1
Case 90 To 99
Console.WriteLine("Letter grade: A" & vbCrLf)
aCount += 1
Case 80 To 89
Console.WriteLine("Letter grade: B" & vbCrLf)
bCount += 1
Case 70 To 79
Console.WriteLine("Letter grade: C" & vbCrLf)
cCount += 1
Case 60 To 69
Console.WriteLine("Letter grade: D" & vbCrLf)
dCount += 1
Case 0 To 59
Console.WriteLine("Letter grade: F" & vbCrLf)
fCount += 1

End Select
End Sub
End Module