【问题标题】:The Click event on a PictureBox is firing but no action is performedPictureBox 上的 Click 事件正在触发,但未执行任何操作
【发布时间】:2011-02-04 12:58:00
【问题描述】:

我编写了代码来在 PictureBox 上使用 C# 和 Windows 窗体应用程序处理的 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;
        }
    }
}

【问题讨论】:

  • 你可以从 temp 按钮执行操作吗?

标签: c# .net winforms events picturebox


【解决方案1】:

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

 flagarrow = !flagarrow;
 pictureBox1.Invalidate();

【讨论】:

    【解决方案2】:

    首先确保您正在挂钩 Click 事件。我看到这是一个部分类,所以它可能在后面的设计器代码中。第二次尝试让图片框失效,点击后强制刷新。

    private void pictureBox1_Click(object sender, EventArgs e)
    {
    
        if (flagarrow == false)
        {
            flagarrow = true;
        }
        else
        {
            flagarrow = false;
        }
    
        pictureBox1.Invalidate();
    }
    

    【讨论】:

      【解决方案3】:

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

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

      解决方法很简单:调用Invalidate method这将强制PictureBox 控件重绘自身。在我们进行此操作时,您不妨稍微清理一下您的代码。

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

      private void pictureBox1_Click(object sender, EventArgs e)
      {
          flagarrow = !flagarrow;
          pictureBox1.Invalidate();
      }
      

      【讨论】:

      • @Joe:不,你是对的。它确实有效。我显然想在这里一次做太多的事情。我批准了你的编辑,但需要两个人。而且您的待定编辑阻止我自己进行更改。所以希望它很快就会得到第二个赞许。但感谢您指出这一点。
      • 你应该在 meta 上提出这个问题,待定的编辑批准不应该阻止你编辑你的帖子。
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-02-08
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多