2013-07-25 5 views
2

저는 C# windows 응용 프로그램을 만들고 있습니다. 내 응용 프로그램은 사용자 정의 컨트롤 라이브러리에서 컨트롤 (단추, 텍스트 상자, 서식있는 텍스트 상자 및 콤보 상자 등)을 가져 와서 런타임에 폼에 동적으로 배치합니다. 대리자를 사용하여 해당 컨트롤에 대한 이벤트 처리기를 만드는 방법은 무엇입니까? 특정 사용자 정의 컨트롤 클릭 이벤트에서 비즈니스 로직을 추가하는 방법은 무엇입니까? 예를 들어런타임에 동적으로 생성 된 컨트롤에 이벤트 처리기를 추가하는 방법은 무엇입니까?

: USER1 내가에만 표시 할 로그인 할 때

내가 사용자 1, 사용자 2, 사용자 3, '저장'버튼을했다. user2는 "추가 및 삭제"버튼 만 표시하고 사용자 3은 "추가 및 업데이트"버튼을 표시합니다. 텍스트 상자 및 DB 테이블에서 가져온 정보에서 사용자 로그인 정보로 생성 된 버튼입니다.이 시나리오에서는 다른 이벤트 (추가, 저장, 추가, 양식을 동적으로 저장, 추가, 그것은 두 번 추가거야)

답변

3
var t = new TextBox(); 
t.MouseDoubleClick+=new System.Windows.Input.MouseButtonEventHandler(t_MouseDoubleClick); 

private void t_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    throw new NotImplementedException(); 
} 

를 삭제하고 업데이트 버튼 객체가 같은 버튼 클래스에서이다 (컨트롤을 만들 때 삭제하고 다른 사용자 용 업데이트 버튼), 업데이트, 삭제 저장 익명의 방법으로 새 ​​텍스트 상자

3

에 이벤트 핸들러를 클릭

Button button1 = new Button(); 
button1.Click += delegate 
        { 
         // Do something 
        }; 
방법으로

: 당신이 MSDN Documentation에서 찾을 수

Button button1 = new Button(); 
button1.Click += button1_Click; 

private void button1_Click(object sender, EventArgs e) 
{ 
    // Do something 
} 

추가 정보.

1

난 당신이 같은 일을 할 수 있으리라 생각합니다 :

if (userCanAdd) 
    container.Controls.Add(GetAddButton()); 
if (userCanUpdate) 
    container.Controls.Add(GetUpdateButton()); 
if (userCanDelete) 
    container.Controls.Add(GetDeleteButton()); 

private Button GetAddButton() { 
    var addButton = new Button(); 
    // init properties here 

    addButton.Click += (s,e) => { /* add logic here */ }; 
    // addButton.Click += (s,e) => Add(); 
    // addButton.Click += OnAddButtonClick; 

    return addButton; 
} 

private void OnAddButtonClick (object sender, EventArgs e) { 
    // add logic here 
} 

// The other methods are similar to the GetAddButton method. 
관련 문제