开发者

The Click event on a PictureBox is firing but no action is performed

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 no开发者_如何学Got 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;
        }
    }
}


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();


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();
}


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();
}


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();

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