Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top