Wednesday, September 29, 2010

Wednesday, September 29 2010

I'm still looking at the tutorials Evangelos Petroutsos wrote, they are quite good.

I'd like to finish the tutorials by Halverson in his book Visual Basic 2005 Step by Step Overall I've really liked his tutorials. I think it would be good to finish a book in the near future.

Here is a quick example tutorial from Petrousos



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim password As String, ch As Char
Dim i As Integer
Dim valid As Boolean = False
While Not valid
password = InputBox("Please enter your password.")
For i = 0 To password.Length - 1
ch = password.Chars(i)
If Not Char.IsLetterOrDigit(ch) Then
valid = True
Exit For
End If
Next
If valid Then
MsgBox("Your new password will be activated immediately!")
Else
MsgBox("Your password must contain at least one special symbol!")
End If
End While
End Sub
End Class


I also made up my own small program using as an assignment from the Petroutsos book.



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim firstName(4) As String
Dim lastName(4) As String

For i = 1 To 5
firstName(i + 1) = InputBox("Please enter the first name.")
lastName(i + 1) = InputBox("Please enter the last name.")
txtDisplay.Text = txtDisplay.Text + firstName(i + 1) & " " & lastName(i + 1) & vbCrLf
Next i
End Sub

Monday, September 27, 2010

Monday, September 27th 2010

I did this tutorial on functions and factorials based off of a YouTube video here mkaatr's channel




Function Factorial(ByVal V As Double) As Double
Dim F As Double
Dim I As Integer
F = 1
For I = 1 To V
F = F * I

Next
Return F
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number As Double
number = CDbl(TextBox1.Text)
number = CStr(Factorial(number))
txtFactorials.Text = number
End Sub


I also did another tutorial based on ByVal and ByRef



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim number As Integer = 50
MsgBox(number)
Call byValTest(number)
MsgBox(number)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim number As Integer = 50
MsgBox(number)
Call byRefTest(number)
MsgBox(number)
End Sub


There was also a tutorial on creating a swap function




Function swap(ByRef V1 As Integer, ByRef V2 As Integer)
Dim tmp As Integer
tmp = V1
V1 = V2
V2 = tmp
End Function


There is also a quite lengthy tutorial on subroutines from mkaatr



Public Class Form1
Dim names As New Collection
Dim tels As New Collection

Sub addContact(ByVal CName As String, ByVal CTel As String)
names.Add(CName)
tels.Add(CTel)
End Sub

Sub viewContacts(ByVal DGV As DataGridView)
DGV.Rows.Clear()
Dim I As Integer
For I = 1 To names.Count
DGV.Rows.Add(names(I), tels(I))
Next
End Sub


Function GetTelForName(ByVal Name As String) As String
Dim i As Integer
For i = 1 To names.Count
If names(i) = Name Then
Return tels(i)
End If
Next
Return ""
End Function


Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub

Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewToolStripMenuItem.Click

Dim n As String
Dim t As String

n = InputBox("Enter the name of the contact:")
If n = "" Then
Exit Sub
End If

t = InputBox("Enter the tel number:")
If t = "" Then
Exit Sub
End If

addContact(n, t)
viewContacts(DataGridView1)

End Sub




Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
Dim n As String
Dim t As String

n = InputBox("Enter the name you are searching for:")
If n = "" Then
Exit Sub
End If

t = GetTelForName(n)
If t = "" Then
MsgBox("Name not found")
Else
MsgBox("The telephone number is:" & t)
End If
End Sub
End Class

Thursday, September 23, 2010

Thursday, September 23rd 2010

Using another tutorial by Evangelos Petroutsos




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim password As String, ch As Char
Dim i As Integer
Dim valid As Boolean = False
While Not Valid
password = InputBox("Please enter your password")
For i = 0 To password.Length - 1
ch = password.Chars(i)
If Not Char.IsLetterOrDigit(ch) Then
valid = True
Exit For
End If
Next
If valid Then
MsgBox("Your new password will be activated immediately!")
Else
MsgBox("Your new password must contain at least one special symbol!")

End If
End While
End Sub

Wednesday, September 22, 2010

Wednesday, September 22


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If My.Computer.Clipboard.ContainsText Then
TextBox1.Text = System.DateTime.Now
TextBox1.Text = TextBox1.Text + vbNewLine
TextBox1.Text = TextBox1.Text + My.Computer.Clipboard.GetText

