문제

I have this code

private void picturebox1_Paint(object sender, PaintEventArgs e)
    {
        if (Percent == 100)
        {
            e.Graphics.DrawString("Completed!", font, Brushes.Black, new Point(3, 2));
        }
    }

And I want to navigate if from here:

public void Complete()
{
    picRooms_Paint();//How can I reach picRooms_Paint from here and draw the string?
}

Any help would be appreciated!

도움이 되었습니까?

해결책

It's not clear from your example what picRooms is, so I can't tell you how to access its Graphics from elsewhere.

Though as a general API improvement, it would be better to move the functional code out of the event handler into a method where it can be re-used;

private void DrawComplete(Graphics g)
{
    if (Percent == 100)
    {
        g.DrawString("Completed!", font, Brushes.Black, new Point(3, 2));
    }
}

And now you can call that method from anywhere;

private void picRooms_Paint(object sender, PaintEventArgs e)
{
    DrawComplete(e.Graphics)
}

public void Complete()
{
    DrawComplete(this.CreateGraphics());
}

Also worth nothing that in your example, since Complete does nothing else, you might as well follow this example, but put the functional code in Complete, not add another method the way I have.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top