目前我有一个图片框,上面有两个项目,一个处理gif文件和一个标签。现在,当调用backgroundworker时,我已经为所有三个项目(图片框,处理gif,标签)调用了BringToFront()
以下是我的后台工作人员的代码片段
private void buttonUpload_Click(object sender, EventArgs e)
{
LoadFile(pdfFullPath);
bgwLoadFile.RunWorkerAsync(dummyPDFPath);
pictureBox1.BringToFront();
picLoading.BringToFront();
label.BringToFront();
}
private void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
{
this.Invoke((MethodInvoker)delegate() {
acrPDFViewer.LoadFile(e.Argument.ToString());
});
}
private void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
}
else if (e.Error != null)
{
}
else
{
pictureBox1.SendToBack();
picLoading.SendToBack();
label.SendToBack();
}
}
执行时无法向我显示这三个项目。
答案 0 :(得分:3)
这对我有用:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UpdateControls(false);
bgwLoadFile.DoWork += new DoWorkEventHandler(bgwLoadFile_DoWork);
bgwLoadFile.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwLoadFile_RunWorkerCompleted);
}
private void buttonUpload_Click(object sender, EventArgs e)
{
UpdateControls(true);
bgwLoadFile.RunWorkerAsync();
}
void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
{
//simulate work
System.Threading.Thread.Sleep(2000);
}
void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(!e.Cancelled && e.Error == null)
UpdateControls(false);
}
private void UpdateControls(bool isVisable)
{
if (isVisable)
{
pictureBox1.BringToFront();
picLoading.BringToFront();
label1.BringToFront();
}
else
{
pictureBox1.SendToBack();
picLoading.SendToBack();
label1.SendToBack();
}
}
}
如果你想做的只是在背景工作者处于活动状态时才显示这三个控件,你可以将UpdateControls()
- 方法更改为:
private void UpdateControls(bool isVisable)
{
pictureBox1.Visible = isVisable;
picLoading.Visible = isVisable;
label1.Visible = isVisable;
}