简体   繁体   中英

c# Form.Show() does not paint labels in the form to show, ShowDialog() do it

I call form1.Show() method on a WinForm with only a label in it, when the form is showed at the place where the label must be is showed a hole in form1 (I can see the form under the form1). If I call form1.ShowDialog() this issue doesn't apply, and the label is visible. Transparency key is set to VisualStudio default. The problem is I have to call form1.Show() and not ShowDialog() because the caller form ( mainForm ) have to work in background, then have to close programmatically form1 . Any idea on what can be the problem and how to resolve it?

my code: 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 is a simple Form, generated by Visual Studio, with only one label added by me via WYSIWYG.

backupData() is a method that ZIPs some dirs to a file.

Try calling Application.Run(new closer()) ; instead of new closer().Show() .

I decisely changed approach to the problem, I ported the backupData() function into the closer Form, so now I can call backupData() trough a BackgroundWorker and also fill a ProgressBar , so now I call:

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

GetResult() returns a bool that returns backupData() success or failure.

You need to give time to the form to paint all its controls (it will not do so unless you manually tell it to, or the UI thread is idle). You can call Refresh ... something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM