创建的Winusercontrol由1个表单和1个面板组成。 在用户控件面板的绘制事件上绘制一些文本。 在我的应用程序中添加用户控件的引用。
用户控制代码:
bool flag=false;
public void Somthing()
{
///some code...
///
flag=true;
}
protected void panel_paint(PainteventArgs e)
{
if(flag==true)
//draw some text
}
表单申请代码:
private void frmMain_Load(object sender, EventArgs e)
{
usercontrol obj=new usercontrol();
obj.Somthing();
}
当我创建库对象并调用 Somthing() 时。 当检查绘制事件标志值时将为 false。 所以不会绘制任何文字 我不知道为什么标志值会被重置。
最佳答案
也许 Paint 事件在表单加载事件之前触发,因此您的标志为 false,您将永远无法添加文本。
尝试添加 Invalidate()到包含面板的用户控件,以便它将重新绘制。
public void Somthing()
{
flag=true;
this.Invalidate();
//EDIT: it couldn't be synchronous so force with Update that sends WM_PAINT event
this.Update();
}
关于c# - 标志值将在用户控件绘制事件时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16768771/