Solved

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

Posted on 2006-06-20
15
210 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split string on commas but not when enclosed in parentheses 7 48
Angular JS Route 3 54
Angular - break the dataset in chunks? 7 39
ASP.NET 5 Templates 2 66
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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now