PictureBox 上的 Click 事件正在触发,但未执行任何操作

发布于 2024-10-15 21:34:08 字数 1879 浏览 3 评论 0原文

我编写了代码,使用 C# 和 Windows 窗体应用程序在 PictureBox 上处理的 MouseClick 事件上绘制切换按钮。这里点击事件正在触发,但操作并未执行。谁能告诉我我做错了什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千紇 2024-10-22 21:34:08

Winforms 没有理由仅仅因为您更改了代码中的私有字段而执行任何特殊操作。您必须告诉它您在 Paint 事件处理程序中使用的条件已更改并且需要新的绘制。让您的 Click 事件处理程序如下所示:

 flagarrow = !flagarrow;
 pictureBox1.Invalidate();

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();
千と千尋 2024-10-22 21:34:08

首先确保您正在挂钩 Click 事件。我发现这是一个部分类,因此它可能位于后面的设计器代码中。第二次尝试在单击图片框以强制刷新后使其无效。

private void pictureBox1_Click(object sender, EventArgs e)
{

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

    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();
}
十年不长 2024-10-22 21:34:08

PictureBox.Click 事件确实正在引发,我怀疑事件处理程序中的代码完全按照预期运行。

问题是,您在该事件处理程序方法中所做的只是设置变量的值 (flagarrow)。 您没有执行任何会导致 PictureBox 控件重新绘制自身的操作。它的 Paint 事件永远不会被触发,因此其外观保持不变。

修复方法很简单:调用 Invalidate< /code> 方法这将强制 PictureBox 控件重绘自身。当我们这样做时,您不妨稍微清理一下您的代码。

修改 Click 事件处理程序中的代码,如下所示:

private void pictureBox1_Click(object sender, EventArgs e)
{
    flagarrow = !flagarrow;
    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();
}
回梦 2024-10-22 21:34:08

您只需修改图片框点击事件如下:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文