Solved

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

Posted on 2006-06-20
15
220 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
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.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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