检查vb.net中Rich文本框中的文本

时间:2011-12-27 14:54:15

标签: vb.net richtextbox

我在更新表单上的vb.net应用程序中使用Rich Textbox来获取地址值。我的更新查询是在运行时生成的,循环遍历表单上的文本框,并检查哪些字段有一些值,并在数据库中更新相应的字段。

For Each x As Control In Me.Controls

        If x.GetType Is GetType(TextBox) Or x.GetType Is GetType(MaskedTextBox) Or x.GetType Is GetType(RichTextBox) Then
            If Not x.Name = "party_code" Then

                'if user has not entered a value in a textbox then incremnets the flag variable

                If (x.Text = String.Empty) Or ((x.Name = "contact1" Or x.Name = "contact2" Or x.Name = "ptcl") And (x.Text.Trim().EndsWith("-") And x.Text.Trim().StartsWith("-"))) Then
                    flag += 1
                End If

                'checks if the user has entered a value in some textbox 

                If (Not x.Text = String.Empty And Not x.Name = "contact1" And Not x.Name = "contact2" And Not x.Name = "ptcl") Or ((x.Name = "contact1" Or x.Name = "contact2" Or x.Name = "ptcl") And Not x.Text.Trim.EndsWith("-")) Then


                    'generates query text for the textbox which contains some value

                    str = str & comma & x.Name & " = @" & x.Name

                    comma = " , "
                End If
            End If
        End If

    Next

我也使用Enter键将焦点转移到表单上的下一个文本框。当焦点到达Adress Rich文本框时,我不必更新数据库中的地址字段,然后按Enter键移动到下一个文本框,然后上面的代码跟踪地址Rich Textbox有一些新值,即Enter键并更新数据库中的地址字段。我怎样才能摆脱这个问题?

改变焦点的代码:

 Private Sub town_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles town.KeyDown
    If e.KeyData = Keys.Return Then
        address.Focus()
    End If
End Sub

 Private Sub address_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles address.KeyDown
    If e.KeyData = Keys.Return Then
        Button3.Focus()
    End If
End Sub

我通过Keydown活动来处理它。!!

1 个答案:

答案 0 :(得分:0)

由于您正在更改town_KeyDown中的焦点,因此使用e.Handled处理KeyDownEvent会阻止按键进一步处理。

Private Sub town_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles town.KeyDown 
    If e.KeyData = Keys.Return Then 
        address.Focus() 
        e.Handled = True
        Exit Sub
    End If 
End Sub