0

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!

David Mulder
  • 26,123
  • 9
  • 51
  • 114
Emalton
  • 5
  • 3
  • Why don't you set the `Text` instead? unless you want some kind of custom drawn string. By the way, what is your `picRooms`? a pictureBox? – King King Jun 27 '13 at 23:20
  • What you probably want to do is invalidate picRooms so that the Paint event is initiated. I think it is something like picRooms.Invalidate(); – Eric J. Jun 27 '13 at 23:21
  • 1
    How are these methods implemented? Are they in the same class? Also, your method call `picRooms_Paint()` **will not work** in the example above. Initially you were passing it two overloads `object sender, PaintEventArgs e`, and aren't following that same pattern in the second method. – Brian Jun 27 '13 at 23:22
  • I have a certain font that isn't pre-installed on C#. And yes, it is a picturebox. – Emalton Jun 27 '13 at 23:58
  • They are both in Form1. My code can be found [here](http://pastebin.com/Nnwjih3z) – Emalton Jun 28 '13 at 00:04
  • @EricJ.'s `Invalidate()` should work, as well as `picRooms.Refresh()`. Just make sure the main UI thread isn't stuck in any tight loops that would prevent it from updating itself... – Idle_Mind Jun 28 '13 at 02:43

1 Answers1

0

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.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • Amazing idea, then I can just load the graphics into the picturebox! Thanks so much! – Emalton Jun 27 '13 at 23:35
  • I think you'll find this is more complicated when you get into it; the PictureBox may just redraw itself when the form is painted, depending on what other code you've got going on. – RJ Lohan Jun 27 '13 at 23:37
  • Yea I'm having trouble loading the graphics into the picturebox. I want it to keep trying until the percent is at 100 if possible. – Emalton Jun 27 '13 at 23:51