Tuesday, December 21, 2010

Wednesday 12/21/10

Here is a tutorial on stacks by Wei-Ming Lee

Module Module1

Sub Main()
Dim stack1 As New System.Collections.Stack

'push the items onto the stack
stack1.Push("What ")
stack1.Push("is ")
stack1.Push("the ")
stack1.Push("point ")
stack1.Push("of ")
stack1.Push("using ")
stack1.Push("stacks? ")


'get the item count in the stack

Dim itemCount As Integer = stack1.Count
Console.WriteLine("The stack has {0} items in it", itemCount)

'pop the items out from the stack
For i As Integer = 0 To stack1.Count - 1
MsgBox(stack1.Pop()) 'strings are printed in the reverse order
Next

Console.ReadLine()
End Sub

End Module


Here is a tutorial by Michael Halvorson


Imports System.Drawing.Printing
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
'Print using an error handler to catch problems
Try
AddHandler printdocument1.printpage, AddressOf Me.printgraphic
Catch ex As Exception
MessageBox.Show("Sorry, there was a problem printing", _
ex.ToString())

End Try
End Sub

'Sub for printing graphic
Private Sub PrintGraphic(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
'create the graphic using DrawImage
ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text), _
ev.Graphics.VisibleClipBounds)
'specify that this is the last page to print
ev.HasMorePages = False

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

End Sub
End Class


and here are some tentative things I am doing with event handlers


Public Class MainClass
Private Event firstEvent()

Private Sub MainClass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RaiseEvent firstEvent()
End Sub


Sub TestEvents() Handles Me.firstEvent
MsgBox("First Event Raised")
Me.Name = "MainForm"
MsgBox("The form's name is now " & Me.Name.ToString)
btn = New Button
btn.Location = New Point(80, 70)
btn.Text = "Click me"
Me.Controls.Add(btn)
AddHandler btn.Click, AddressOf btn_click


End Sub


Private WithEvents btn As Button


Private Sub btn_click(ByVal sender As Object, ByVal e As EventArgs)
Dim textbox1 As New tTextbox(Me)
Me.Width = 400
Me.Height = 400
Me.Text = "Enter Text"
End Sub

End Class

Class buttons

End Class
Class tTextbox
Dim tbox As TextBox

Sub New(ByRef form1 As Form)
tBox = New TextBox
tBox.Size = New System.Drawing.Size(300, 300)
tBox.Location = New System.Drawing.Point(20, 20)
form1.Controls.Add(tBox)

End Sub



End Class

Monday, December 20, 2010

Tuesday 12/20/2010


Option Strict On
Imports System
Namespace InterfaceDemo
'define the interface
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property Status() As Integer
End Interface

'create a class that implements the IStorable interface

Public Class document
Implements IStorable
'oh cool it just added a bunch of things on its own

Private myStatus As Integer = 0

Public Sub New(ByVal s As String)
Console.WriteLine("Creaing document with: {0}", s)
End Sub 'new

Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub 'read

Public Property Status As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal value As Integer)
myStatus = value
End Set
End Property

Public Sub Write(ByVal obj As Object) Implements IStorable.Write
Console.WriteLine("Implementing the write method for IStorable.")
End Sub 'write
End Class

Class tester
Public Sub run()
Console.writeline("Implementing Tester.Run")


Dim doc As New document("Test Document")
doc.status = -1
doc.read()
console.writeline("Document status: {0} ", doc.status)
End Sub
End Class

End Namespace

Module Module1

Sub Main()
Dim t As New InterfaceDemo.tester()
t.run()
Console.WriteLine("Ran an instance of InterfaceDemo.tester()")
Console.ReadLine()
End Sub

End Module


Classes can implement any number of interfaces.


Option Strict On
Imports System
Namespace InterfaceDemo
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property status() As Integer
End Interface 'IStorable

Interface ICompressible
Sub Compress()
Sub Decompress()
End Interface 'ICompressible

'document implements both interfaces

Public Class Document
Implements ICompressible, IStorable

'hold the value for IStorable's Status property
Private myStatus As Integer = 0

'the document constructor
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with {0} ", s)
End Sub 'New



Public Sub Compress() Implements ICompressible.Compress
Console.WriteLine("Implementing Compress")
End Sub

