• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Making a label visible is not working if Excel application starts after that.

Hi, I have a C# application that creates and populates an Excel file.  This process is initiated by clicking a button and takes a few seconds.
I want to display a message "Please wait..." until the Excel file is created and populated.

So I have in the code something like this:

private void btnUpdateA_Click(object sender, System.EventArgs e)
{
      lblWait.Visible = true;
      lblWait.Text = "Please Wait...";
                btnNext.Enabled = false;

      oXL = new Excel.Application();

      //Get a new workbook.
      oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
      oSheetP = (Excel._Worksheet)oWB.Worksheets[1];
               . . . . . . . . . . . . . . . . . .
               // and after the file is populated
      lblWait.Visible = false;
                btnNext.Enabled = true;

}

The Excel file is created and populated, but the label remains invisible all the time.
The btnNext button (see code) becomes enabled when the process is complete so I can see the start and end of the process.
But the label lblWait remains invisible.

Please note that if I place a message box (MessageBox.Show ("Clicked");) after the statement (lblWait.Visible = true;), then the label becomes visible until the file is created.
I tried to replace the message box statement by some time consuming code (Thread.Sleep(1000);) but that did not work as well.

Is there some threading involved that prevents the label from becoming visible and how can I make the label visible in this case?

Thank you.

0
pavelmed
Asked:
pavelmed
  • 8
  • 7
1 Solution
 
dstanley9Commented:
You need to tell the form to repaint.  

Try this:

     lblWait.Text = "Please Wait...";
     btnNext.Enabled = false;
     lblWait.Invalidate();
0
 
pavelmedAuthor Commented:
This did not work.  The label remained invisible.
0
 
dstanley9Commented:
How about repainting the whole form?

     lblWait.Text = "Please Wait...";
     btnNext.Enabled = false;
     this.Invalidate();
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
pavelmedAuthor Commented:
It still does not work.  The label remains invisible.
0
 
dstanley9Commented:
What about changing the cursor?  

private void btnUpdateA_Click(object sender, System.EventArgs e)
{
     lblWait.Visible = true;
     lblWait.Text = "Please Wait...";
     btnNext.Enabled = false;
     this.Invalidate();

     this.Cursor = Cursors.WaitCursor;

     oXL = new Excel.Application();

     //Get a new workbook.
     oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
     oSheetP = (Excel._Worksheet)oWB.Worksheets[1];
               . . . . . . . . . . . . . . . . . .
               // and after the file is populated
     lblWait.Visible = false;
                btnNext.Enabled = true;

     this.Cursor = Cursors.Default;


}
0
 
dstanley9Commented:
also try

this.Invalidate(true);
0
 
pavelmedAuthor Commented:
this.Invalidate(true); did not help.
But at least I got a "clock" cursor after clicking on the btnUpdateA button.  It does show that something is going on.  Thank you for this.
However it would be great to make the label visible issue as well.
0
 
dstanley9Commented:
For some reason, the form is not being re-painted.  Invalidate() should do it; not sure why it's not in this case.

This may sound weird, but if you move the form while it is processing, does it repaint?
0
 
pavelmedAuthor Commented:
I did a couple of experiments.  I added another label and a button to the form and made them invisible, and I added a code to make them visible just like for that lblWait label - they remained invisible.

Then I did it vise versa: I made them initially visible, and added a code to make them invisible while Excel file was being created and populated - same code, just switched true and false.  Guess what: it worked.  First they became invisible, then visible again.

So something is just preventing the controls from becoming visible but allows them to become invisible.
0
 
dstanley9Commented:
Very strange.    

Another test that may sound weird...

try switching the Visible and Text setting lines:

     lblWait.Text = "Please Wait...";
     lblWait.Visible = true;

Also, try this (OK, it's an ugly hack, but it may work)

     lblWait.Text = "Please Wait...";
     lblWait.Visible = false;
     this.Invalidate(true);
     lblWait.Visible = true;
     this.Invalidate(true);

0
 
pavelmedAuthor Commented:
No, this did not help.
0
 
dstanley9Commented:
What about using the Refresh() method instead of Invalidate()?  
0
 
pavelmedAuthor Commented:
Another detail: the label can become visible after (AFTER!) the Excel book is created although the statement lblWait.Visible = true; is placed before the code to create the Excel.
0
 
dstanley9Commented:
My guess is since the Excel code is synchronous, the paint message is not handled on the form until after excel is through.  What's strange is that it works one way (visible-->invisible) but not the other.  Refresh is supposed to synchronously re-paint the control (basically calling Invalidate() followed by Update().
0
 
pavelmedAuthor Commented:
I am going to close this question.  Although I could not make the label visible in my scenario, I could get around by changing the cursor to a "Clock" cursor for the same time I needed the Wait label to be displayed.
Since this was suggested by dsgtanley9, the points are his.
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now