Example 047 text box editing

TextBox class

https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.textbox?view=netframework-4.8

When using the  TextBox  control, the user can enter text in the application. This control has other features not found in standard Windows text box controls, including multiline editing and password character masking.

Generally, the TextBox  control is used to display or accept a line of text as input. You can use the " Multiline " and " ScrollBars " properties to enable display or enter multiple lines of text. Set the  AcceptsTab  and  AcceptsReturn  properties  to enable larger text operations truein multi-line  TextBox controls.

You can  limit the  amount of text entered in the TextBox control by setting the  MaxLength property to a specific number of characters  The TextBox  control can also be used to accept passwords and other sensitive information. You can use the  PasswordChar  property to mask characters entered in the single-line version of the control. Using the " CharacterCasing " attribute allows the user to type only uppercase, lowercase, or  a combination of uppercase and lowercase characters with the  TextBox control.

To scroll   the content of the TextBox until the cursor (caret) is in the visible area of ​​the control, you can use the  ScrollToCaret  method. To select the range of text in the text box, you can use the  Select  method.

To limit  text input in the  TextBox control, you can  create an event handler for the  KeyDown event to verify each character entered in the control. You can also limit  all data input in the TextBox control by setting the  ReadOnly  property  trueto  .

Attributes

Table 2
AcceptsReturn

Gets or sets a value indicating  whether a new line of text is created in the control or the form's default button is activated when the Enter key is pressed in a multiline  TextBox control.

AcceptsTab

Gets or sets a value indicating whether to press a Tab character in the control when pressing the Tab key in a multiline text box control, instead of moving the focus to the next control in the order of the tabs.

(Inherited from  TextBoxBase )
AllowDrop

Gets or sets a value indicating whether the control can accept data that the user drags and drops onto it.

(Inherited from  Control )
AutoSize

Gets or sets a value indicating whether the height of the control is automatically adjusted when the font assigned to the control is changed.

(Inherited from  TextBoxBase )
BackColor

Gets or sets the background color of the control.

(Inherited from  TextBoxBase )
CanUndo

Gets a value indicating whether the user can undo the previous operation in the text box control.

(Inherited from  TextBoxBase )
ForeColor

Gets or sets the foreground color of the control.

(Inherited from  TextBoxBase )
Lines

Gets or sets the text line in the text box control.

(Inherited from  TextBoxBase )
Location

Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.

(Inherited from  Control )
MaxLength

Gets or sets the maximum number of characters that users can type or paste in the text box control.

(Inherited from  TextBoxBase )
Multiline

Gets or sets a value indicating whether this is a multi-line  TextBox  control.

ReadOnly

Gets or sets a value indicating whether the text in the text box is read-only.

(Inherited from  TextBoxBase )
ScrollBars

Gets or sets which scroll bars should appear in the multi-line  TextBox  control.

SelectedText

Gets or sets a value indicating the currently selected text in the control.

(Inherited from  TextBoxBase )
SelectionStart

Gets or sets the starting point of the selected text in the text box.

(Inherited from  TextBoxBase )
Text

Gets or sets the text associated with this control.

textAlign

Gets or sets the  text  alignment in the TextBox control.

TextLength

Get the length of the text in the control.

(Inherited from  TextBoxBase )

 

method

table 3
AppendText(String)

Append text to the current text in the text box.

(Inherited from  TextBoxBase )
Clear()

Clear all text from the text box control.

(Inherited from  TextBoxBase )
Copy()

将文本框中的当前选定内容复制到“剪贴板”。

(继承自 TextBoxBase)
CreateGraphics()

为控件创建 Graphics

(继承自 Control)
Cut()

将文本框中的当前选定内容移动到“剪贴板”中。

(继承自 TextBoxBase)
DeselectAll()

将 SelectionLength 属性的值指定为零,从而不会在控件中选择字符。

(继承自 TextBoxBase)
DoDragDrop(Object, DragDropEffects)

开始拖放操作。

(继承自 Control)
Paste()

用剪贴板的内容替换文本框中的当前选定内容。

(继承自 TextBoxBase)
Paste(String)

将选定文本设置为指定文本,但不清除撤消缓冲区。

Select()

激活控件。

(继承自 Control)
Undo()

撤消文本框中的上一个编辑操作。

(继承自 TextBoxBase)
Update()

使控件重绘其工作区内的无效区域。

(继承自 Control)

对话框类Form是定义在 Windows名称空间下的类,对话框类中定义了一个名为Controls的属性,用于表示对话框上的所有控件。 Controls为控件集合,可利用下标访问每个控件。集合变量的使用方法类似于数组,可通过下标指定访问的集合元素。

本例控件及其属性值

控件 属性 控件 属性
TextBox Name txtText Button Name btnClear
  Multiline true   text 清除
  text   Button Name btnReadOnly
Button Name btnCopy   Text 只读
  Text 复制 Button Name btnUndo
Button Name btnCut   Text 撤消
  Text 剪切 Button Name btnPaste
        Text 粘贴
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtText.Width = Me.Width
        txtText.Height = 220
        txtText.Left = 0
        txtText.Top = 0
        txtText.WordWrap = True '是否自动换行
    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        Static tempStr As String
        tempStr = txtText.SelectedText
        If tempStr = "" Then
            MsgBox("选择文本")
        End If
        String.Copy(tempStr)
    End Sub

    Private Sub btnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaste.Click
        txtText.Paste()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtText.Clear()
    End Sub

    Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click
        txtText.Undo()
    End Sub

    Private Sub btnReadOnly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadOnly.Click
        Dim i As Integer
        If txtText.ReadOnly Then
            btnReadOnly.Text = "只读"
            txtText.ReadOnly = False
            For i = 0 To Me.Controls.Count - 1
                Me.Controls(i).Enabled = True
            Next
        Else
            btnReadOnly.Text = "可写"
            txtText.ReadOnly = True
            For i = 0 To Me.Controls.Count - 1
                If Not (Me.Controls(i) Is btnReadOnly) Then
                    Me.Controls(i).Enabled = False
                End If
            Next

        End If
    End Sub

    Private Sub btnCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCut.Click
        txtText.Cut()
    End Sub
End Class

 

发布了146 篇原创文章 · 获赞 0 · 访问量 2713

Guess you like

Origin blog.csdn.net/ngbshzhn/article/details/105682908