2012-12-09 1 views
1

나는 여전히 OOP를 공부하고있다. 나는 현재 당신이 좋아하는 영화의 포스터 버튼을 클릭하는 영화 티켓 간이 건축물을 만들고자 노력 중이다. Form1은 다음과 같이 3 개의 단추가 표시되는 기본 메뉴 양식입니다. movibutton1, movibutton2, movibutton3. 그래서 기본적으로 버튼 중 하나가 클릭 될 때마다 Form1을 숨기고 싶었지만 버튼의 이벤트 핸들러는 다른 클래스에 있습니다. 이벤트 처리기가 클래스에 남아 있기를 원했습니다.이벤트 처리기 내부에서 양식을 닫을 수 있습니까? 여기서 이벤트 처리기는 클래스에 있습니까?

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     buttonPosters(); 
    } 
    private void buttonPosters() 
    { 
     derClassForForm1 classForm1 = new derClassForForm1(); 
     moviButton1.Click += new EventHandler(classForm1.movPosterClicked); 
     moviButton2.Click += new EventHandler(classForm1.movPosterClicked); 
     moviButton3.Click += new EventHandler(classForm1.movPosterClicked); 
    } 
} 

public class derClassForForm1 
{ 
    public void movPosterClicked(object sender, EventArgs e) 
    { 
     Button posterClick = (Button)sender; 
     if (posterClick.Name.Equals("moviButton1")) 
     { 
      Mov1 movie = new Mov1(); 
      Form2 movPoster = new Form2(movie.movTitle(), movie.movSynop(), movie.movImagesrc()); 
      movPoster.Show(); 
     } 
     else if (posterClick.Name.Equals("moviButton2")) 
     { 
      Mov2 movie = new Mov2(); 
      Form2 movPoster = new Form2(movie.movTitle(), movie.movSynop(), movie.movImagesrc()); 
      movPoster.Show(); 
     } 
     else if (posterClick.Name.Equals("moviButton3")) 
     { 
      Mov3 movie = new Mov3(); 
      Form2 movPoster = new Form2(movie.movTitle(), movie.movSynop(), movie.movImagesrc()); 
      movPoster.Show(); 
     } 

     //wanted to have like a form.hide() here 
    } 
} 

답변

0

당신은 당신과 같은 클래스 derClassForForm1에 공용 속성을 추가 할 수 있습니다

private void buttonPosters() 
{ 
    derClassForForm1 classForm1 = new derClassForForm1(); 
    classForm1.ParentForm = this; 
    moviButton1.Click += new EventHandler(classForm1.movPosterClicked); 
    moviButton2.Click += new EventHandler(classForm1.movPosterClicked); 
    moviButton3.Click += new EventHandler(classForm1.movPosterClicked); 
} 

이 할 수 있습니다 것입니다 :

public Form ParentForm {get; set;} 

그런 다음 당신이 방법 buttonPosters 당신의 코드를 수정할 수 있습니다 movPosterClicked 끝 부분에 다음과 같이 씁니다.

if(this.ParentForm != null) 
{ 
    this.ParentForm.Hide(); 
}