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