PictureBox 上的 Click 事件正在触发,但未执行任何操作
我编写了代码,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Winforms 没有理由仅仅因为您更改了代码中的私有字段而执行任何特殊操作。您必须告诉它您在 Paint 事件处理程序中使用的条件已更改并且需要新的绘制。让您的 Click 事件处理程序如下所示:
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:
首先确保您正在挂钩 Click 事件。我发现这是一个部分类,因此它可能位于后面的设计器代码中。第二次尝试在单击图片框以强制刷新后使其无效。
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.
PictureBox.Click
事件确实正在引发,我怀疑事件处理程序中的代码完全按照预期运行。问题是,您在该事件处理程序方法中所做的只是设置变量的值 (
flagarrow
)。 您没有执行任何会导致PictureBox
控件重新绘制自身的操作。它的Paint
事件永远不会被触发,因此其外观保持不变。修复方法很简单:调用
Invalidate< /code> 方法
。
这将强制
PictureBox
控件重绘自身。当我们这样做时,您不妨稍微清理一下您的代码。修改
Click
事件处理程序中的代码,如下所示: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 thePictureBox
control to repaint itself. ItsPaint
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 thePictureBox
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:您只需修改图片框点击事件如下:
You need to just modify the picturebox click event as follows :