0

I wrote code to paint a toggle button on the MouseClick event handled on a PictureBox using C# with a Windows Forms application. Here the click event is firing but the action is not being performed. Can anyone tell me what I'm doing wrong?

public partial class Form1 : Form
{
    bool flagarrow = false;

    public Form1()
    {
        InitializeComponent();

        pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);  

    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Point[] arrPoints = new Point[3];

        //Identify rectangle area filled by label.
        Rectangle lblBackground = (sender as Control).ClientRectangle;

        if (false == flagarrow)
        {
            //(x0,y0) for Triangle.
            arrPoints[0].X = lblBackground.Left + 5;

            arrPoints[0].Y = lblBackground.Top + 7;

            //(x1,y1) for Triangle.
            arrPoints[1].X = lblBackground.Left + 5;

            arrPoints[1].Y = lblBackground.Top + 17;

            //(x2,y2) for Triangle.
            arrPoints[2].X = lblBackground.Left + 14;

            arrPoints[2].Y = lblBackground.Top + 12;
        }
        else
        {

            //(x0,y0) for Triangle.
            arrPoints[0].X = lblBackground.Left + 5;

            arrPoints[0].Y = lblBackground.Top + 7;

            //(x1,y1) for Triangle.
            arrPoints[1].X = lblBackground.Left + 15;

            arrPoints[1].Y = lblBackground.Top + 7;

            //(x2,y2) for Triangle.
            arrPoints[2].X = lblBackground.Left + 10;

            arrPoints[2].Y = lblBackground.Top + 16;

        }

        //Fill the Triangle with Black Color. 
        e.Graphics.FillPolygon(Brushes.Black, arrPoints);
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

        if (flagarrow == false)
        {
            flagarrow = true;
        }
        else
        {
            flagarrow = false;
        }
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
SharpUrBrain
  • 3,180
  • 5
  • 38
  • 56

4 Answers4

4

Winforms has no reason to do anything special just because you changed a private field in your code. You have to tell it that conditions you use in the Paint event handler changed and a new paint is required. Make your Click event handler look like this:

 flagarrow = !flagarrow;
 pictureBox1.Invalidate();
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

The PictureBox.Click event is indeed being raised, and I suspect that the code in your event handler is running exactly as expected.

The problem is, all you do inside that event handler method is set the value of a variable (flagarrow). You haven't done anything that would cause the PictureBox control to repaint itself. Its Paint event is never triggered and thus its appearance remains unchanged.

The fix is simple: toss in a call to the Invalidate method. That will force the PictureBox control to redraw itself. And while we're at it, you might as well clean up your code a little.

Modify the code in your Click event handler as follows:

private void pictureBox1_Click(object sender, EventArgs e)
{
    flagarrow = !flagarrow;
    pictureBox1.Invalidate();
}
JoeyRobichaud
  • 2,689
  • 1
  • 15
  • 11
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • @Joe: No, you're right. It actually does work. I'm clearly trying to do *way* too many things at a time here. I approved your edit, but it takes two people. And your pending edit is blocking me from making the change myself. So hopefully it'll get a second thumbs up soon. But thanks for pointing that out. – Cody Gray - on strike Feb 04 '11 at 14:09
  • You ought to bring this up at meta, a pending edit approval shouldn't block you from editing your post. – Hans Passant Feb 04 '11 at 15:02
2

First be sure you are hooking the Click event. I see that this is a partial class so it may be in the designer code behind. Second try invalidating the picture box after they click it to force a refresh.

private void pictureBox1_Click(object sender, EventArgs e)
{

    if (flagarrow == false)
    {
        flagarrow = true;
    }
    else
    {
        flagarrow = false;
    }

    pictureBox1.Invalidate();
}
JoeyRobichaud
  • 2,689
  • 1
  • 15
  • 11
0

You need to just modify the picturebox click event as follows :

private void pictureBox1_Click(object sender, EventArgs e)
{
            if (flagarrow == false)
            { 
                flagarrow = true; 
            } 
            else 
            { 
                flagarrow = false;
            }
            //Add this following line to repaint the picture box.
            pictureBox1.Refresh();

}
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22