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

Sunday, October 31, 2010

Sunday October 21st 2010


Dim dtCurrent As System.DateTime
Dim iHour As Integer

dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour

If (iHour < 12) Then
Console.WriteLine("Good morning!")
ElseIf (iHour >= 12) And (iHour < 18) Then
Console.WriteLine("Good afternoon!")
Else
Console.WriteLine("Good evening!")
End If
Console.ReadLine()




Dim temp As Integer = 32


For i = 1 To temp
If temp <= 32 Then
If temp = 32 Then
Console.WriteLine("temp = 32")
Else
Console.WriteLine("Temp: {0}", temp)
End If
End If
' I added a cool sound effect here just for fun
System.Console.Beep(10 * temp + 40, 7 * temp + 35)

temp = temp - 1
Next i



Dim superString As String

superString = Console.ReadLine()

If superString.Length > 10 Then
Console.WriteLine("Your string is longer than ten")
Else
Console.WriteLine("Your string is less than ten")
End If

Dim length As Integer = superString.Length

For i = 0 To superString.Length - 1
Console.WriteLine(superString.Chars(i))
System.Console.Beep(100 + i * 5 + 40, 100 + i * 4 + 40)

Next

Friday, October 29, 2010

Friiday, October 29th 2010



Public Class Rectangle

Dim dblLength As Double
Dim dblWidth As Double



Public Function CalculateArea() As Double
Return (dblLength * dblWidth)
End Function

Sub New(ByVal [_length] As Double, ByVal [_width] As Double)
dblLength = _length
dblWidth = _width

End Sub

End Class



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myLength As Double = InputBox("Please enter the length of the box")
Dim myWidth As Double = InputBox("Please enter the width of the box")

Dim myRectangle As New Rectangle(myLength, myWidth)
Me.Label1.Text = "The area is: " & myRectangle.CalculateArea

makeRectangle(myLength, myWidth)


End Sub

Public Sub makeRectangle(ByVal myLength, ByVal myWidth)


Dim firstX, firstY, secondX, secondY As Integer

firstY = 30
firstX = 30
secondY = firstY
secondX = (firstX + myLength) * 2

drawLine(firstX, firstY, secondX, secondY)

firstY = (30 + myWidth) * 2
firstX = 30
secondY = firstY
secondX = (firstX + myLength) * 2

drawLine(firstX, firstY, secondX, secondY)


firstY = 30
firstX = 30
secondY = (firstY + myWidth) * 2
secondX = firstX

drawLine(firstX, firstY, secondX, secondY)

firstY = 30
firstX = (30 + myLength) * 2
secondY = (firstY + myWidth) * 2
secondX = firstX

drawLine(firstX, firstY, secondX, secondY)

End Sub

Private Sub drawLine(ByVal firstX As Integer, ByVal firstY As Integer, ByVal secondX As Integer, ByVal secondY As Integer)
Dim myGraphics As Graphics
Dim myPen As Pen

'’ set the color and the width of the pen
myPen = New Pen(Color:=Color.Blue, Width:=2)

'’ set where the graphics will be drawn
myGraphics = Graphics.FromHwnd(hwnd:=ActiveForm().Handle)

'’ draw the line between two points
myGraphics.DrawLine(pen:=myPen, x1:=firstX, y1:=firstY, x2:=secondX, y2:=secondY)
End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Thursday, October 28, 2010

Thursday, 10/28/2010

Here is a tutorial done by Evangelos Petroutsos


Dim Payment As Double
Dim LoanIRate As Double
Dim LoanDuration As Integer
Dim LoanAmount As Integer

' Validate amount
If IsNumeric(txtAmount.Text) Then
LoanAmount = Convert.ToInt32(txtAmount.Text)
Else
MsgBox("Please enter a valid amount.")
Exit Sub
End If
' Validate interest rate
If IsNumeric(txtRate.Text) Then
LoanIRate = 0.01 * Convert.ToDouble(txtRate.Text) / 12
Else
MsgBox("Invalid interest rate, please re-enter.")
Exit Sub
End If
'Validate loan's duration
If IsNumeric(txtDuration.Text) Then
LoanDuration = Convert.ToInt32(txtDuration.Text)
Else
MsgBox("Please specifiy the loan's duration as a number of months")
Exit Sub
End If
'If all data were validated, proceed with calculations
Dim payEarly As DueDate
If chkPayEarly.Checked Then
payEarly = DueDate.BegOfPeriod
Else
payEarly = DueDate.EndOfPeriod
End If
Payment = Pmt(LoanIRate, LoanDuration, -LoanAmount, 0, payEarly)
txtPayment.Text = Payment.ToString("#.00")


Here is a rich text editor with lots of dialog boxes dealing with saving, loading etc.


Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click

End Sub

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
txtField.Clear()

End Sub

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Try
Dim dlg As OpenFileDialog = New OpenFileDialog
dlg.Title = "Open"
dlg.Filter = "Rich Text Files (*.rtf) | *.rtf"
If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtField.LoadFile(dlg.FileName)
End If
Catch ex As Exception : End Try
End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Try
Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Title = "Save"
dlg.Filter = "Rich Text Files (*.rtf) | *.rtf"
If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtField.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
End If
Catch ex As Exception : End Try
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub

Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click

End Sub

Private Sub UnoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnoToolStripMenuItem.Click
txtField.Undo()

End Sub

Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
Try
Dim dlg As FontDialog = New FontDialog
dlg.Font = txtField.Font
If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
txtField.Font = dlg.Font
End If
Catch ex As Exception

End Try
End Sub

Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
Try
Dim dlg As ColorDialog = New ColorDialog
dlg.Color = txtField.ForeColor
If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
txtField.ForeColor = dlg.Color
End If
Catch ex As Exception

End Try
End Sub


I also did a tutorial on do while loops


Dim Num1 As Integer = 1

Do While Num1 <= 10
MessageBox.Show("the value of the variable is: " & Num1)
Num1 = Num1 + 1
Loop