Public Sub Decompress() Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub

Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the read method for IStorable")
End Sub 'Read

Public Property status As Integer Implements IStorable.status
Get
Return myStatus
End Get
Set(ByVal value As Integer)
myStatus = value
End Set
End Property

Public Sub Write(ByVal obj As Object) Implements IStorable.Write
Console.WriteLine("Implementing the Write Method for IStorable")
End Sub 'Write
End Class


End Namespace


And here is a variation I wrote:


Class Tester
Public Sub Run(ByVal textValue As String)
Dim doc As New Document(textValue)
doc.status = -1
doc.Read()
doc.Compress()
Console.WriteLine("Document Status: {0}", doc.status)
End Sub 'Run
End Class

End Namespace

Module Module1
Sub Main()
Dim textVal As String = ""
For i = 1 To 5
textVal = "Document #" & i.ToString
Dim t As New InterfaceDemo.Tester()
t.Run(textVal)
Next

Console.ReadLine()

End Sub 'main
End Module

Sunday, December 19, 2010

Sunday 12/19/2010


Public Class Form1
'This is a tutorial written by Michael Halvorston towards the end of his book, "Microsoft Visual Basic 2005 Step By Step
'The book has been a big help
'I originally tried to get this to work by using several classes and writing it all out in code - but it was a bit too much making my own event handlers
'Getting there, though
'I would really like to have Mr. Halvorston's book finished by Christmas but fat chance


'Variables
Dim GoingUp As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = New System.Drawing.Bitmap("C:\Users\Alfred\Documents\VBPractice\dog.JPG")
Timer1.Enabled = False
Timer1.Interval = 500
Button1.Text = "Going Up"
Button2.Text = "Going Down"
'note this is not how the original tutorial goes, in the original tutorial Michael Halvorston has us change the properties using the Visual Studio editor

End Sub

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GoingUp = True Then
'move picture box toward the top
If PictureBox1.Top > 10 Then
PictureBox1.Location = New Point _
(PictureBox1.Location.X - 10, _
PictureBox1.Location.Y - 10)
'didn't realize this when I typed it but this makes the picture move diagonally.
End If
Else
'move picture box toward the bottom
If PictureBox1.Top < (Me.Size.Height - 75) Then
PictureBox1.Location = New Point _
(PictureBox1.Location.X + 10, _
PictureBox1.Location.Y + 10)
End If
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GoingUp = True
Timer1.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
GoingUp = False
Timer1.Enabled = True
End Sub
End Class


Here is another tutorial from Michael Halvorston


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
Dim Employee As New Person
Dim DOB As Date

Employee.FirstName = TextBox1.Text
Employee.LastName = TextBox2.Text
DOB = DateTimePicker1.Value.Date

MsgBox(Employee.FirstName & " " & Employee.LastName _
& " Is " & Employee.age(DOB) & " years old.")

End Sub
End Class

Public Class Person
Private Name1 As String
Private Name2 As String

Public Property FirstName() As String
Get
Return (Name1)
End Get
Set(ByVal value As String)
Name1 = value
End Set
End Property

Public Property LastName() As String
Get
Return Name2
End Get
Set(ByVal value As String)
Name2 = value
End Set
End Property

'Create a method
Public Function age(ByVal Birthday As Date) As Integer
Return Int(Now.Subtract(Birthday).Days / 365.25)
End Function
End Class

Friday, December 17, 2010

Friday December 17 2010

Almost time for Christmas

Here is a variation I did, starting from a tutorial by Michael Halvorson


Public Class buttonClass
Dim buttonScheme As Button
Dim baseForm As Form

Sub New()
buttonScheme = New Button
With buttonScheme
.Size = New Size(200, 30)
.Enabled = True
.visible = True
.forecolor = Color.Blue
End With
End Sub

Sub buttonUp()
buttonScheme.text = "Go Up"
buttonScheme.Location = New System.Drawing.Point(600, 100)
buttonScheme.Name = "buttonUp"
End Sub

Sub add(ByRef form1 As Form)
baseForm = form1
baseForm.Controls.Add(buttonScheme)
End Sub

End Class

Public Class basicAnimation
Dim picturebox As New PictureBox
Dim buttonUp As buttonClass
Dim buttonDown As Object


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


