0% found this document useful (0 votes)
164 views24 pages

Practice Vbdnet 09

This document summarizes the key properties and methods of the TextBox control in Visual Basic .NET and provides code examples for a basic text editor application called TextPad that utilizes many of the TextBox features. The TextPad application allows for opening, saving, finding/replacing text, formatting text, and includes undo/redo functionality implemented using the TextBox control.

Uploaded by

ysorath5221
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views24 pages

Practice Vbdnet 09

This document summarizes the key properties and methods of the TextBox control in Visual Basic .NET and provides code examples for a basic text editor application called TextPad that utilizes many of the TextBox features. The TextPad application allows for opening, saving, finding/replacing text, formatting text, and includes undo/redo functionality implemented using the TextBox control.

Uploaded by

ysorath5221
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PRACTICE 09

The TextBox Control


Visual Basic.NET
by Has Bunton

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

ƒ Design the menu as follows.

by Has Bunton 6
TextPad Project (3) – The Design

by Has Bunton 7
TextPad Project (4) – The Design

ƒ Place Open, Save, Font, and Color dialog


boxes for the main form

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

78. Private Sub FileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


FileNew.Click
79. If Editor.Modified Then
80. Dim reply As Integer
81. reply = MsgBox("File hasn't been saved. Erase anyway?", MsgBoxStyle.YesNo, "New Text
Requested")
82. If reply = MsgBoxResult.Yes Then
83. Editor.Clear()
84. End If
85. Else
86. Editor.Clear()
87. End If
88. 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

95. Private Sub EditSelect_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles EditSelect.Click
96. Editor.SelectAll()
97. End Sub

98. Private Sub CustomizePageColor_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles CustomizePageColor.Click
99. ColorDialog1.AllowFullOpen = True
100. ColorDialog1.Color = Editor.BackColor
101. ColorDialog1.ShowDialog()
102. Editor.BackColor = ColorDialog1.Color
103. 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

109. Private Sub ProcessNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles ProcessNumber.Click
110. Dim iLine As Integer
111. Dim newText As New System.Text.StringBuilder()
112. For iLine = 0 To Editor.Lines.Length - 1
113. newText.Append((iLine + 1).ToString & vbTab & Editor.Lines(iLine) & vbCrLf)
114. Next
115. ' The following operation, which moves the text to the Clipboard
116. ' and back to the TextBox control, marks the current operation as an
117. ' undoable operation. The Undo command will remove the numbers in front
118. ' of the lines, as long as you don't insert or delete any text after the
119. ' Number Lines operation.
120. ' You can repeat these statements after any operation you want to reverse with
121. ' the Edit > Undo command
122. Editor.SelectAll()
123. Clipboard.SetDataObject(newText.ToString())
124. Editor.Paste()
125. End Sub
by Has Bunton 16
TextPad Project (13) - Coding
126. Private Sub EditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
EditPaste.Click
127. If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
128. Editor.SelectedText = Clipboard.GetDataObject.GetData(DataFormats.Text).ToString
129. End If
130. End Sub

131. Private Sub EditFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


EditFind.Click
132. frm.searchWord.Text = Editor.SelectedText
133. frm.Show()
134. 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

138. Private Sub ProcessUpper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles ProcessUpper.Click
139. Editor.SelectedText = Editor.SelectedText.ToUpper
140. 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

144. Private Sub EditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


EditCut.Click
145. Clipboard.SetDataObject(Editor.SelectedText)
146. Editor.SelectedText = ""
147. End Sub

148. Private Sub FileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


FileExit.Click
149. If Editor.Modified Then
150. Dim reply As MsgBoxResult
151. reply = MsgBox("The current file has been edited. Quit anyway (Y/N)?", MsgBoxStyle.YesNo)
152. If reply = MsgBoxResult.Yes Then
153. Application.Exit()
154. End If
155. End If
156. 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

160. Private Sub TXTPADForm_Closing(ByVal sender As Object, ByVal e As


System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
161. If Editor.Modified Then
162. Dim reply As MsgBoxResult
163. reply = MsgBox("The current file has been edited. Quit anyway
(Y/N)?", MsgBoxStyle.YesNo)
164. If reply = MsgBoxResult.No Then
165. e.Cancel = True
166. End If
167. End If
168. 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

12. If chkCase.Checked = True Then


13. srchMode = CompareMethod.Binary
14. Else
15. srchMode = CompareMethod.Text
16. End If
17. selStart = InStr(TXTPADForm.txtBox.Text, searchWord.Text, srchMode)
18. If selStart = 0 Then
19. MsgBox("Can't find word")
20. Exit Sub
21. End If
22. TXTPADForm.txtBox.Select(selStart - 1, searchWord.Text.Length)
23. bttnFindNext.Enabled = True
24. bttnReplace.Enabled = True
25. bttnReplaceAll.Enabled = True
26. TXTPADForm.txtBox.ScrollToCaret()
27. End Sub

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

43. End Sub

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

47. If chkCase.Checked = True Then


48. srchMode = Microsoft.VisualBasic.CompareMethod.Binary
49. Else
50. srchMode = Microsoft.VisualBasic.CompareMethod.Text
51. End If
52. selStart = InStr(TXTPADForm.txtBox.SelectionStart + 2,
TXTPADForm.txtBox.Text, searchWord.Text, srchMode)
53. If selStart = 0 Then
54. MsgBox("No more matches")
55. Exit Sub
56. End If
57. TXTPADForm.txtBox.Select(selStart - 1, searchWord.Text.Length)
58. TXTPADForm.txtBox.ScrollToCaret()
59. End Sub

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

63. Private Sub bttnReplace_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles bttnReplace.Click
64. If TXTPADForm.txtBox.SelectedText <> "" Then
65. TXTPADForm.txtBox.SelectedText = replaceWord.Text
66. End If
67. bttnFindNext_Click(sender, e)
68. End Sub

69. Private Sub replaceWord_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles replaceWord.TextChanged
70. If replaceWord.Text.Length > 0 Then
71. bttnReplace.Enabled = True
72. bttnReplaceAll.Enabled = True
73. Else
74. bttnReplace.Enabled = False
75. bttnReplaceAll.Enabled = False
76. End If
77. End Sub

End!
by Has Bunton 24

You might also like