-2

当用户输入我的文本框时,我希望它自动更正几个单词,如果它们输入时没有以大写字母开头。这有点像 Visual Studio 如何更正 vb 语法。这方面的一个例子是:

用户键入“else”,它会自动将其更正为“Else”。我知道我需要它在 textchanged 事件上执行此操作。我只是不知道该怎么做。

我正在使用 vb.net。

4

1 回答 1

0

如果您的问题与起始上限有关,那么这是我在项目中使用的代码。

Public Sub StartCaps(ByRef mCtrl As Object)
    Dim mTypeName As String
    mTypeName = TypeName(mCtrl)
    Select Case mTypeName
        Case "TextBox"
            Dim sel_start As Integer, sel_length As Integer
            Dim txtProperCase As TextBox
            txtProperCase = CType(mCtrl, TextBox)
            sel_start = txtProperCase.SelectionStart
            sel_length = txtProperCase.SelectionLength
            txtProperCase.Text = StrConv(txtProperCase.Text, VbStrConv.ProperCase)
            txtProperCase.SelectionStart = sel_start
            txtProperCase.SelectionLength = sel_length
        Case "ComboBox"
            Dim sel_start As Integer, sel_length As Integer
            Dim cmbProperCase As ComboBox
            cmbProperCase = CType(mCtrl, ComboBox)
            sel_start = cmbProperCase.SelectionStart
            sel_length = cmbProperCase.SelectionLength
            cmbProperCase.Text = StrConv(cmbProperCase.Text, VbStrConv.ProperCase)
            cmbProperCase.SelectionStart = sel_start
            cmbProperCase.SelectionLength = sel_length
        Case "DataGridView"
            Dim grdProperCase As DataGridView, mCell As DataGridViewCell
            grdProperCase = CType(mCtrl, DataGridView)
            If Not grdProperCase Is Nothing Then
                mCell = grdProperCase.CurrentCell
                If Not mCell Is Nothing AndAlso Not mCell.Value Is Nothing Then
                    mCell.Value = StrConv(mCell.Value.ToString, VbStrConv.ProperCase)
                End If
            End If
    End Select
End Sub

这是从控件 TextChanged 事件调用

Private Sub txtSname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSname.TextChanged, txtName.TextChanged
    StartCaps(sender)
End Sub

-------------- 它可能对您的代码有所帮助 ------ 祝你好运 ---

于 2012-12-15T13:39:47.593 回答