With picturebox
.Image = New System.Drawing.Bitmap("C:\Users\Alfred\Documents\VBPractice\maoMao.jpg")
.Location = New System.Drawing.Point(90, 30)
.Visible = True
.Size = New System.Drawing.Size(300, 300)
.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
End With


buttonUp = New buttonClass
buttonUp.buttonUp()
buttonUp.add(Me)



Me.ClientSize = New System.Drawing.Size(1200, 900)
Me.Controls.Add(picturebox)

End Sub

Private Sub buttonUp_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonUp.click



End Sub

End Class

Thursday, December 16, 2010

Thursday 12/16/2010


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim newform As New formBuilder.formAssembler(Me)
End Sub
End Class

Namespace formBuilder
Public Class formAssembler
Private form1 As Form

Private textboxMain As New formTextBox
Private textboxEntry As New formTextBox



Sub New(ByRef formA As Form)
form1 = formA

'define the form itself
form1.ClientSize = New System.Drawing.Size(700, 700)

'Main textbox
textboxMain = New formTextBox()
textboxMain.mainTextBox()
textboxMain.addtextbox(form1)

'entry textbox
textboxEntry = New formTextBox()
textboxEntry.secondaryTextBox()
textboxEntry.addtextbox(form1)


End Sub

End Class

Public Class formTextBox
Private textBoxBuilder As TextBox

Sub New()
textBoxBuilder = New TextBox
End Sub

Sub secondaryTextBox()
textBoxBuilder.Name = "secondaryTextBox"
textBoxBuilder.Multiline = True
textBoxBuilder.Location = New System.Drawing.Point(150, 320)
End Sub

Sub mainTextBox()
textBoxBuilder.Name = "textboxMain"
textBoxBuilder.Multiline = True
textBoxBuilder.Location = New System.Drawing.Point(150, 100)
textBoxBuilder.Size = New System.Drawing.Size(200, 200)
End Sub

Sub addtextbox(ByRef formA As Form)
formA.Controls.Add(textBoxBuilder)
End Sub


End Class
End Namespace

Wednesday, December 15, 2010

Wednesday 12/15/10

I have started using a book written by Tim Patrick. Here is a tutorial by him. A list of his books are available at http://www.timaki.com/books.htm



Public Class Form1
Private WithEvents helloDecoder As HelloStuff.SayHello
' What does withEvents mean?
' "The withEvents keyword included in the statement is what lets the control know that someone wants to monitor event notifications.

Private Sub button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles button1.Click
'show the message
helloDecoder = New HelloStuff.SayHello("!iqwtB ,tqqjM", True)
helloDecoder.DecodeMessage(-5)
helloDecoder.ReportMessage()
End Sub

Private Sub HelloDecoder_MessageDecoded(ByVal decodedMessage As String) _
Handles helloDecoder.MessageDecoded
'show the decoded message
MsgBox(decodedMessage)
End Sub

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

End Sub


End Class

Namespace HelloStuff
Friend Class SayHello
Private secretMessage As String
Private reverseFlag As Boolean
Private decoded As Boolean

Public Event MessageDecoded(ByVal decodedMessage As String)

'The New constructor allows the user to create a new instance of SayHello
'with an initial message text, and a flag which tells
' the program whether the text should be reversed before display.


Public Sub New(ByVal codedMessage As String, ByVal reverseIt As Boolean)
'save the secret message. It will be decoded later.
secretMessage = codedMessage
reverseFlag = reverseIt
decoded = False
End Sub

'The decodeMessage method converts each letter of the
'encoded message by shifting each letter a number of spaces
'determined by the rotationFactor


Public Sub DecodeMessage(ByVal rotationFactor As Integer)
'The original message was coded. Rotate each character through the alphabet by the specified number
'of times to get the true value
Dim result As String
Dim oneChar As Char
Dim counter As Integer
Dim charValue As Integer

'Don't decode twice
If (decoded = True) Then Return

