2009-07-03 2 views
5

게시물을 THIS으로 읽어보십시오. 이 게시물에 설명 된 것과 동일한 문제가 있지만 VB.net 대신 C#으로 작업하려고합니다.vb.net winforms 사용자 정의 컨트롤에 대한 사용자 지정 이벤트를 표시하고 발생시키는 방법

나는 사용자 지정 이벤트를 사용해야한다는 것을 확신합니다. (. 나는 사용자 정의 이벤트에 대해 배우고 얻을 수있는 code conversion site을 사용) 그래서 IDE에서 나는 다음 입력 할 때 : 그것은 다음과 같은 코드로 확장

이벤트 핸들러

으로

공공 사용자 정의 이벤트 AddRemoveAttendees.

Public Custom Event AddRemoveAttendees As EventHandler 
    AddHandler(ByVal value As EventHandler) 

    End AddHandler 

    RemoveHandler(ByVal value As EventHandler) 

    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs) 

    End RaiseEvent 
End Event 

하지만 어떻게해야할지 모르겠다. 오늘까지는 맞춤 이벤트에 대해 들어 본 적이 없었습니다.

내가 원하는 것은 사용자 컨트롤의 컨테이너까지 버튼 버블의 클릭 이벤트를 갖는 것입니다. 나는 내 자신의 이벤트를 감쌀 수 있다는 것을 알고 있지만 적어도 내가 길을 떠나기 전에 맞춤 이벤트를 이해하고 싶습니다. 당신은 단순히 첨부하거나 지정된 이벤트를 제거하기 위해 전화를 전파 RemoveHandlerAddHandler에서

Public Custom Event AddRemoveAttendees As EventHandler 
    AddHandler(ByVal value As EventHandler) 
     AddHandler _theButton.Click, value 
    End AddHandler 

    RemoveHandler(ByVal value As EventHandler) 
     RemoveHandler _theButton.Click, value 
    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs) 
     ' no need to do anything in here since you will actually ' 
     ' not raise this event; it only acts as a "placeholder" for the ' 
     ' buttons click event ' 
    End RaiseEvent 
End Event 

과 :

세스

답변

7

이 다른 컨트롤의 이벤트를 버블 링에 대한 사용자 정의 이벤트를 사용하려면 다음과 같이 할 수있다 처리기 Click 이벤트로 /에서 이벤트 처리기. 그것은 위에서 보이는

Dim _handlers As New List(Of EventHandler) 
Public Custom Event AddRemoveAttendees As EventHandler 

    AddHandler(ByVal value As EventHandler) 
     _handlers.Add(value) 
    End AddHandler 

    RemoveHandler(ByVal value As EventHandler) 
     If _handlers.Contains(value) Then 
      _handlers.Remove(value) 
     End If 
    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs) 
     For Each handler As EventHandler In _handlers 
      Try 
       handler.Invoke(sender, e) 
      Catch ex As Exception 
       Debug.WriteLine("Exception while invoking event handler: " & ex.ToString()) 
      End Try 
     Next 
    End RaiseEvent 
End Event 

는 일반 이벤트 선언보다 작은 다른 않습니다, 지금 :

사용자 정의 이벤트의 사용에 조금 확장하려면, 여기에 사용자 정의 이벤트의 또 다른 샘플 구현입니다

Public Event AddRemoveAttendees As EventHandler 

이벤트 핸들러를 연결 및 제거하고 이벤트를 발생시킬 수있는 유사한 메커니즘을 제공합니다. Custom 이벤트가 추가하는 것은 추가 제어 수준입니다. 규칙을 시행 할 수있는 이벤트 추가, 제거 및 제기와 관련된 코드를 작성하고 약간의 일이 발생할 수있는 상황을 조정할 수 있습니다. 예를 들어, 이벤트에 첨부 된 이벤트 핸들러의 수를 제한하려고 할 수 있습니다. 위의 예제에서 AddHandler 섹션을 변경할 수 있음을 달성하기 위해 :

AddHandler(ByVal value As EventHandler) 
     If _handlers.Count < 8 Then 
      _handlers.Add(value) 
     End If 
    End AddHandler 

당신이 세부 컨트롤의 종류를 필요로하지 않는 경우에, 나는 사용자 정의 이벤트를 선언 할 필요는 볼 수 없습니다. 당신이 했나요 다른 포스트에서 같은 일을하려면

5

, 여기에 VB.NET 동등한입니다 :

Public Custom Event AddRemoveAttendees As EventHandler 

    AddHandler(ByVal value As EventHandler) 
     AddHandler SaveButton.Click, value 
    End AddHandler 

    RemoveHandler(ByVal value As EventHandler) 
     RemoveHandler SaveButton.Click, value 
    End RemoveHandler 

End Event 

하지만 좋은 생각이라고 생각하지 않는 이벤트의 sender 매개 변수 때문에 대신 Button, 아닌 UserControl ...

될 것입니다, 당신은 Button.Click 이벤트를 구독 할 수 있고, 당신의 대답 (명시적인 접근으로) 당신에게

+0

감사를 자신의 이벤트를 발생 ... 내가 준 fi에 대답 플래그 첫 번째 게시물은 철저하지만 귀하의 답변을 upvoted. 사용자 정의 컨트롤이 사용자의 제안에 따라 chilc 컨트롤 대신 이벤트를 방출 할 수 있도록 컨트롤을 표준 대리자 및 컨트롤의 이벤트로 래핑하기로 결정했습니다. 감사. 세스 –

관련 문제