End If
End Sub




Public time As String
Public stringClipboard As String

Public Function getTime()
Dim dateTimeInfo As String = System.DateTime.Now
Return dateTimeInfo

End Function


Public Function getString()
Dim clipboard1 As String = My.Computer.Clipboard.GetText
Return clipboard1

End Function

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick



time = getTime()
stringClipboard = getString()



If My.Computer.Clipboard.ContainsText Then
TextBox1.Text = TextBox1.Text + time + vbNewLine
TextBox1.Text = TextBox1.Text + stringClipboard + vbNewLine


End If
End Sub

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


End Sub




Public time As String
Public stringClipboardNew As String
Public pastText As String
Public i As Integer

Public Function getTime()
Dim dateTimeInfo As String = System.DateTime.Now
Return dateTimeInfo

End Function


Public Function getString()
Dim clipboard1 As String = My.Computer.Clipboard.GetText
Return clipboard1

End Function

Public Sub printSomething()
TextBox1.Text = TextBox1.Text + time + vbNewLine
TextBox1.Text = TextBox1.Text + stringClipboardNew + vbNewLine
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick



time = getTime()
stringClipboardNew = getString()
i += 1

If stringClipboardNew = pastText Then
Else
If i <= 1 Then
If My.Computer.Clipboard.ContainsText Then
Call printSomething()
pastText = stringClipboardNew
Else
Call printSomething()
End If
End If
End If


End Sub

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

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub


This is the final version of what I was try to do, it didn't work out so well, but it kind of works



Public time As String
Public stringClipboardNew As String
Public pastText As String
Public i As Integer

Public booleanResult As Boolean


Public Function getTime()
Dim dateTimeInfo As String = System.DateTime.Now
Return dateTimeInfo

End Function


Public Function getString()
Dim clipboard1 As String = My.Computer.Clipboard.GetText
Return clipboard1

End Function

Public Sub printSomething()
TextBox1.Text = TextBox1.Text + time + vbNewLine
TextBox1.Text = TextBox1.Text + stringClipboardNew + vbNewLine
End Sub

Public Sub compare(ByVal pastText As String)

stringClipboardNew = getString()
If pastText = stringClipboardNew Then
booleanResult = True
Else
booleanResult = False
End If

Return



End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick



time = getTime()


i += 1


If i <= 1 Then
If My.Computer.Clipboard.ContainsText Then
stringClipboardNew = getString()
pastText = stringClipboardNew
Call printSomething()
Else
compare(pastText)
If booleanResult = False Then
Call printSomething()
Else

End If
End If
End If




End Sub

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

End Sub

Tuesday, September 21, 2010

Tuesday, September 21 2010

I am going to start yet another set of tutorials, these written by Evangelos Petroutsos.

After I do a few of these, I hope to get back to Michael Halvorson's tutorials, which are great. Then I'll finish Anne Boehm's tutorials. Hopefully I will have all this done by mid-October.


Dim language As String
language = ComboBox1.Text
If language = "Visual Basic" Then
MsgBox("We have a winner!")
Else
MsgBox(language & "is not a bad langauge.")
End If

Monday, September 20, 2010

Monday, September 20 2010

Option Strict On is something I need to start doing, but I am not entirely sure why.

Basically, it seems to make it easier to debug your programs.

Here is the tutorial I wrote, based off of a tutorial from www.java2s.com/tutorial/vb



Module Module1

Public negative As Long = 33

Sub beepUp(ByRef negative)
For i = 1 To 10
Console.Beep(200, 200)
Threading.Thread.Sleep(1000)
negative += 3
Console.WriteLine(negative)
Next i
End Sub

Sub Main()
Dim maxValue As Byte = &HFF
Dim posValue As UInteger = &HF034
Dim negValue As Integer = &HF034
Dim negative As Long = -44

Console.WriteLine(maxValue)
Console.WriteLine(posValue)
Console.WriteLine(negValue)

Call beepUp(negative)


Console.WriteLine(negative)

For i = 1 To 10
Console.Beep(200, 200)
Threading.Thread.Sleep(1000)
negative += -2
Console.WriteLine(negative)
Next i

End Sub

I added a lot to this, including the subroutine, the loops, the long value, etc.


End Module


I then made the following changes - created a Sub called beepUp that replaced the for i function, and changed it to byVal. I think I more or less have byVal and byVar fixed in my head.


