【问题标题】:Checking text in Rich textbox in vb.net在 vb.net 的富文本框中检查文本
【发布时间】:2011-12-28 02:25:49
【问题描述】:

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

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 键将焦点转移到表单上的下一个文本框。当焦点到达地址富文本框并且我不必更新数据库中的地址字段并且我按 Enter 键移动到下一个文本框时,上面的代码跟踪地址富文本框有一些新值,即 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 事件处理了它..!!

【问题讨论】:

  • 显然您的“将焦点转移到 Enter 键”代码已损坏。您没有发布它,但我的水晶球说您忘记使用 Handled 或 SuppressKeyPress 属性。
  • 我已经在 keydown 事件中添加了处理焦点转移的代码...请立即告诉问题

标签: vb.net richtextbox


【解决方案1】:

由于您正在更改城镇中的焦点_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 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多