2017-09-29 6 views
0

userControl에서 양식으로 이벤트를 사용하려고했지만 폼 생성자에서 이벤트를 만들 때 문제가 발생했습니다. 나는 어디서 실패했는지 모른다. 내 코드가있다.Field System.MulticastDelegate._invocationCount is not available C#

UserControl을

public GameField() 
    { 
     InitializeComponent(); 
     button.Click += Button_Clicked; 
    } 

public event EventHandler ButtonClicked; 
private void Button_Clicked(object sender, EventArgs e) 
    { 
     if (this.ButtonClicked != null) this.ButtonClicked(sender, e); 
    } 

양식

GameField gameField = new GameField(); //Instance of the derived class UserControl  

    public Form1() 
    {    
     InitializeComponent(); 
     gameField.ButtonClicked += new EventHandler(this.btn_Click); 
    } 

    private void btn_Click(object sender, EventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 

이 문제가 enter image description here

+0

사용자 컨트롤의 버튼을 클릭하면 어떻게됩니까? – praty

+0

'void Button_Clicked'를 참조합니다. –

+0

내가 'btn_Click'을 치지 않았 냐고 물어 보려고 했습니까? – praty

답변

0

난 당신이 GameFieldButton_Clicked 대신 ButtonClicked에 가입하고 싶어 생각이있다.

button.Click += Button_Clicked; 

내가 볼

편집, 난 당신이 GameField의 두 인스턴스를 직감이있다. 양식 디자이너를 통해 추가 한 양식은 gameField1이고 양식에 코드로 추가 한 양식은 gameField입니다.

Form1.Designer.cs을 열면 gameField1이 표시 될 수 있습니까 (또는 디자이너를 통해 추가 할 때 부여한 이름이 무엇이든간에)?

는 다음과 같은 시도 할 수

gameField1.ButtonClicked += new EventHandler(btn_Clicked); // name can be other than gameField1, gameField1 is just the automatically generated name by VS 
+0

그래야하지만, 입력하는 동안 미안 해요. 그러나 문제는 여전히 동일합니다 ... –

+0

놀라워요! 너는 아주 많이! 이제 제대로 작동합니다 ... :) –