2013-12-20 2 views
0

나는 datagridview에서 흥미로운 상황이 있습니다. 이 I "프로그래밍 방식으로 클릭"내 논리 프로그램의 흐름을 얻기 위해 현재 행에 4 셀로맞춤 발신자 받기

rowindex = DataGridView1.CurrentRow.Index 
Dim cea As New DataGridViewCellEventArgs(4, rowindex) 
DataGridView1_CellClick("program", cea) 

: _keydown 핸들러에서

는 코드입니다.
하지만 마우스로 셀 4를 클릭해도 동일하게 데이터 그 림 뷰에서 발생할 수 있습니다.

질문 : 이벤트를 (마우스 또는 프로그램별로) 활성화 한 DataGridview의 _CellClick 처리기에서 어떻게 인식 할 수 있습니까? 그 목적을 위해 나는 "프로그램"이라는 이름으로 발신자를 불렀다.

_CellClick 처리기에서 어떻게 든이 이름 (보낸 사람 이름)을 얻을 수 있으며 보낸 사람을 "프로그램"으로 캐스팅 할 수 없기 때문에 어떻게 할 수 있습니까? 아니면 내가 할 수 있니?

답변

1

첫 번째 : 직접 이벤트 처리기 메서드를 호출하면 안됩니다. 그리고 다른 이벤트 처리기가 실행되지 않기 때문에 이벤트를 발생시키는 것과 같지 않은 fyi도 있습니다.

이 접근

내가 어떻게 할 것인지의 그

Private Sub DataGridView1_CellClick(sender as object, e as DataGridViewCellEventArgs) 
    ProcessCellClick(e.RowIndex, e.ColumnIndex, true) 
End Sub 

Private Sub Button1_Click(...) 
    rowindex = DataGridView1.CurrentRow.Index 
    ProcessCellClick(rowIndex, 4, false) 
End Sub 

Private Sub ProcessCellClick(rowIndex as Integer, columnIndex as integer, fromCellClickEvent as boolean) 
    ... 
End Sub 

더 나은 방법입니다. 어쨌든 발신자를 확인하기 만하면 도움이 될 것입니다.

Private Sub DataGridView1_CellClick(sender as object, e as DataGridViewCellEventArgs) 
    if sender.Equals(DataGridView1) then 
     ... 
    else 
     ... 
    end if 
End Sub 

Another approach to check if an event (like TextBox.TextChanged) is initiatet by the user is this one 


Private Sub DataGridView1_CellClick(sender as object, e as DataGridViewCellEventArgs) 

    If me.ActiveControl Is DataGridView1 Then 
     ' DataGridView is focused 
    End If 

End Sub 
+0

흠 그 곳에서 나는 핸들러를 함수로 호출하는 것이 편리합니다. 그리고, 만약 내가 그렇게 생각한다면 아마도 가장 간단하고 유용한 것이 될 것이다. 하나의 공용 변수를 사용하면 _CellClick에서 "프로그램 클릭"을 datagridview에 알려줄 것이다. –

관련 문제