-1

目前我有一个图片框,上面有两个项目,一个处理 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();
    }
}

在执行时,它没有向我显示这三个项目。

4

1 回答 1

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

如果您只想让这三个控件仅在 backgroundworker 处于活动状态时显示,您可以将UpdateControls()-method 更改为:

private void UpdateControls(bool isVisable)
{
    pictureBox1.Visible = isVisable;
    picLoading.Visible = isVisable;
    label1.Visible = isVisable;
}
于 2013-04-15T12:33:26.023 回答