'Process each character
result = ""
For counter = 1 To Len(secretMessage)
oneChar = CChar(Mid(secretMessage, counter, 1))
If (Char.IsLower(oneChar) = True) Then
'rotate the character. convert it to an integer,
'rotate it, then convert it back
charValue = Asc(oneChar) - Asc("a"c)
charValue = (charValue + rotationFactor + 26) Mod 26
oneChar = Chr(charValue + Asc("a"c))
result &= oneChar
ElseIf (Char.IsUpper(oneChar) = True) Then
'rotate the character. convert it to an integer, rotate it, then convert it back.
charValue = Asc(oneChar) - Asc("A"c)
charValue = (charValue + rotationFactor + 26) Mod 26
oneChar = Chr(charValue + Asc("A"c))
result &= oneChar
Else
'no special translation on this character.
result &= oneChar
End If
Next counter

'store the converted value
secretMessage = result
decoded = True
End Sub

Public Sub ReportMessage()
'fire event for the caller
If (reverseFlag = True) Then
RaiseEvent MessageDecoded(StrReverse(secretMessage))
Else
RaiseEvent MessageDecoded(secretMessage)
End If
End Sub


End Class
End Namespace

Tuesday, December 14, 2010

Tuesday, December 14th

This is my first hello world program using javascript:



document.writeln('Hello, world!');



Here is another program, I can tell that working with JavaScript and posting it on this blog might be a problem, we may have to end up switching to Google Documents





The alert() method displays a simple alert dialog box whose content is whatever text is passed as a parameter to the method.



<html>
<head>
<title>My First Script</title>
<style type="text/css">
.highlight {font-weight:bold}
</style>
</head>

<body>
<h1>Let's Script</h1>
<hr>
<script type="text/javascript">
document.write("This browser is version " + navigator.appVersion);
document.write(" of " + navigator.appName + ".");
</script>
</body>
</html>


Here is another JavaScript tutorial using deferred scripts In this tutorial, a javaScript function is called upon to loan right after the page loads.




<html>

<head>
<title>An onload script</title>

<script type="text/javascript">
function done() {
alert("The page has finished loading.");
}
</script>

</head>

<body onload="done()">
Here is some body text.
</body>

</html>



Here is another tutorial on running a script from User Action



<html>
<head>
<title>An onclick script</title>
<script type="text/javascript">
function alertUser() {
alert("Ouch!");
}
</script>
</head>

<body>
Here is some body text
<form>
<Input type="text" name="entry">
<input type="button" name="oneButton" value="Press Me!" onclick="alertUser()">
</form>
</body>
</html>



OK this is going to be my last JavaScript Tutorial for today, and for now I am going to go back to working on vb.net



<html>
<head>
<title>Window Opener and Closer</title>
<script type="text/javascript">
var newWindow
function makeNewWindow() {
newWindow = window.open("","","height=300,width=300");
}
function closeNewWindow() {
if (newWindow) {
newWindow.close();
newWindow = null;
}
}
</script>
</head>

<body>
<form>
<input type="button" value="Create New Window" onclick="makeNewWindow()">
<input type="button" value="Close the Window" onclick="closeNewWindow()">
</form>
</body>
</html>



Here is a tutorial on using parameter arrays




Module Module1

Sub Main()
AnyNumberArgument()
AnyNumberArgument(3, 4)
AnyNumberArgument(8, 2, 3, 7, 10)
Console.ReadLine()
End Sub

'use paramArray to create a variable-length parameter list
Sub AnyNumberArgument(ByVal ParamArray array1 As Integer())
Dim i, total As Integer
total = 0

If array1.Length = 0 Then
Console.WriteLine("Received 0 arguments.")
Else
Console.Write("The total of ")
For i = 0 To array1.GetUpperBound(0)
Console.Write(array1(i) & " ")
total += array1(i)
Next
Console.WriteLine("is {0}.", total)

End If
End Sub
End Module


Here is another tutorial on Parameter Arrays



Sub displayVals(ByVal ParamArray intVals() As Integer)
Dim i As Integer
For Each i In intVals
Console.WriteLine("Display Vals {0} ", i)
Next i

End Sub

Sub main()
Dim a As Integer = 5
Dim b As Integer = 7
Dim c As Integer = 9
Console.WriteLine("Calling with three Integers")
displayVals(a, b, c)

Console.WriteLine("Calling with four integers")
displayVals(5, 6, 7, 8)

Console.WriteLine("Calling with an array of four integers")
Dim explicitArray() As Integer = {5, 6, 7, 8}
displayVals(explicitArray)

Console.ReadLine()

End Sub