0

小さな初心者の問題があります。ボタンクリック イベントがあり、そのイベントで form1_paint のペイント イベントを初期化する必要があります。私は 2 つのペイント イベントを持つクラス Oversteekplaats を持っています。1 つは teken1 と呼ばれ、もう 1 つは teken2 と呼ばれます。Form1_paint では teken1 は既に初期化されています。そして、Button1 をクリックすると変更されるブール値を介して、form1_paint を再描画します。

コード:

    public Form1()
{
    InitializeComponent();

    // Opdracht 1
    rood = new FietsLamp(Color.Red, 10, 10, 60);
    oranje = new VoetgangerLamp(Color.Orange, 120, 10, 60);
    rood.Status = LampStatus.Aan;
    oranje.Status = LampStatus.Aan;

    // Opdracht 2
    stoplicht = new Stoplicht(10, 120, 130, 280);

    // Opdracht 3
    voetgangerlicht = new VoetgangerStoplicht(150, 120, 130, 195);
    fietslicht = new FietsStoplicht(290, 120, 130, 195);

    // Opdracht 5 (teken oversteekplaats en zebrapad voor voetganger)
    oversteekplaats1 = new Oversteekplaats(650, 10, 200, 200);
    oversteekplaats2 = new Oversteekplaats(650, 210, 200, 200);
    oversteekplaats3 = new Oversteekplaats(650, 410, 200, 200);
    oversteekplaats4 = new Oversteekplaats(450, 210, 200, 200);
    oversteekplaats5 = new Oversteekplaats(850, 210, 200, 200);
}


private void button1_Click(object sender, EventArgs e)
{
    if (voetganger == false) 
    { 
        voetganger = true; 
    }
    if (voetganger == true) 
    { 
        voetganger = false;
    }

}

public void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Opdracht 1 graphics
    rood.Teken(e.Graphics);
    oranje.Teken(e.Graphics);

    // Opdracht 2 graphics
    stoplicht.Teken(e.Graphics);

    // Opdracht 3 graphics
    voetgangerlicht.Teken(e.Graphics);
    fietslicht.Teken(e.Graphics);

    // Opdracht 5 graphics
    oversteekplaats1.Teken1(e.Graphics);
    oversteekplaats2.Teken1(e.Graphics);
    oversteekplaats3.Teken1(e.Graphics);
    oversteekplaats4.Teken1(e.Graphics);
    oversteekplaats5.Teken1(e.Graphics);

    if (voetganger == true)
    {
    oversteekplaats4.Teken2(e.Graphics, 120);
    }
}
4

1 に答える 1

0

ボタンをクリックした後に描画を更新したい場合は、 を呼び出すだけInvalidate()です。

private void button1_Click(object sender, EventArgs e)
{
    if (voetganger == false) 
    { 
        voetganger = true; 
    }
    else if (voetganger == true) 
    { 
        voetganger = false;
    }

    this.Invalidate();
}

そして、少し思慮深くなるために、その条件を次のように書きます。

private void button1_Click(object sender, EventArgs e)
{
    voetganger = !(voetganger);
    this.Invalidate();
}
于 2013-10-31T15:02:36.487 に答える