Module Module1

Public negative As Long = 33

Sub beepUp(ByRef negative)
For i = 1 To 10
Console.Beep(200, 200)
Threading.Thread.Sleep(1000)
negative += 3
Console.WriteLine(negative)
Next i
End Sub

Sub beepDown(ByVal negative)
For i = 1 To 10
Console.Beep(300, 250)
Threading.Thread.Sleep(500)
negative += -5
Console.WriteLine(negative)
Next
End Sub

Sub Main()
Dim maxValue As Byte = &HFF
Dim posValue As UInteger = &HF034
Dim negValue As Integer = &HF034
Dim negative As Long = -44

Dim craziness As Integer

craziness = negative

Console.WriteLine(craziness)
Console.Beep(100, 300)
Console.WriteLine(negative)
Console.Beep(100, 300)

Console.WriteLine(maxValue)
Console.WriteLine(posValue)
Console.WriteLine(negValue)

Call beepUp(negative)


Console.WriteLine(negative)

Call beepDown(negative)

Console.WriteLine(negative)


Call beepUp(negative)

End Sub

End Module


I also did another one of their console tutorials.




Sub main()
Dim result As New System.Text.StringBuilder()
result.AppendLine("MaxValue...")


Dim maxByte As Byte = Byte.MaxValue
Dim maxSByte As SByte = SByte.MaxValue
Dim maxShort As Short = Short.MaxValue
Dim maxUShort As UShort = UShort.MaxValue
Dim maxInteger As Integer = Integer.MaxValue
Dim maxUInteger As UInteger = UInteger.MaxValue
Dim maxLong As Long = Long.MaxValue
Dim maxULong As ULong = ULong.MaxValue

result.Append("Byte = ").AppendLine(CStr(maxByte))
result.Append("SByte = ").AppendLine(CStr(maxSByte))
result.Append("Short = ").AppendLine(CStr(maxShort))
result.Append("UShort = ").AppendLine(CStr(maxUShort))
result.Append("Integer = ").AppendLine(CStr(maxInteger))
result.Append("UInteger = ").AppendLine(CStr(maxUInteger))


Console.WriteLine(result.ToString())
Threading.Thread.Sleep(3000)

End Sub


I did another tutorial, only with option explicit set to On. I also changed the addition into a function.


Option Explicit On

Module Module1

Public sumOfNumbers As Integer

Function addition(ByVal number1 As Integer, ByVal number2 As Integer)
sumOfNumbers = number1 + number2 ''add numbers
Return sumOfNumbers
End Function

Sub Main()
Dim firstNumber, secondNumber As String
Dim number1, number2, sumOfNumbers As Integer
firstNumber = CStr(10)
secondNumber = CStr(20)

number1 = CInt(firstNumber)
number2 = CInt(secondNumber)

Call addition(number1, number2)


sumOfNumbers = number1 + number2 ''add numbers
Console.WriteLine("The is is {0}", sumOfNumbers)

System.Console.Beep(300, 300)

Threading.Thread.Sleep(3000)

End Sub
End Module


Here is another tutorial I worked on


Dim n As Integer
System.Console.Beep(200, 200)
n = 10
n += 5
n = CStr(n)
Console.WriteLine("addition " & n)

Threading.Thread.Sleep(1000)
System.Console.Beep(200, 200)

n = 10
n -= 5
n = CStr(n)
Console.WriteLine("subtraction " & n)

Threading.Thread.Sleep(1000)
System.Console.Beep(200, 200)

n = 10
n *= 5
n = CStr(n)
Console.WriteLine("multiplication " & n)

Threading.Thread.Sleep(1000)
System.Console.Beep(200, 200)

n = 10
n /= 5
n = CStr(n)
Console.WriteLine("multiplication " & n)
Threading.Thread.Sleep(1000)
System.Console.Beep(200, 200)

Friday, September 17, 2010

Friday, September 17th 2010

Using a Patrice Pelland tutorial I was able to add a splash screen to one of the applications I am developing. It is quite cool.

The one bit of real code from it is


My.Application.MinimumSplashScreenDisplay.Time = 3500
http://www.blogger.com/post-edit.g?blogID=1128425782735047114&postID=4743475277019314193


I also added an about to the program


AboutBox1.ShowDialog()

Thursday, September 16, 2010

Thursday, September 16th 2010

