KeyPressEventHandler 委派
很屌
大致上先自訂一個 處理事件 sub(sender as obj, e as event)
然後透過控制項加入某個事件,將加入的事件委派由 自訂事件來處理
就可以了
要注意的是
當使用了委派
該事件將會一直跟著控制項
直到移除委派
AddHandler MyObject.Event1, AddressOf Me.MyEventHandler
RemoveHandler MyObject.Event1, AddressOf Me.MyEventHandler
ex
'自訂事件
'處理 Cell 輸入 數字字元
Private Sub ChkNumeric(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If e.KeyChar <> Convert.ToChar(8) Then
'只讓數字過
e.Handled = Not Char.IsDigit(e.KeyChar)
Else '非內容輸入 時, 將 判斷為 鍵盤控制項按鈕(backspace) ,輸入可作用
e.Handled = Not Char.IsControl(e.KeyChar)
End If
End Sub
'編輯儲存格時,將所有輸入轉大寫
Private Sub myDataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Me.EditingControlShowing
If (TypeOf e.Control Is TextBox) Then
DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
If Me.CurrentCell.ColumnIndex = Me.Columns("Type").Index Or _
Me.CurrentCell.ColumnIndex = Me.Columns("Sn").Index Or _
Me.CurrentCell.ColumnIndex = Me.Columns("Ordno").Index Then
'委派,將控制項加入 keyPress事件,該事件委派由 ChkNumeric 處理
AddHandler e.Control.KeyPress, AddressOf ChkNumeric
End If
End If
End Sub
沒有留言:
張貼留言