riqitang
riqitang

Reputation: 3371

C# dynamic label blank

I created a very basic console app that executes some code behind the scenes. While it's doing this, it generates a custom message box (it's technically a Windows Form) that just displays one label telling the user to please wait. The issue is that the label NEVER is displayed (it's just a big white box where the label should be). I tried creating a dynamic label and putting it there, but this too does not work.

The form is shown in the following manner:

public void DoSomething()
{
   MyForm form = new MyForm();
   form.Show();
   try {
   // Execute other logic
   } finally { form.Close(); }
}

I'm guessing it has something to do with calling Show(), but I'm not positive. I put in logging and see that it's going through the constructor, generating the dynamic label (which is added to the form via Controls.Add(myLabel)), but it still does not show any label.

Upvotes: 1

Views: 530

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564363

A Form requires the message pump to be started in order to display correctly. This requires calling Application.Run. Unfortunately, this will block the current thread until the "application" is complete, so won't be able to just "drop in" to your code.

That being said, if you're developing an application which requires windows, you should consider making it a true windows application instead of a console application. You could just hide the main form, if required, and that will allow code like yours above to work properly.

Upvotes: 2

Related Questions