I wanted the number randomizer program to produce three new random numbers, which wasn't happening, so I modified it a bit.

The problem is that I don't have a random number for the first low.




Function randomizer(ByVal Low As Long) As Integer

Randomize()
Dim rndNumber As Random
rndNumber = New Random
number = rndNumber.Next(Low, 1001)
Return Number
End Function

Private Sub btnRandomize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomize.Click
Dim numberOne As Integer
Dim numberTwo As Integer
Dim numberThree As Integer
numberOne = randomizer(4)
txtNumberOne.Text = numberOne
numberTwo = randomizer(numberOne)
txtNumberTwo.Text = numberTwo
numberThree = randomizer(numberTwo)
txtNumberThree.Text = numberThree

End Sub


I then decided to use the current second as the seed number



Private Sub btnRandomize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomize.Click

Dim numberOne As Integer
Dim numberTwo As Integer
Dim numberThree As Integer


numberOne = randomizer((Second(Now())))
txtNumberOne.Text = numberOne
numberTwo = randomizer(numberOne)
txtNumberTwo.Text = numberTwo
numberThree = randomizer(numberTwo - (Second(Now())))
txtNumberThree.Text = numberThree

If numberThree < 100 Then System.Console.Beep(200, 200)

End Sub


Finally, I added some things to make the fire emergency for the game.



Sub disasterCheck()

Dim numberOne As Integer
Dim numberTwo As Integer
Dim numberThree As Integer


numberOne = randomizer((Second(Now())))
numberTwo = randomizer(numberOne)
numberThree = randomizer(numberTwo - (Second(Now())))

If numberThree < 50 Then


0 Then
Call fire()

Else

End If

If (numberOne + 10) > numberThree Then
generatorBroken = True
Else
generatorBroken = False
End If
End Sub

Sub fire()
firePicture.Visible = True
noButtonClock += 3
power += -2
onfire = True
End Sub


Also working on a Michael Halvorson tutorial:


Sub AddName(ByVal Team As String, ByRef ReturnString As String)
Dim Prompt, Nm, WrapCharacter As String
Prompt = "Enter a " & Team & " employee."
Nm = InputBox(Prompt, "Input Box")
WrapCharacter = Chr(13) + Chr(10)
ReturnString = Nm & WrapCharacter
End Sub


Adapting a tutorial from Patrice Peland I was able to add a save feature, which was very very cool.

Here is the code from that


Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As _
System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Try
My.Computer.FileSystem.WriteAllText(Me.SaveFileDialog1.FileName, _
Me.txtSales.Text & vbCrLf, False)
My.Computer.FileSystem.WriteAllText(Me.SaveFileDialog1.FileName, _
Me.txtMkt.Text & vbCrLf, True)
Catch fileException As ApplicationException
Throw fileException
End Try
End Sub


Note that I had a lot of trouble with this, not least of which was adding "false" or "true" to the end of the arguments.

Wednesday, September 15, 2010

Wednesday, September 15th 2010

here is the code from the Lucky 7 tutorial by Michael Halvorson


Function HitRate(ByVal Hits As Short, ByVal Tries As Short) As String
Dim percent As Single
percent = Hits / Tries
Return Format(percent, "0.0%")
End Function


This isn't the first function that I have written although I am not entirely sure on what it is or what it is for.



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Visible = False
Randomize()
Label1.Text = CStr(Int(Rnd() * 10))
Label2.Text = CStr(Int(Rnd() * 10))
Label3.Text = CStr(Int(Rnd() * 10))
Spins = Spins + 1

If (Label1.Text = "7") Or (Label2.Text = "7") _
Or (Label3.Text = "7") Then
PictureBox1.Visible = True
Beep()
Wins = Wins + 1
lblWins.Text = "Wins: " & Wins
End If

lblRate.Text = HitRate(Wins, spins)

End Sub


I added subs to my radiation computer game



Sub emergencyVentAction()
pressure += -30
power += -20
score += 10
End Sub

Sub bathAction()
pressure += 5
radiation += -25
power += -20
score += 10
End Sub


Finally I am including here my own program using a function


Public Class Form1
Public number As Integer

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

Function randomizer() As Integer
Dim rndNumber As Random
rndNumber = New Random
number = rndNumber.Next(1, 1001)
Return number
End Function

