Winfoms Label appears on Form with no text.

DonBaechtel-7903 191 Reputation points
2023-01-22T20:42:46.02+00:00

I have a Winforms App in C# using VS 2022.

I have a Form1 that has a Label control on it and associated OpenFileDialog control.

In the code below, I put Text into the Label, Refresh the Label, Refresh the Form, cause the form to Show, Sleep for 5 secs, then cause the OpenFileDialog to pop up.

During the 5 sec Sleep, the Form and Label show up, but the label background is white with no text. The OpenFileDialog appears. Once the OpenFileDialog closes, the Form appears with the correct text in Black,

How can I make the Form with the Label, with the correct Text in Black appear before the 5-sec delay?

public Form1()
{
    InitializeComponent();
    label1.Text= "Please select file to Encrypt or Decrypt.";
    label1.Invalidate();
    label1.Refresh();
    this.Invalidate();
    this.Refresh();
    this.Show();
    Thread.Sleep(5000);
    openFileDialog1.ShowDialog();
}
Developer technologies | Windows Forms
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-01-22T22:40:12.36+00:00

    It's time to learn a bit about async/await. Instead of calling Thread.Sleep which blocks the UI thread, you can use await Task.Delay. For example, assuming you have the using statement:

    using System.Threading.Tasks;
    

    Then put the loading code, in the Load event handler, like this:

    private async void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "Something ...";
        await Task.Delay(5000);
        MessageBox.Show("After 5 seconds!");
    }
    

    Here you do not need to call refresh or update or invalidate, and you do not need it in constructor. Just remember to handle the load event (assign the event handler using designer or using code).

    Learn more


2 additional answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2023-01-22T22:20:41.4033333+00:00

    Don't use Sleep in the constructor

    If you want to display an OpenFileDialog after 5 seconds, you can use a Timer, like in the MSDN sample


  2. Abdulhakim M. Elrhumi 356 Reputation points
    2023-01-25T21:03:57.75+00:00

    Hi

    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace DesktopAppasync
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
           
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Thanks!");
            }
            private async void Form1_LoadAsync(object sender, EventArgs e)
            {
                textBox1.Text = "Do Something ...";
                await Task.Delay(5000);
                MessageBox.Show("After 5 seconds!");
            }
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.