TextBox (txt)

alt textTextBox - Allows you to display text and to allow the user to enter information.

A multilane textbox has a practical limit 32KB of text. if you require any more text then you will have to consider using a RichTextBox.


TextBox does not support BackColor = Transparent ??


No Vertical Alignment

Unlike a label control a textbox has no vertical alignment
Using labels as textboxes - there are several advantages as labels are readonly by default
TextBox to resemble a Label
BackColor = Control
ReadOnly = True
BorderStyle = None


Label to resemble a TextBox
BackColor = White
ReadOnly = False
BorderStyle = Fixed3D


Doesn't Accept Ampersand (&)

A single ampersand is not displayed in a label and must be changed to a double ampersand (&&)



Examples

alt text

Useful Code

Removes the selection in the textbox

txtTextBox1.Select(0,0) 

Selecting All

txtTextBox.SelectionStart = 0; 
txtTextBox.Selection.Length = txtTextBox.Text.Length();


Read-Only textbox

Have to use a label instead to look like a textbox because Read-Only make the background grey ??



??

Private Sub txtTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms) Handles txtBox1.KeyPress, txtBox2.KeyPress 
If (e.KeyChar = Microsoft.VisualBasic.ChrW(1)) Then
   DirectCast(sender, Windows.Forms.TextBox).SelectAll()
End If
End Sub



Restricting to only Numbers


Private Sub txtTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms) 

''this allows a Ctrl + V in the text box
   If Microsoft.VisualBasic.Asc(e.KeyChar) = 22 Then
      Exit Sub
   End If

''this allows a Ctrl + C in the text box
   If Microsoft.VisualBasic.Asc(e.KeyChar) = 3 Then
      Exit Sub
   End If

''this allows a Ctrl + X in the text box
   If Microsoft.VisualBasic.Asc(e.KeyChar) = 24 Then
      Exit Sub
   End If

   If (e.KeyChar < "0"c Or e.KeyChar > "9"c) And _
      Not (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then

      e.Handled = True
   End If

End Sub

Properties

AcceptsReturn Gets or sets a value indicating whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the form.
AcceptsTab Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order. (Inherited from TextBoxBase.)
AutoCompleteCustomSource Gets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSource property is set to CustomSource.
AutoCompleteMode Gets or sets an option that controls how automatic completion works for the TextBox.
AutoCompleteSource Gets or sets a value specifying the source of complete strings used for automatic completion.
AutoSize Gets or sets a value indicating whether the height of the control automatically adjusts when the font assigned to the control is changed. This property is not relevant for this class. (Inherited from TextBoxBase.)
BackColor Gets or sets the background color of the control. (Inherited from TextBoxBase.)
BorderStyle Gets or sets the border type of the text box control. (Inherited from TextBoxBase.)
CanUndo Gets a value indicating whether the user can undo the previous operation in a text box control. (Inherited from TextBoxBase.)
CharacterCasing Gets or sets whether the TextBox control modifies the case of characters as they are typed.
DefaultCursor Gets or sets the default cursor for the control. (Inherited from TextBoxBase.)
DefaultSize (Inherited from TextBoxBase.)
ForeColor Gets or sets the foreground color of the control. (Inherited from TextBoxBase.)
HideSelection Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus. (Inherited from TextBoxBase.)
Lines Gets or sets the lines of text in a text box control. (Inherited from TextBoxBase.)
MaxLength Gets or sets the maximum number of characters the user can type or paste into the text box control. (Inherited from TextBoxBase.)
Modified Gets or sets a value that indicates that the text box control has been modified by the user since the control was created or its contents were last set. (Inherited from TextBoxBase.)
MultiLineOverridden. Gets or sets a value indicating whether this is a multiline TextBox control.
PasswordCharGets or sets the character used to mask characters of a password in a single-line TextBox control.
PreferredHeight Gets the preferred height for a text box. (Inherited from TextBoxBase.)
ReadOnlyGets or sets a value indicating whether text in the text box is read-only. (Inherited from TextBoxBase.)
ScrollBars Gets or sets which scroll bars should appear in a multiline TextBox control.
SelectedTextReturns or sets the string containing the currently selected text; consists of a zero-length string ("") if no characters are selected.
SelectionLengthGets or sets the number of characters selected in the text box. (Inherited from TextBoxBase.)
SelectionStartReturns or sets the starting point of text selected, indicates the position of the insertion point if no text is selected Gets or sets the starting point of text selected in the text box. (Inherited from TextBoxBase.)
ShortcutsEnabled Gets or sets a value indicating whether the defined shortcuts are enabled. (Inherited from TextBoxBase.)
Text Overridden. Gets or sets the current text in the TextBox.
TextAlign Gets or sets how text is aligned in a TextBox control.
TextLength Gets the length of text in the control. (Inherited from TextBoxBase.)
UseSystemPasswordChar Gets or sets a value indicating whether the text in the TextBox control should appear as the default password character.
WordWrap Indicates whether a multiline text box control automatically wraps words to the beginning of the next line when necessary. (Inherited from TextBoxBase.)

Methods

AppendText Appends text to the current text of a text box. (Inherited from TextBoxBase.)
Clear Clears all text from the text box control. (Inherited from TextBoxBase.)
ClearUndo Clears information about the most recent operation from the undo buffer of the text box. (Inherited from TextBoxBase.)
Copy Copies the current selection in the text box to the Clipboard. (Inherited from TextBoxBase.)
DeselectAll Specifies that the value of the SelectionLength property is zero so that no characters are selected in the control. (Inherited from TextBoxBase.)
Dispose Overloaded. Releases the resources used by the TextBox.
GetCharFromPosition Retrieves the character that is closest to the specified location within the control. (Inherited from TextBoxBase.)
GetCharIndexFromPosition Retrieves the index of the character nearest to the specified location. (Inherited from TextBoxBase.)
GetFirstCharIndexFromLine Retrieves the index of the first character of a given line. (Inherited from TextBoxBase.)
GetFirstCharIndexOfCurrentLine Retrieves the index of the first character of the current line. (Inherited from TextBoxBase.)
GetLineFromCharIndex Retrieves the line number from the specified character position within the text of the control. (Inherited from TextBoxBase.)
GetPositionFromCharIndex Retrieves the location within the control at the specified character index. (Inherited from TextBoxBase.)
Paste Overloaded. Replaces the current selection in the text box with the contents of the Clipboard.
ScrollToCaret Scrolls the contents of the control to the current caret position. (Inherited from TextBoxBase.)
Select Overloaded. Selects text within the control. (Inherited from TextBoxBase.)
SelectAll Selects all text in the text box. (Inherited from TextBoxBase.)

Events

AcceptsTabChanged Occurs when the value of the AcceptsTab property has changed. (Inherited from TextBoxBase.)
BorderStyleChanged Occurs when the value of the BorderStyle property has changed. (Inherited from TextBoxBase.)
Click Occurs when the text box is clicked. (Inherited from TextBoxBase.)
HideSelectionChanged Occurs when the value of the HideSelection property has changed. (Inherited from TextBoxBase.)
ModifiedChanged Occurs when the value of the Modified property has changed. (Inherited from TextBoxBase.)
MouseClick Occurs when the control is clicked by the mouse. (Inherited from TextBoxBase.)
MultilineChanged Occurs when the value of the Multiline property has changed. (Inherited from TextBoxBase.)
TextAlignChanged Occurs when the value of the TextAlign property has changed.
TextChanged Occurs when the value of the TextAlign property has changed.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext