我在更新表單上的vb.net應用程序中使用富文本框來獲取地址值。我的更新查詢是在運行時生成的,它循環遍歷表單上的文本框,並檢查哪些字段具有某些值,並在數據庫中更新相應的字段。在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鍵移動到下一個文本框,然後上面的代碼跟蹤該地址富文本框已獲得一些新的價值,即輸入密鑰,並更新地址字段在數據庫中。我怎樣才能擺脫這個問題?
後面轉移焦點代碼:
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事件處理它..!
顯然你的「轉移重心上回車鍵」的代碼被打破。你沒有發佈它,但我的水晶球說你忘了使用Handled或SuppressKeyPress屬性。 – 2011-12-27 15:37:43
我已經在keydown事件中添加了處理焦點轉移的代碼...請現在 – user944591 2011-12-27 18:18:18