c#Form.Show()不会在要显示的表单中绘制标签,ShowDialog()会这样做

问题描述 投票:0回答:3

我在WinForm上调用form1.Show()方法,其中只有一个标签,当标签必须显示的地方显示的形式在form1中显示一个洞(我可以看到form1下的形式)。如果我打电话给form1.ShowDialog()这个问题不适用,标签是可见的。透明度键设置为VisualStudio默认值。问题是我必须调用form1.Show()而不是ShowDialog(),因为调用者形式(mainForm)必须在后台工作,然后必须以编程方式关闭form1。关于什么可能是问题以及如何解决它的任何想法?

我的代码:mainWindows.cs

    private void mainWindow_FormClosing(object sender, FormClosingEventArgs e)
    {           
            DialogResult res = MessageBox.Show("Stai terminando la sessione, vuoi fare un backup?", "Fine Sessione", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
            { 
                 backupData(false);
            }
            else if(res == DialogResult.No)
            {
            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;
            closer cl = new closer(); //closer is Form1 in the question

            cl.Show();

            backupData(true);
            this.Enabled = true;
            cl.Close();

            this.Cursor = Cursors.Arrow;
            }
            else
               e.Cancel = true;
   }

closer.cs是一个简单的表单,由Visual Studio生成,只有一个标签由我通过WYSIWYG添加。

backupData()是一种将某些dirs压缩到文件的方法。

c# winforms show
3个回答
0
投票

试着打电话给Application.Run(new closer());而不是new closer().Show()


0
投票

我决定改变问题的方法,我将backupData()函数移植到closer形式,所以现在我可以通过backupData()调用BackgroundWorker并填充ProgressBar,所以现在我打电话给:

   closer cl = new closer();
   cl.ShowDialog();
   if(cl.GetResult())
      this.Close();

GetResult()返回一个bool,返回backupData()成功或失败。


0
投票

你需要给表单一些时间来绘制它的所有控件(除非你手动告诉它,否则它不会这样做,或者UI线程是空闲的)。你可以打电话给Refresh ...类似于:

 cl.Show();
 cl.Refresh();
 backupData(true);
 this.Enabled = true;
 cl.Close();
© www.soinside.com 2019 - 2024. All rights reserved.