1

我有一个 win 表单,其中表单 KeyPreview 设置为“true”,我编写了代码以在表单 keydown 事件中按 Delete 键时从数据库中删除详细信息。但是,当我尝试使用 Delete 键清除表单中文本框中的内容时,会发生表单 keydown 事件。如何防止在按 Delete 清除文本框内容时发生表单 keydown 事件?

我用于 Form KeyDown 的代码是

Public Sub frm_Customers_KeyDown(ByVal myForm As frm_Customers, ByVal e As System.Windows.Forms.KeyEventArgs, ByVal txtTIN As TextBox, ByVal txtCustomerPhone As TextBox, ByVal txtCustomerMobileno As TextBox, ByVal txtCustomerEmail As TextBox, ByVal txtCustomerAddress As TextBox, ByVal btnCustomerSave As Button, ByVal txtCustomerName As TextBox, ByVal txtCustomerAliasname As TextBox, ByVal txtacgroup As TextBox, ByVal txtcustomerOPAmount As TextBox, ByVal UC_autoCompleteComboBox1 As UC_autoCompleteComboBox, ByVal frm As Form, ByVal errorProvider1 As System.Windows.Forms.ErrorProvider, ByVal rbCredit As RadioButton)

 If e.KeyCode = Keys.F2 Then

   If btnCustomerSave.Tag = 1 Then
        updateprocess(txtTIN, txtCustomerPhone, txtCustomerMobileno, txtCustomerEmail, txtCustomerAddress, btnCustomerSave, txtCustomerName, txtCustomerAliasname, txtacgroup, txtcustomerOPAmount, UC_autoCompleteComboBox1, errorProvider1, rbCredit)
   Else
        saveprocess(txtTIN, txtCustomerPhone, txtCustomerMobileno, txtCustomerEmail, txtCustomerAddress, btnCustomerSave, txtCustomerName, txtCustomerAliasname, txtacgroup, txtcustomerOPAmount, UC_autoCompleteComboBox1, errorProvider1, rbCredit)
   End If

 ElseIf e.KeyCode = Keys.Delete Then
   deletionprocess(txtTIN, txtCustomerPhone, txtCustomerMobileno, txtCustomerEmail, txtCustomerAddress, btnCustomerSave, txtCustomerName, txtCustomerAliasname, txtacgroup, txtcustomerOPAmount, UC_autoCompleteComboBox1, errorProvider1)
 End If

End Sub
4

2 回答 2

0

一种方法是检查文本控件是否具有焦点。如果是这样,那么继续并忽略按键按下。

http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol.aspx

于 2013-08-24T07:08:41.283 回答
0

我尝试myForm.ActiveControl.Name使用所有textbox.name控件属性检查控件是否存在光标并且它对我有用。这是我使用的代码

If e.KeyCode = Keys.Delete Then

   If txtTIN.Name = myForm.ActiveControl.Name Or txtCustomerPhone.Name = myForm.ActiveControl.Name Or txtCustomerMobileno.Name = myForm.ActiveControl.Name Or txtCustomerEmail.Name = myForm.ActiveControl.Name Or txtCustomerAddress.Name = myForm.ActiveControl.Name Or txtCustomerName.Name = myForm.ActiveControl.Name Or txtCustomerAliasname.Name = myForm.ActiveControl.Name Or txtacgroup.Name = myForm.ActiveControl.Name Or txtcustomerOPAmount.Name = myForm.ActiveControl.Name Then
      Exit Sub
   Else
      deletionprocess(txtTIN, txtCustomerPhone, txtCustomerMobileno, txtCustomerEmail, txtCustomerAddress, btnCustomerSave, txtCustomerName, txtCustomerAliasname, txtacgroup, txtcustomerOPAmount, UC_autoCompleteComboBox1, errorProvider1)
   End If

End If
于 2013-08-26T04:15:12.967 回答