Thursday, December 9, 2010

Thursday, Decemeber 12 2010

Here is the code from a tutorial by Evangelos Petroutsos that I have been working on:




Public Class frmTextPad

Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
If TextBox1.SelectionLength > 0 Then
Clipboard.SetText(TextBox1.SelectedText)
End If
End Sub

Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
Clipboard.SetText(TextBox1.SelectedText)
TextBox1.SelectedText = ""
End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
If Clipboard.ContainsText Then
TextBox1.SelectedText = Clipboard.GetText
End If
End Sub

Private Sub NumberLinesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumberLinesToolStripMenuItem.Click
Dim iLine As Integer
'notice the use of StringBuilder
Dim newText As New System.Text.StringBuilder()
For iLine = 0 To TextBox1.Lines.Length - 1
'adds a number before each line, then tabs and then the text for that line
newText.Append((iLine + 1).ToString & vbTab & TextBox1.Lines(iLine) & vbCrLf)
Next
TextBox1.SelectAll()
Clipboard.SetText(newText.ToString)
TextBox1.Paste()
End Sub

Private Sub FindReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindReplaceToolStripMenuItem.Click
findReplace.ShowDialog()
End Sub

Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
If UndoToolStripMenuItem.Text = "Undo" Then
If TextBox1.CanUndo Then
TextBox1.Undo()
UndoToolStripMenuItem.Text = "Redo"
End If
Else
If TextBox1.CanUndo Then
TextBox1.Undo()
EditToolStripMenuItem.Text = "Undo"
End If
End If
End Sub
End Class


Here is another tutorial by Jesse Liberty



Imports System
Public Class window
'constructor takes two integers to fix location on the console
Public Sub New(ByVal top As Integer, ByVal left As Integer)
Me.top = top
Me.left = left
End Sub

'simulates drawing the window
Public Overridable Sub drawWindow()
Console.WriteLine("Window: drawing window at {0}, {1}", top, Left)
End Sub

'these members are protected and thus invisible to derived class methods
'we'll examine this later in the chapter

Protected top As Integer
Protected left As Integer
End Class


'ListBox derives from Window
Public Class listBox
Inherits window

'the keyword MyBase indentifies the base class for the current object
'constructor adds a parameter
Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal contents As String)
MyBase.New(top, left) 'call base constructor
listBoxContents = contents
End Sub

'an overrident version(note keyword) because in the derived method we change the behavior
Public Overrides Sub drawWindow()
MyBase.drawWindow() 'nvokes the base method
Console.WriteLine( _
"Writing string to the listbox: {0} ", listBoxContents)
End Sub 'DrawWindow

Private listBoxContents As String 'new member variable

End Class 'listbox

Public Class Button
Inherits window

Public Sub New(ByVal top As Integer, ByVal left As Integer)
MyBase.New(top, left)
End Sub 'new

'an overridden version (again, note keyword) because in the derived method we change the behavior

Public Overrides Sub drawWindow()
Console.WriteLine("Drawing a button at {0}, {1}" & ControlChars.Lf, top, left)
End Sub 'drawWindow

End Class 'button

Module Module1
'The following tutorial is by Jesse Liberty.
'It deal with polymorphism and overriding
'It's important to note that VB.net differs from Java or C++ in that an "overridable function is always considered to the be the root of dispatch"
'This tutorial also looks at object arrays

Sub Main()
Dim win As New window(1, 2)
Dim lb As New listBox(3, 4, "Stand Alone list box")
Dim b As New Button(5, 6)
win.drawWindow()
lb.drawWindow()
b.drawWindow()
Dim winArray(3) As window
winArray(0) = New window(1, 2)
winArray(1) = New listBox(3, 4, "List box in array")
winArray(2) = New Button(5, 6)

Dim i As Integer
For i = 0 To 2
winArray(i).drawWindow()
Next i

Console.ReadLine()
End Sub 'Main

End Module


Here is a little mini form builder class I have been working on





Public Class formBuilder

Dim formNew As Form

Dim buttonButton(10) As Button
Dim buttonIndex As Integer = 0
Dim buttonArray() As Button


Sub New()
formNew = New Form
MessageBox.Show("We created a new form")
End Sub

Sub showForm()
formNew.Show()
End Sub

Sub addButton(ByVal x, ByVal y)

If buttonIndex <= 10 Then
buttonButton(buttonIndex) = New Button
buttonButton(buttonIndex).Top = y
10: buttonButton(buttonIndex).Left = x
buttonButton(buttonIndex).Name = "NewButton" & buttonIndex
MessageBox.Show("We created a new button named " & buttonIndex)
buttonButton(buttonIndex).Text = "Button" & buttonIndex

formNew.Controls.Add(buttonButton(buttonIndex))
buttonIndex = buttonIndex + 1
Else
MessageBox.Show("You created enough buttons already")
Exit Sub
End If
End Sub

Sub bringForward()
formNew.TopMost = True
End Sub
End Class

Public Class Form1
Dim formRoll As formBuilder
Dim x As Integer = 0
Dim y As Integer = 0


Private Sub btnCreateForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateForm.Click
formRoll = New formBuilder
btnShowForm.Enabled = True
btnBringFormForward.Enabled = True
btnAddButton.Enabled = True
btnCreateForm.Enabled = False
End Sub

Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click
formRoll.showForm()
End Sub

Private Sub btnAddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddButton.Click

y = InputBox("Enter the top coordinate")
x = InputBox("enter the left coordinate")
formRoll.addButton(x, y)

End Sub

Private Sub btnBringFormForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBringFormForward.Click
formRoll.bringForward()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnShowForm.Enabled = False
btnBringFormForward.Enabled = False
btnAddButton.Enabled = False
End Sub
End Class

No comments: