如何在文本框中搜索单词并将其替换为textchanged上的单词?

时间:2012-12-15 00:05:18

标签: .net regex vb.net string visual-studio-2010

当用户输入我的文本框时,我希望它能在没有以大写字母开头的情况下自动更正几个单词。这有点像visual studio如何纠正vb语法。一个例子是:

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

我正在使用vb.net。

1 个答案:

答案 0 :(得分: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

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