Practice Vbdnet 09
Practice Vbdnet 09
1
TextBox Control Properties - Basic
MultiLine
ScrollBars
WordWrap (no hard breaks/returns at
the end of each line)
AcceptsReturn/AcceptsTab
MaxLength: Default = 32,767*. Set to
0 will give the max. length of 2 GB.
by Has Bunton 2
TextBox Control Properties -
Manipulation
Text
Example: Locating a string in a TextBox
Dim startIndex = -1
startIndex = TextBox1.Text.IndexOf(“basic”, startIndex + 1)
While startIndex > 0
Console.WriteLine(“String found at “ & startIndex)
startIndex = TextBox1.Text.IndexOf(“basic”, startIndex + 1)
End While
ReadOnly, Locked
Lines: to read lines of text
PasswordChar
by Has Bunton 3
TextBox Control Properties – Text
Selection
Selected Text
SelectionStart/SelectionLength*
Example: Text Selection
Dim seekString As String
Dim textStart As Integer
seekString = “Visual”
textStart = TextBox1.Text.IndexOf(seekString)
If textStart > 0 Then
TextBox1.SelectionStart = textStart – 1
TextBox1.SelectionLength = seekString.Length
End If
TextBox1.Focus()
HideSelection
CanUndo: enable the Undo method to undo and toggle
the last edit operation
by Has Bunton 4
TextPad Project (1) – The Analysis
and Design
It is a basic text editor application that
demonstrates most of the TextBox control’s
properties and methods.
Place a
TextBox on
the form and
set its Dock
and Multiline
to Fill and
True,
respectively
by Has Bunton 5
TextPad Project (2) –The Design
Name the main form to TextPadForm, and
the Find and Replace Dialog to FindForm
by Has Bunton 6
TextPad Project (3) – The Design
by Has Bunton 7
TextPad Project (4) – The Design
by Has Bunton 8
TextPad Project (5) - Coding
1. Dim SaveFileName As String
2. Public Shared txtBox As TextBox
3. Dim frm As New FindForm()
4. Private Sub EditUndo_Click(ByVal sender As _ System.Object,
ByVal e As System.EventArgs) Handles _ EditUndo.Click
5. If EditUndo.Text = "Undo" Then
6. Editor.Undo() ‘Editor is the name of main TextBox.
7. EditUndo.Text = "Redo"
8. Else
9. Editor.Undo()
10. EditUndo.Text = "Undo"
11. End If
12. End Sub
13.
by Has Bunton 9
TextPad Project (6) - Coding
14. Private Sub FileSaveAs_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles FileSaveAs.Click
15. SaveFileDialog1.DefaultExt = "TXT"
16. SaveFileDialog1.Filter = "Text Files|*.TXT|HTML Files|*.HTM|All Files|*.*"
17. SaveFileDialog1.FilterIndex = 1
18. SaveFileDialog1.ShowDialog()
19. If SaveFileDialog1.FileName = "" Then Exit Sub
20. Dim tWriter As System.IO.StreamWriter
21. tWriter = New System.IO.StreamWriter(SaveFileDialog1.FileName)
22. tWriter.Write(Editor.Text)
23. tWriter.Close()
24. tWriter = Nothing
25. Editor.SelectionStart = 0
26. Editor.SelectionLength = 0
27. SaveFileName = SaveFileDialog1.FileName
28. Editor.Modified = False
29. End Sub
by Has Bunton 10
TextPad Project (7) - Coding
30. Private Sub FileSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FileSave.Click
31. If SaveFileName = "" Then
32. FileSaveAs_Click(sender, e)
33. Exit Sub
34. End If
35. Dim TWriter As System.IO.StreamWriter
36. TWriter = New System.IO.StreamWriter(SaveFileName)
37. TWriter.Write(Editor.Text)
38. TWriter.Close()
39. TWriter = Nothing
40. Editor.SelectionStart = 0
41. Editor.SelectionLength = 0
42. Editor.Modified = False
43. End Sub
by Has Bunton 11
TextPad Project (8) - Coding
44. Private Sub EditCopy_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
Handles EditCopy.Click
45. Clipboard.SetDataObject(Editor.SelectedText)
46. End Sub
by Has Bunton 12
TextPad Project (9) - Coding
47. Private Sub FileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
FileOpen.Click
48. If Editor.Modified Then
49. Dim reply As MsgBoxResult
50. reply = MsgBox("File hasn't been saved. Discard changes?", MsgBoxStyle.YesNo, "New Text
Requested")
51. If reply <> MsgBoxResult.Yes Then
52. Exit Sub
53. End If
54. End If
55. OpenFileDialog1.DefaultExt = "TXT"
56. OpenFileDialog1.Filter = "Text Files|*.TXT|HTML Files|*.HTM|All Files|*.*"
57. OpenFileDialog1.FilterIndex = 1
58. OpenFileDialog1.ShowDialog()
59. If OpenFileDialog1.FileName = "" Then Exit Sub
60. Dim TReader As System.IO.StreamReader
61. TReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
62. Editor.Text = TReader.ReadToEnd
63. TReader.Close()
64. TReader = Nothing
65. Editor.SelectionStart = 0
66. Editor.SelectionLength = 0
67. SaveFileName = SaveFileDialog1.FileName
68. End Sub
by Has Bunton 13
TextPad Project (10) - Coding
69. Private Sub EditWrap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
EditWrap.Click
70. If EditWrap.Checked Then
71. Editor.WordWrap = False
72. EditWrap.Checked = False
73. Else
74. Editor.WordWrap = True
75. EditWrap.Checked = True
76. End If
77. End Sub
by Has Bunton 14
TextPad Project (11) - Coding
89. Private Sub CustomizeTextColor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CustomizeTextColor.Click
90. ColorDialog1.AllowFullOpen = True
91. ColorDialog1.Color = Editor.ForeColor
92. ColorDialog1.ShowDialog()
93. Editor.ForeColor = ColorDialog1.Color
94. End Sub
by Has Bunton 15
TextPad Project (12) - Coding
104. Private Sub CustomizeFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles CustomizeFont.Click
105. FontDialog1.FontMustExist = True
106. FontDialog1.ShowDialog()
107. Editor.Font = FontDialog1.Font
108. End Sub
135. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
136. txtBox = Editor
137. End Sub
by Has Bunton 17
TextPad Project (14) - Coding
141. Private Sub ProcessLower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles ProcessLower.Click
142. Editor.SelectedText = Editor.SelectedText.ToLower
143. End Sub
by Has Bunton 18
TextPad Project (15) - Coding
157. Private Sub Editor_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Editor.TextChanged
158. EditUndo.Text = "Undo"
159. End Sub
by Has Bunton 19
TextPad Project (16) – Coding The
Find and Replace Form
1. Private Sub bttnReplaceAll_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
bttnReplaceAll.Click
2. Dim curPos, curSel As Integer
3. curPos = TXTPADForm.txtBox.SelectionStart
4. curSel = TXTPADForm.txtBox.SelectionLength
5. TXTPADForm.txtBox.Text =
Replace(TXTPADForm.txtBox.Text,
Trim(searchWord.Text), Trim(replaceWord.Text))
6. TXTPADForm.txtBox.SelectionStart = curPos
7. TXTPADForm.txtBox.SelectionLength = curSel
8. End Sub
by Has Bunton 20
TextPad Project (17) – Coding The
Find and Replace Form
9. Private Sub bttnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bttnFind.Click
10. Dim selStart As Integer
11. Dim srchMode As Microsoft.VisualBasic.CompareMethod
by Has Bunton 21
TextPad Project (18) – Coding The
Find and Replace Form
28. Private Sub searchWord_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles searchWord.TextChanged
29. If searchWord.Text.Length > 0 Then
30. bttnFind.Enabled = True
31. bttnFindNext.Enabled = True
32. Else
33. bttnFind.Enabled = False
34. bttnFindNext.Enabled = False
35. End If
36. If replaceWord.Text.Length > 0 Then
37. bttnReplace.Enabled = True
38. bttnReplaceAll.Enabled = True
39. Else
40. bttnReplace.Enabled = False
41. bttnReplaceAll.Enabled = False
42. End If
by Has Bunton 22
TextPad Project (19) – Coding The
Find and Replace Form
44. Private Sub bttnFindNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bttnFindNext.Click
45. Dim selStart As Integer
46. Dim srchMode As Microsoft.VisualBasic.CompareMethod
by Has Bunton 23
TextPad Project (20) – Coding The
Find and Replace Form
60. Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Activated
61. searchWord_TextChanged(sender, e)
62. End Sub
End!
by Has Bunton 24