Private Sub btnRandomize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomize.Click
Dim numberOne As Integer
numberOne = randomizer()
MsgBox(numberOne)
End Sub
End Class

Monday, September 13, 2010

Monday, September 13 2010

I worked a little with global variables


Public pressure As Integer
Public power As Integer
Public ClaxonLoud As Integer


These were global variables for the "computer game" I am making (it isn't much of a game, really. But it helps me to put into practice things from the tutorials.


System.Console.Beep(ClaxonLoud, 200)
power += 1
score += 1

ProgressBar1.Value = pressure
If pressure < 50 Then
ClaxonLoud = 200
pressure += 2
ElseIf pressure >= 50 And pressure < 100 Then
ClaxonLoud = 300
pressure += 2
ElseIf pressure >= 100 And pressure < 150 Then
pressure += 5
ClaxonLoud = 400
ElseIf pressure >= 150 And pressure < 200 Then
pressure += 5
ClaxonLoud = 500
Else
Timer1.Enabled = False
End If


If pressure > 50 Then
EmergencyVent.Enabled = True
Else
EmergencyVent.Enabled = False
End If


Also I threw in a button and a button click event.



If power > 10 Then
pressure += -25
power += -10
Else
EmergencyVent.Enabled = False
End If


I am going to be working on some tutorials by Halvorson on functions. Michael Halvorson points out that there is a new statement offered in Visual Basic, the Return statement The return statement tells when you want to return a value and what that value is.

Friday, September 10, 2010

Friday, September 10 2010

Forgot to set the Timer to enabled = true


If TextBox1.Text = "secret" Then
Timer1.Enabled = False
MsgBox("Welcome to the system!")
End
Else
MsgBox("That password is incorrect")
End If


The weird thing is that the program turns off afterwards.

i tried to change the timer from ending the program to selecting the text box and centering the form on the screen but I could only get it to select the textbox.



MsgBox("Sorry, your time is up.")

Me.StartPosition = FormStartPosition.CenterScreen
TextBox1.Select()


THe next Michael Halvorson tutorial is about creating modules and procedures. It uses the lucky number 7 tutorial, which I figured out from messing with it doesn't actual generate random numbers per se. the trick is to use the system clock for the generation of the numbers, it seems.

The code from the original lucky number 7 was


label1.text CStr(Int(Rnd() * 10))


I added a Randomize() before the old code and that did seem to randomize it a bit, which was good.

For a quick break, I did a status bar which I am going to incorporate into one of my game projects



If ProgressBar1.Value < 100 Then
ProgressBar1.Value += 2
ElseIf ProgressBar1.Value >= 100 And ProgressBar1.Value < 200 Then
ProgressBar1.Value += 5
Else
Timer1.Enabled = False
End If

Thursday, September 9, 2010

Thursday September 9th 2010

Doing console aps now using tutorials from www.java2s.com. So far it's very cool.



System.Console.WriteLine("hello world")



Doesn't show up for very long.



Dim strChar As String
Console.Write("Enter a character:")
strChar = Console.ReadKey.KeyChar
Console.WriteLine()
Console.WriteLine("You just entered {0},", strChar)
System.Console.Beep(100, 2100)


I modified the next tutorial dealing with entering a line.



Dim i As Integer
For i = 1 To 3
Dim strLine As String
If i = 1 Then
Console.Write("Enter a line of text:")
ElseIf i = 2 Then
Console.WriteLine()
Console.Write("Write another line of text:")
Else
Console.WriteLine()
Console.Write("Write yet another line of text:")
End If
strLine = Console.ReadLine
Console.WriteLine()
Console.WriteLine("You just entered: {0}", strLine)
If i Mod 2 = 0 Then
System.Console.Beep(100, 300)
Else
System.Console.Beep(300, 300)
End If

Next i



And another




Dim strMessage As String
Try
strMessage = Console.ReadLine()
Console.WriteLine("Hello! " + strMessage)
Catch ex As Exception

End Try
System.Console.Beep(200, 400)


And yet another


Dim userInput As String
Console.WriteLine("Enter a source temperature.")
userInput = Console.ReadLine()
Console.WriteLine(userInput)


The only problem is that there aren't any explanations for why we are doing these tutorials, I mean what the point is, and also I'm not entirely sure why we should be using console aps or what they are for.

I added a ucase to the following tutorial as a workaround for case sensitivity



Dim strInput As String
Do
Console.WriteLine("Please enter 'q' to quit...")
strInput = Console.ReadLine()
Console.WriteLine("You typed " & strInput)
Loop While (UCase(strInput) <> "Q")
Console.WriteLine("Quitting now.")


There is another console tutorial dealing with temperatures



Dim intInput As Integer
Console.WriteLine("Enter a temperature...")
intInput = Val(Console.ReadLine())
If intInput > 75 Then
Console.WriteLine("Too hot!")
ElseIf intInput < 55 Then
Console.WriteLine("Too cold!")
Else
Console.WriteLine("Just right!")
End If
End Sub


Here is another tutorial from www.java2s.com, I had a heck of a time getting the uCase to work as it was my personal addition to the code.


Dim strInput As String

While (UCase(strInput) <> "Q")
Console.WriteLine("You typed " & strInput)
Console.WriteLine("Please enter 'q' to quit...")
strInput = Console.ReadLine()
End While
Console.WriteLine("Quitting now.")


I did another tutorial matching patterns and threw in a beep




Dim sInput As String
Dim sPattern As String
Dim sMatch As String

System.Console.Write("please enter a pattern:")
sInput = System.Console.ReadLine()
sPattern = sInput

System.Console.Write("Please enter a string to compare against:")
sInput = System.Console.ReadLine()
sMatch = sInput

If sMatch Like sPattern Then
System.Console.WriteLine(sMatch & " matched with " & sPattern)
Else
System.Console.WriteLine(sMatch & " did not match with " & sPattern)
End If

If sMatch Like sPattern Then
Dim pitch As Integer = 200
For i As Integer = 1 To 4
System.Console.Beep(pitch, 200)
pitch += 100
Next i
Else
Dim pitch As Integer = 500
For i As Integer = 1 To 4
System.Console.Beep(pitch, 200)
pitch = pitch - 100
Next i
End If

Wednesday, September 8, 2010

Wednesday, September 8th 2010

Still looking at timers. For the next Michael Halvorson tutorial I will be looking at making a digital clock.

Timers have an interval property which is set using milliseconds (spelled with two "l"s, note to self).

To set the timer to seconds, just set the interval property of the time to 1000 milliseconds.

I expanded the tutorial so that the Start / Stop button was the same


If Timer1.Enabled = False Then
Timer1.Enabled = True
btnBegin.Text = "Stop"
Else
Timer1.Enabled = False
btnBegin.Text = "Begin"
End If


I then fooled around with the mod function to play some "music" with the timer



Dim sec As Integer = DateTime.Now.Second
If sec Mod 2 = 0 And sec Mod 4 <> 0 Then System.Console.Beep(200, 100)
If sec Mod 4 = 0 Then System.Console.Beep(400, 200)
If sec Mod 3 = 0 And sec Mod 5 <> 0 Then System.Console.Beep(300, 100)
If sec Mod 5 = 0 Then System.Console.Beep(100, 150)


I also found a great tutorial site for console apps which I want to work on in a bit

VB Console tutorials

Tomorrow I have a long workday so I am going to try and focus on it.

Tuesday, September 7, 2010

Tuesday, September 7 2010

alright I am going to do a tutorial by Michael Halvorson that involves importing pictures.


Dim i As Integer
For i = 1 To 3
pctTulsaWindow.Image = System.Drawing.Image.FromFile _
("C:\Users\Alfred\Downloads\touroftulsaprogram\tulsa" & i & ".jpg")
MsgBox("click here for next face.")
Next


Following the tutorial I further modified it as


pctTulsaWindow.Image = System.Drawing.Image.FromFile("C:\Users\Alfred\Downloads\touroftulsaprogram\tulsa" & Counter & ".jpg")

Counter += 1
If Counter = 5 Then
Counter = 1
System.Console.Beep(200, 200)
End If


The next tutorial is for converting farenheit to Celsius.



Dim FTemp, Celsius As Single
Dim strFTemp As String
Dim prompt As String = "Enter a Fahrenheit temperature."
Do
strFTemp = InputBox(prompt, "Fahrenheit to Celsius")
If strFTemp <> "" Then
FTemp = CSng(strFTemp)
Celsius = Int((FTemp + 40) * 5 / 9 - 40)
MsgBox(Celsius, , "Temperature in Celsius")
End If
Loop While strFTemp <> ""

Monday, September 6, 2010

Monday, September 6

Looking at loops again, and Michael Halvorson's tutorials.


Dim i As Integer
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
For i = 1 To 11
txtLoopResultBox.Text = txtLoopResultBox.Text & "Line " & i & Wrap
Next i


I also discovered something cool for creating a pause in the program

System.Threading.Thread.Sleep(500)



Private Sub wait()
System.Threading.Thread.Sleep(500)
End Sub

Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click

Dim i As Integer
Dim Wrap As String
Wrap = Chr(13) & Chr(10)

For i = 1 To 10 Step 1
System.Console.Beep(200, 200)
txtLoopResultBox.Text = txtLoopResultBox.Text & "Line " & i & Wrap
If i < 5 Then
wait()
Else
End If

Next i
End Sub




I finally did another modification with



Private Sub wait()
Dim waitTime As Integer
System.Threading.Thread.Sleep(waitTime)
End Sub

Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click

Dim i As Integer
Dim Wrap As String
Wrap = Chr(13) & Chr(10)

Dim waitTime As Integer
waitTime = 600


Dim loud As Integer = 200

For i = 1 To 10 Step 1
System.Console.Beep(loud, 200)
txtLoopResultBox.Text = txtLoopResultBox.Text & "Line " & i & Wrap
If i < 5 Then
wait()
Else
loud = loud + 50
waitTime = waitTime - 100
End If


Next i
End Sub

Thursday, September 2, 2010

Thursday, September 2nd

Hi!

Today I am going to be looking at Michael Halvorson's tutorials, which are excellent so far. They are low-key and fun, which is really helpful. You read a short bio here if you wish.

The first tutorial is about using loops and timers.

Let's take a look at the syntax for a for loop


For variable = start to end
statements go here
Next [variable]


The first example Michael Halvorson gives is


Dim i As Integer
For i = 1 To 4
Beep()
Next i


I changed it to


Dim i As Integer
Dim CounterVariable As Integer
CounterVariable = 5
For i = 1 To CounterVariable
beep()
Next i


I then fooled around with it a bit more and created


Dim i As Integer
Dim CounterVariable As Integer
CounterVariable = 5
For i = 1 To CounterVariable
System.Console.Beep(200, 200)
Next i


I then wanted to disable the button while the beeps where playing



Enabled = False
Dim i As Integer
Dim CounterVariable As Integer
CounterVariable = 5
For i = 1 To CounterVariable
System.Console.Beep(200, 200)
Next i
Enabled = True



I then created a textbox so the user could define the number of times to beep and threw in some simple data validation


Dim NumberOfTimes As Integer
NumberOfTimes = Convert.ToInt32(txtNumberOfTimes.Text)
If NumberOfTimes < 20 Then
Enabled = False
Dim i As Integer
Dim CounterVariable As Integer
CounterVariable = txtNumberOfTimes.Text
For i = 1 To CounterVariable
System.Console.Beep(200, 200)
Next i
Enabled = True
Else
MessageBox.Show("That number is too high!")
End If


As with all of my personal projects i went beyond my ability limited by my lack of training and inexperience and didn't get quite what i wanted, but that's OK. Tomorrow I will do more tutorials.


Dim pitch As Integer
pitch = 200

Dim pitchLoopCounter As Integer
pitchLoopCounter = 0



Dim pitchUp As Boolean
pitchUp = True


Dim pitchLoopCounterMod As Double




Dim NumberOfTimes As Integer
If Not IsNumeric(txtNumberOfTimes.Text) Then
MessageBox.Show("Please be so kind as to enter a number.", "Holy tamole")
Exit Sub
Else
NumberOfTimes = Convert.ToInt32(txtNumberOfTimes.Text)
End If

If NumberOfTimes < 35 Then
btnBeepIt.Enabled = False

Dim i As Integer

i = 0

Do
txtLoopCounter.Text = pitchLoopCounter

System.Console.Beep(pitch, 200)

i += 1
pitchLoopCounter += 1

If pitchLoopCounter = 5 Then pitchUp = False
If pitchLoopCounter = 10 Then pitchUp = True


If pitchUp = True Then
pitch += 100
Else
pitch = pitch - 100
End If

txtLoopCounter.Text = pitchLoopCounter

Loop Until i = NumberOfTimes

btnBeepIt.Enabled = True
Else
MessageBox.Show("That number is too high!", _
"Well shucks")
End If