2011年12月7日 星期三

#VB 重複 加入/移除 自訂事件


     If Me.CurrentCell.ColumnIndex = Me.Columns("Type").Index Or _
                    Me.CurrentCell.ColumnIndex = Me.Columns("Sn").Index Or _
                    Me.CurrentCell.ColumnIndex = Me.Columns("Ordno").Index Or _
                    Me.CurrentCell.ColumnIndex = Me.Columns("Trndate").Index Or _
                    Me.CurrentCell.ColumnIndex = Me.Columns("Delidate").Index Then

                    '委派,將控制項加入 keyPress事件,該事件委派由 ChkNumeric 處理
                    AddHandler e.Control.KeyPress, AddressOf ChkNumeric
                Else
                    '其他欄位,則將該委派移除
                    RemoveHandler e.Control.KeyPress, AddressOf ChkNumeric
                End If


這邊發生說
當我在AddHandler的欄位多次來回的時候
將會不斷重複加入自訂事件

變成說道其他欄位
該事件雖然被 RemoveHandler一次
卻還存在其他數次加入過的事件


@所以應該避免重複加入已經加入的事件
移除也應該避免重複移除已經移除的事件

目前透過加入一個判斷的變數,
判斷目前事件加入狀態

沒有留言:

張貼留言