VB.Net Tutorial/GUI/TextBox
Содержание
- 1 Change TextBox font by RadioButton and CheckBox
- 2 Comprehensive Demo for TextBox
- 3 Convert input value in a TextField
- 4 Copy selection text from a TextBox and paste to another
- 5 Create PasswordField by setting the TextBox.PasswordChar
- 6 Disable TextBox Context Menu
- 7 Display text file in a TextBox
- 8 Get input from TextBox and Set to Label
- 9 Get selected text in TextBox
- 10 Inherit TextBox to create custom control based on TextBox
- 11 Multiline Text Boxes
- 12 Numeric TextBox
- 13 Save text in TextBox to a file
- 14 TextBox Text Changed Event
Change TextBox font by RadioButton and CheckBox
<source lang="vbnet">Imports System.Windows.Forms public class RadioButtonControlFont
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Public mysize As Integer Public mybold As Boolean Public myItalic As Boolean Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) CheckBox1.Checked = 1 End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged myItalic = Not myItalic TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Regular) If myItalic And mybold Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Italic) ElseIf myItalic Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Italic) ElseIf mybold Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Bold) End If End Sub Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mybold = False myItalic = False mysize = 25 End Sub Private Sub RadioButton1_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged mysize = 36 TextBox1.Font = New System.Drawing.Font("", mysize) End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged mysize = 25 TextBox1.Font = New System.Drawing.Font("", mysize) End Sub Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged mybold = Not mybold TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Regular) If myItalic And mybold Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Bold) ElseIf myItalic Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Italic) ElseIf mybold Then TextBox1.Font = New System.Drawing.Font("", mysize, Drawing.FontStyle.Bold) End If End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.CheckBox1 = New System.Windows.Forms.CheckBox Me.CheckBox2 = New System.Windows.Forms.CheckBox Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.RadioButton1 = New System.Windows.Forms.RadioButton Me.RadioButton2 = New System.Windows.Forms.RadioButton Me.TextBox1 = New System.Windows.Forms.TextBox Me.GroupBox1.SuspendLayout() Me.SuspendLayout() " "CheckBox1 " Me.CheckBox1.AutoSize = True Me.CheckBox1.Location = New System.Drawing.Point(230, 34) Me.CheckBox1.Name = "CheckBox1" Me.CheckBox1.Size = New System.Drawing.Size(48, 16) Me.CheckBox1.TabIndex = 1 Me.CheckBox1.Text = "Italic" Me.CheckBox1.UseVisualStyleBackColor = True " "CheckBox2 " Me.CheckBox2.AutoSize = True Me.CheckBox2.Location = New System.Drawing.Point(230, 56) Me.CheckBox2.Name = "CheckBox2" Me.CheckBox2.Size = New System.Drawing.Size(48, 16) Me.CheckBox2.TabIndex = 2 Me.CheckBox2.Text = "Bold" Me.CheckBox2.UseVisualStyleBackColor = True " "GroupBox1 " Me.GroupBox1.Controls.Add(Me.RadioButton2) Me.GroupBox1.Controls.Add(Me.RadioButton1) Me.GroupBox1.Location = New System.Drawing.Point(224, 85) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(132, 77) Me.GroupBox1.TabIndex = 3 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Font size" " "RadioButton1 " Me.RadioButton1.AutoSize = True Me.RadioButton1.Location = New System.Drawing.Point(6, 20) Me.RadioButton1.Name = "RadioButton1" Me.RadioButton1.Size = New System.Drawing.Size(59, 16) Me.RadioButton1.TabIndex = 1 Me.RadioButton1.TabStop = True Me.RadioButton1.Text = "Larger font" Me.RadioButton1.UseVisualStyleBackColor = True " "RadioButton2 " Me.RadioButton2.AutoSize = True Me.RadioButton2.Location = New System.Drawing.Point(6, 42) Me.RadioButton2.Name = "RadioButton2" Me.RadioButton2.Size = New System.Drawing.Size(59, 16) Me.RadioButton2.TabIndex = 2 Me.RadioButton2.TabStop = True Me.RadioButton2.Text = "Smaller font" Me.RadioButton2.UseVisualStyleBackColor = True " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(12, 12) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(195, 150) Me.TextBox1.TabIndex = 4 Me.TextBox1.Text = "www.vbex.ru" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(368, 174) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.GroupBox1) Me.Controls.Add(Me.CheckBox2) Me.Controls.Add(Me.CheckBox1) Me.Name = "Form1" Me.Text = "Form1" Me.GroupBox1.ResumeLayout(False) Me.GroupBox1.PerformLayout() Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class</source>
Comprehensive Demo for TextBox
<source lang="vbnet">"Programming .Net Windows Applications [ILLUSTRATED] (Paperback) "by Jesse Liberty (Author), Dan Hurwitz (Author) "# Publisher: O"Reilly Media, Inc.; 1 edition (October 28, 2003) "# Language: English "# ISBN-10: 0596003218 "# ISBN-13: 978-0596003210 imports System imports System.Drawing imports System.Windows.Forms imports System.Text namespace ProgrammingWinApps
public class TextBoxes : inherits Form dim yDelta as integer dim yPos as integer = 20 dim txtSingle as TextBox dim txtMulti as TextBox dim txtDisplay as TextBox dim btn as Button dim txtBoxes(1) as TextBox public sub New() Text = "TextBoxes" Size = new Size(450,375) dim lblSingle as new Label() lblSingle.Parent = me lblSingle.Text = "Single Line TextBox:" lblSingle.Location = new Point(10,yPos) lblSingle.Size = new Size(150,20) lblSingle.TextAlign = ContentAlignment.MiddleRight yDelta = lblSingle.Height + 10 txtSingle = new TextBox() txtSingle.Parent = me txtSingle.Text = "Single Line" txtSingle.Size = new Size(200, txtSingle.PreferredHeight) txtSingle.Location = new Point(lblSingle.Left + _ lblSingle.Size.Width, yPos) txtSingle.Multiline = false txtSingle.BorderStyle = BorderStyle.Fixed3D dim lblMulti as new Label() lblMulti.Parent = me lblMulti.Text = "Multi Line TextBox:" lblMulti.Location = new Point(10, yPos + yDelta) lblMulti.Size = new Size(150,20) lblMulti.TextAlign = ContentAlignment.MiddleRight txtMulti = new TextBox() txtMulti.Parent = me txtMulti.Text = "Multi Line" txtMulti.Size = new Size(200,100) txtMulti.Location = new Point(lblMulti.Left + _ lblMulti.Size.Width, yPos + yDelta) txtMulti.AcceptsTab = true txtMulti.Multiline = true txtMulti.BorderStyle = BorderStyle.Fixed3D txtMulti.ScrollBars = ScrollBars.Vertical btn = new Button() btn.Parent = me btn.Text = "Show MultiLines" btn.Location = new Point(lblMulti.Left + _ lblMulti.Size.Width, yPos + (5 * yDelta)) AddHandler btn.Click, AddressOf btn_Click dim xSize as integer = CType((Font.Height * .75) * _ btn.Text.Length, integer) dim ySize as integer = Font.Height + 10 btn.Size = new Size(xSize, ySize) txtDisplay = new TextBox() txtDisplay.Parent = me txtDisplay.Text = "" txtDisplay.Size = new Size(200,100) txtDisplay.Location = new Point(lblMulti.Left + _ lblMulti.Size.Width, yPos + (6 * yDelta)) txtDisplay.Multiline = true txtDisplay.BorderStyle = BorderStyle.FixedSingle txtDisplay.BackColor = Color.LightGray txtDisplay.ScrollBars = ScrollBars.Vertical txtDisplay.ReadOnly = true " Fill the array of TextBoxes txtBoxes(0) = txtSingle txtBoxes(1) = txtMulti " Menus " Edit menu items dim mnuDash1 as new MenuItem("-") dim mnuDash2 as new MenuItem("-") dim mnuUndo as new MenuItem("&Undo", _ new EventHandler(AddressOf mnuUndo_Click), _ Shortcut.CtrlZ) dim mnuCut as new MenuItem("Cu&t", _ new EventHandler(AddressOf mnuCut_Click), _ Shortcut.CtrlX) dim mnuCopy as new MenuItem("&Copy", _ new EventHandler(AddressOf mnuCopy_Click), _ Shortcut.CtrlC) dim mnuPaste as new MenuItem("&Paste", _ new EventHandler(AddressOf mnuPaste_Click), _ Shortcut.CtrlV) dim mnuDelete as new MenuItem("&Delete", _ new EventHandler(AddressOf mnuDelete_Click)) dim mnuSelectAll as new MenuItem("Select &All", _ new EventHandler(AddressOf mnuSelectAll_Click), _ Shortcut.CtrlA) dim mnuSelect5 as new MenuItem("Select First &5", _ new EventHandler(AddressOf mnuSelect5_Click), _ Shortcut.Ctrl5) dim mnuClear as new MenuItem("Clea&r", _ new EventHandler(AddressOf mnuClear_Click)) dim mnuEdit as new MenuItem("&Edit", _ new MenuItem() {mnuUndo, mnuDash1, _ mnuCut, mnuCopy, mnuPaste, mnuDelete, mnuDash2, _ mnuSelectAll, mnuSelect5, mnuClear}) " View Menu items dim mnuScrollToCaret as new MenuItem("&Scroll to Caret", _ new EventHandler(AddressOf mnuScrollToCaret_Click)) dim mnuView as new MenuItem("&View", _ new MenuItem() {mnuScrollToCaret}) " Main menu Menu = new MainMenu(new MenuItem() {mnuEdit, mnuView})
end sub " close for constructor public shared sub Main() Application.Run(new TextBoxes()) end sub private sub mnuUndo_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.CanUndo = true then txt.Undo() txt.ClearUndo() end if end if next end sub private sub mnuCut_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.SelectedText <> "" then txt.Cut() end if end if next end sub private sub mnuCopy_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.SelectionLength > 0 then txt.Copy() end if end if next end sub private sub mnuPaste_Click(ByVal sender As Object, _ ByVal e As EventArgs) if Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = true then dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.SelectionLength > 0 then if MessageBox.Show( _ "Do you want to overwrite the currently selected text?", _ "Cut & Paste", MessageBoxButtons.YesNo) = _ DialogResult.No then txt.SelectionStart = txt.SelectionStart + _ txt.SelectionLength end if end if txt.Paste() end if next end if end sub private sub mnuDelete_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.SelectionLength > 0 then txt.SelectedText = "" end if end if next end sub private sub mnuClear_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) txt.Clear() end if next end sub private sub mnuSelect5_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) if txt.Text.Length >= 5 then txt.Select(0,5) else txt.Select(0,txt.Text.Length) end if end if next end sub private sub mnuSelectAll_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) txt.SelectAll() end if next end sub private sub mnuScrollToCaret_Click(ByVal sender As Object, _ ByVal e As EventArgs) dim i as integer for i = 0 to txtBoxes.Length - 1 if txtBoxes(i).Focused then dim txt as TextBox = CType(txtBoxes(i), TextBox) txt.ScrollToCaret() end if next end sub private sub btn_Click(ByVal sender as object, _ ByVal e as EventArgs) " Create a string array to hold the Lines property. dim arLines(txtMulti.Lines.Length - 1) as string arLines = txtMulti.Lines " Use stringBuilder for efficiency. dim str as string = "Line" + vbTab + "String" + vbCrLf dim sb as new StringBuilder() sb.Append(str) " Iterate through the array & display each line. dim i as integer for i = 0 to arLines.Length - 1 str = i.ToString() + "." + vbTab + arLines(i) + vbCrLf sb.Append(str) next txtDisplay.Text = sb.ToString() end sub end class
end namespace</source>
Convert input value in a TextField
<source lang="vbnet">Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class ConvertForm
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SourceFahrenheit = New System.Windows.Forms.RadioButton Me.SourceCelsius = New System.Windows.Forms.RadioButton Me.SourceKelvin = New System.Windows.Forms.RadioButton Me.ValueFahrenheit = New System.Windows.Forms.TextBox Me.ValueCelsius = New System.Windows.Forms.TextBox Me.ValueKelvin = New System.Windows.Forms.TextBox Me.ConvertTemperature = New System.Windows.Forms.Button Me.SuspendLayout() " "SourceFahrenheit " Me.SourceFahrenheit.AutoSize = True Me.SourceFahrenheit.Checked = True Me.SourceFahrenheit.Location = New System.Drawing.Point(16, 24) Me.SourceFahrenheit.Name = "SourceFahrenheit" Me.SourceFahrenheit.Size = New System.Drawing.Size(75, 17) Me.SourceFahrenheit.TabIndex = 0 Me.SourceFahrenheit.TabStop = True Me.SourceFahrenheit.Text = "&Fahrenheit" Me.SourceFahrenheit.UseVisualStyleBackColor = True " "SourceCelsius " Me.SourceCelsius.AutoSize = True Me.SourceCelsius.Location = New System.Drawing.Point(16, 56) Me.SourceCelsius.Name = "SourceCelsius" Me.SourceCelsius.Size = New System.Drawing.Size(58, 17) Me.SourceCelsius.TabIndex = 2 Me.SourceCelsius.Text = "&Celsius" Me.SourceCelsius.UseVisualStyleBackColor = True " "SourceKelvin " Me.SourceKelvin.AutoSize = True Me.SourceKelvin.Location = New System.Drawing.Point(16, 88) Me.SourceKelvin.Name = "SourceKelvin" Me.SourceKelvin.Size = New System.Drawing.Size(53, 17) Me.SourceKelvin.TabIndex = 4 Me.SourceKelvin.Text = "&kelvin" Me.SourceKelvin.UseVisualStyleBackColor = True " "ValueFahrenheit " Me.ValueFahrenheit.Location = New System.Drawing.Point(120, 24) Me.ValueFahrenheit.Name = "ValueFahrenheit" Me.ValueFahrenheit.Size = New System.Drawing.Size(100, 20) Me.ValueFahrenheit.TabIndex = 1 " "ValueCelsius " Me.ValueCelsius.Location = New System.Drawing.Point(120, 56) Me.ValueCelsius.Name = "ValueCelsius" Me.ValueCelsius.Size = New System.Drawing.Size(100, 20) Me.ValueCelsius.TabIndex = 3 " "ValueKelvin " Me.ValueKelvin.Location = New System.Drawing.Point(120, 88) Me.ValueKelvin.Name = "ValueKelvin" Me.ValueKelvin.Size = New System.Drawing.Size(100, 20) Me.ValueKelvin.TabIndex = 5 " "ConvertTemperature " Me.ConvertTemperature.Location = New System.Drawing.Point(144, 128) Me.ConvertTemperature.Name = "ConvertTemperature" Me.ConvertTemperature.Size = New System.Drawing.Size(75, 23) Me.ConvertTemperature.TabIndex = 6 Me.ConvertTemperature.Text = "Convert" Me.ConvertTemperature.UseVisualStyleBackColor = True " "ConvertForm " Me.AcceptButton = Me.ConvertTemperature Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(245, 169) Me.Controls.Add(Me.ConvertTemperature) Me.Controls.Add(Me.ValueKelvin) Me.Controls.Add(Me.ValueCelsius) Me.Controls.Add(Me.ValueFahrenheit) Me.Controls.Add(Me.SourceKelvin) Me.Controls.Add(Me.SourceCelsius) Me.Controls.Add(Me.SourceFahrenheit) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "ConvertForm" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Convert Temperatures" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents SourceFahrenheit As System.Windows.Forms.RadioButton Friend WithEvents SourceCelsius As System.Windows.Forms.RadioButton Friend WithEvents SourceKelvin As System.Windows.Forms.RadioButton Friend WithEvents ValueFahrenheit As System.Windows.Forms.TextBox Friend WithEvents ValueCelsius As System.Windows.Forms.TextBox Friend WithEvents ValueKelvin As System.Windows.Forms.TextBox Friend WithEvents ConvertTemperature As System.Windows.Forms.Button
End Class Public Class ConvertForm
Private Sub ConvertTemperature_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConvertTemperature.Click On Error Resume Next If (SourceFahrenheit.Checked = True) Then If (IsNumeric(ValueFahrenheit.Text) = True) Then ValueCelsius.Text = _ (Val(ValueFahrenheit.Text) - 32) / 1.8 ValueKelvin.Text = _ ((Val(ValueFahrenheit.Text) - 32) / 1.8) + 273.15 Else ValueCelsius.Text = "Error" ValueKelvin.Text = "Error" End If ElseIf (SourceCelsius.Checked = True) Then If (IsNumeric(ValueCelsius.Text) = True) Then ValueFahrenheit.Text = _ (Val(ValueCelsius.Text) * 1.8) + 32 ValueKelvin.Text = Val(ValueCelsius.Text) + 273.15 Else ValueFahrenheit.Text = "Error" ValueKelvin.Text = "Error" End If Else If (IsNumeric(ValueKelvin.Text) = True) Then ValueFahrenheit.Text = _ ((Val(ValueKelvin.Text) - 273.15) * 1.8) + 32 ValueCelsius.Text = Val(ValueKelvin.Text) - 273.15 Else ValueFahrenheit.Text = "Error" ValueCelsius.Text = "Error" End If End If End Sub
End Class
public class FahrenheitCelsiusConverter
Public Shared Sub Main() Application.Run(New ConvertForm) End Sub
End Class</source>
Copy selection text from a TextBox and paste to another
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextBoxCopyPaste
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox2 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(128, 72) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(112, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(128, 120) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(112, 23) Me.Button1.TabIndex = 1 Me.Button1.Text = "Copy Selected Text" " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(344, 48) Me.Label1.TabIndex = 2 Me.Label1.Text = "Copying Selected Text" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(128, 168) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(112, 20) Me.TextBox2.TabIndex = 3 Me.TextBox2.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(360, 253) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Copy() TextBox2.Paste() End Sub
End Class</source>
Create PasswordField by setting the TextBox.PasswordChar
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PasswordFieldByTextBox
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.TextBox2 = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(96, 56) Me.TextBox1.Name = "TextBox1" Me.TextBox1.PasswordChar = Microsoft.VisualBasic.ChrW(42) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 104) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Read Text" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(96, 160) Me.TextBox2.Name = "TextBox2" Me.TextBox2.TabIndex = 2 Me.TextBox2.Text = "" " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(0, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(184, 40) Me.Label1.TabIndex = 3 Me.Label1.Text = "Passwords" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox2.Text = TextBox1.Text End Sub
End Class</source>
Disable TextBox Context Menu
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DisableTextBoxContextMenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim text_box As New NoCtxMnuTextBox text_box.Location = New Point(TextBox1.Left, TextBox1.Top + TextBox1.Height + 4) text_box.Size = TextBox1.Size Me.Controls.Add(text_box) End Sub
End Class Public Class NoCtxMnuTextBox
Inherits System.Windows.Forms.TextBox Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Const WM_CONTEXTMENU As Integer = &H7B If m.Msg <> WM_CONTEXTMENU Then MyBase.WndProc(m) End If End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(16, 16) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(120, 20) Me.TextBox1.TabIndex = 0 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(156, 102) Me.Controls.Add(Me.TextBox1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class</source>
Display text file in a TextBox
<source lang="vbnet">Imports System.IO Imports System.Windows.Forms public class TextFileDisplayTextBox
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 24) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Select a File" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(20, 80) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(250, 150) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FileDB As New OpenFileDialog() FileDB.Filter = "All files | *.* | Word files | *.doc | Text files | *.txt" FileDB.FilterIndex = 3 FileDB.InitialDirectory = "C:\Temp" If (FileDB.ShowDialog() = DialogResult.OK) Then Dim FS As FileStream Try FS = FileDB.OpenFile() Catch MsgBox("Error opening " & FileDB.FileName) End Try Dim TextData(1025) As Byte Dim BytesRead As Integer Dim I As Integer Dim NewText As String Do Try BytesRead = FS.Read(TextData, 1, 1024) NewText = "" For I = 1 To BytesRead NewText = NewText & Chr(TextData(I)) Next TextBox1.AppendText(NewText) Catch MsgBox("Error reading file") End Try Loop While (BytesRead <> 0) FS.Close() Else MsgBox("User selected Cancel") End If End Sub
End Class</source>
Get input from TextBox and Set to Label
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GetTextInTextBoxAndSetToLabel
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "Label1 " Me.Label1.Location = New System.Drawing.Point(48, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(160, 32) Me.Label1.TabIndex = 0 " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(64, 64) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(120, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(88, 104) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 2 Me.Button1.Text = "Display" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(256, 166) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = "Hello," + TextBox1.Text + "" End Sub
End Class</source>
Get selected text in TextBox
<source lang="vbnet">Imports System.Windows.Forms Imports System.Drawing.Text Imports System.Drawing Imports System.Drawing.Drawing2D public class TextBoxSelectionLength
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.SelectionLength = 0 Then MessageBox.Show(TextBox1.Text) Else MessageBox.Show(TextBox1.SelectedText) End If End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(12, 12) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(410, 137) Me.TextBox1.TabIndex = 0 " "Button1 " Me.Button1.Location = New System.Drawing.Point(150, 173) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(131, 23) Me.Button1.TabIndex = 1 Me.Button1.Text = "Display selected text" Me.Button1.UseVisualStyleBackColor = True " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(434, 208) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button
End Class</source>
Inherit TextBox to create custom control based on TextBox
<source lang="vbnet">Imports System Imports System.Collections Imports System.IO Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Public Class FileTextBox
Inherits TextBox
- Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ruponentModel.IContainer) MyClass.New() "Required for Windows.Forms Class Composition Designer support Container.Add(Me) End Sub Public Sub New() MyBase.New() "This call is required by the Component Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Component overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Component Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Component Designer "It can be modified using the Component Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() components = New System.ruponentModel.Container() End Sub
- End Region
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) If Not File.Exists(Me.Text) Then Me.ForeColor = Color.Red Else Me.ForeColor = Color.Black End If MyBase.OnTextChanged(e) End Sub
End Class</source>
Multiline Text Boxes
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextBoxMultiline
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(5, 5) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.TextBox1.Size = New System.Drawing.Size(200, 150) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(288, 189) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
End Class</source>
Numeric TextBox
<source lang="vbnet">Imports System.Windows.Forms public class NumericTextBoxDemo
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents NumTextBox1 As NumTextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.NumTextBox1 = New NumTextBox() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(144, 120) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Do It!" " "NumTextBox1 " Me.NumTextBox1.Location = New System.Drawing.Point(32, 56) Me.NumTextBox1.Name = "NumTextBox1" Me.NumTextBox1.TabIndex = 3 Me.NumTextBox1.Text = "NumTextBox1" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.NumTextBox1, Me.Button1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
End Class
Public Class NumTextBox
Inherits System.Windows.Forms.TextBox
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "UserControl1 overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() " "NumTextBox " End Sub
- End Region
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs) If Not IsNumeric(Me.Text) Then MessageBox.Show("You must enter a numeric value!", _ "Please try again....", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation) Me.Focus() End If MyBase.OnLeave(e) End Sub
End Class</source>
Save text in TextBox to a file
<source lang="vbnet">Imports System.IO Imports System.Windows.Forms public class SaveTextBoxToFile
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 24) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Save to File" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(20, 80) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(250, 150) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1}) Me.Name = "Form1" Me.Text = "SaveTextToFile" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FileDB As New SaveFileDialog() FileDB.Filter = "All files | *.* | Word files | *.doc | Text files | *.txt" FileDB.FilterIndex = 3 FileDB.InitialDirectory = "C:\Temp" FileDB.AddExtension = True FileDB.DefaultExt = "txt" If (FileDB.ShowDialog() = DialogResult.OK) Then Dim FS As FileStream Try FS = New FileStream(FileDB.FileName, FileMode.Create) Dim I As Integer For I = 0 To TextBox1.Text.Length - 1 FS.WriteByte(Asc(TextBox1.Text.Chars(I))) Next Catch Console.WriteLine("Error writing file") End Try FS.Close() MsgBox("Text saved to " & FileDB.FileName) Else MsgBox("User selected Cancel") End If End Sub
End Class</source>
TextBox Text Changed Event
<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class TextBoxTextChanged : inherits Form
dim txt as TextBox dim btn as Button dim strOriginal as string public sub New() Text = "TextBox Modified and TextChanged" Size = new Size(300, 375) txt = new TextBox() txt.Parent = me txt.Text = "Enter text here." txt.Size = new Size(280, 275) txt.Location = new Point(10,10) AddHandler txt.TextChanged, AddressOf txt_TextChanged txt.Multiline = true txt.BorderStyle = BorderStyle.Fixed3D txt.ScrollBars = ScrollBars.Vertical txt.Anchor = AnchorStyles.Left or AnchorStyles.Right or AnchorStyles.Top or AnchorStyles.Bottom strOriginal = txt.Text btn = new Button() btn.Parent = me btn.Text = "Check " btn.Location = new Point(20,320) AddHandler btn.Click, AddressOf btn_Click btn.Anchor = AnchorStyles.Bottom end sub public shared sub Main() Application.Run(new TextBoxTextChanged()) end sub private sub txt_TextChanged(ByVal sender as object,ByVal e as EventArgs) Console.WriteLine("txt_TextChanged") if strOriginal = txt.Text then txt.Modified = false else txt.Modified = true end if end sub private sub btn_Click(ByVal sender as object,ByVal e as EventArgs) if txt.Modified then Console.WriteLine("modified.") strOriginal = txt.Text txt.Modified = false else Console.WriteLine("not been modified." ) end if end sub
end class</source>