Solved

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

Posted on 2006-06-20
15
216 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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
 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Object Reference is Required for the non-static field, method, or property 10 35
C# Gridview 1 33
transaction in asp.net, sql server 6 33
tableview is not updating 1 10
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

821 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