使用Delete键清除文本框内容时出现的Delete键的表单KeyDown事件方法

时间:2013-08-24 07:00:29

标签: vb.net forms visual-studio-2008 .net-3.5

我有一个win表单,其中表单KeyPreview设置为'true',我编写了代码,用于在表单keydown事件中按Delete键删除数据库中的详细信息。但是当我尝试使用Delete键清除表单中文本框中的内容时,表单就会出现keydown事件。如何防止表单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

2 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是检查文本控件是否具有焦点。如果确实如此,那么请继续忽略keydown press。

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

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