1

私はまだOOPを勉強しています。私は現在、好きな映画のポスターのボタンをクリックする映画チケットキオスクを作ろうとしています。Form1は、movibutton1、movibutton2、movibutton3という名前の3つのボタンが表示されるメインメニューフォームです。したがって、基本的に、ボタンの1つがクリックされたが、ボタンのイベントハンドラーが別のクラスにある場合は常に、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
    }
}
4

1 に答える 1

0

次のように、クラスにパブリックプロパティを追加できますderClassForForm1

public Form ParentForm {get; set;}

次に、メソッドのコードを変更できますbuttonPosters

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);
}

movPosterClickedこれにより、次の最後に次のようなものを書くことができます。

if(this.ParentForm != null)
{
     this.ParentForm.Hide();
}
于 2012-12-09T09:45:53.360 に答える