Solved

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

Posted on 2006-06-20
15
200 Views
Last Modified: 2010-04-16
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
Comment
Question by:pavelmed
  • 8
  • 7
15 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16943009
You need to tell the form to repaint.  

Try this:

     lblWait.Text = "Please Wait...";
     btnNext.Enabled = false;
     lblWait.Invalidate();
0
 

Author Comment

by:pavelmed
ID: 16944990
This did not work.  The label remained invisible.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16945091
How about repainting the whole form?

     lblWait.Text = "Please Wait...";
     btnNext.Enabled = false;
     this.Invalidate();
0
 

Author Comment

by:pavelmed
ID: 16945931
It still does not work.  The label remains invisible.
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 16952966
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 16952973
also try

this.Invalidate(true);
0
 

Author Comment

by:pavelmed
ID: 16953980
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 25

Expert Comment

by:dstanley9
ID: 16954007
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
 

Author Comment

by:pavelmed
ID: 16954224
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 16954365
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
 

Author Comment

by:pavelmed
ID: 16955159
No, this did not help.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16955232
What about using the Refresh() method instead of Invalidate()?  
0
 

Author Comment

by:pavelmed
ID: 16955424
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 16955566
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
 

Author Comment

by:pavelmed
ID: 16971590
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now