Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am a newbie, so it might seems a silly question.

I have a login form which calls other form where it says .. "please wait while it loads".

in form 2 i have a label for that.

Now the problem is that the text for that label doesnot appears until everything is executed in the login_click event.

Ex:
private void cmdLogin_Click(object sender, EventArgs e)
{
    frm2 objfrm = new frm2();
    frm2.show();

    // Manything to execute.
    
   frm2.close();
}


Now it loads form2 but the label text doesnot appears.

I removed frm2.close() and came to know that after it comes out of the cmdLogin_Click function, it the text appears.

I also tried frm2.showdialog(), which loads the text but the code stops at that line and doesnot move to next line in the function cmdLogin_Click.

Is there any solution for this?

Help would be appreciated.
Posted

1 solution

There is a solution, but it is not simple: you need to move the time intensive code out of your Click event, and into a separate thread. It's not to bad, but for a newbie, it may introduce a lot of concepts you haven't been taught yet, and don't understand.

There is a nasty, quick and dirty "solution" which may solve your problem, but I will probably upset a lot of people by recommending it - normally I would suggest avoiding the use, but it seems safer than trying to introduce you to threading and UI / background threads in particular.

If you must have this display, then in your time intensive code, call the Application.DoEvents method often. If you have a loop, call it at the end of each iteration round the loop. Like magic, your problem will vanish...but others will probably occur!

Why not do the logging in in your "objfrm" form instead?
 
Share this answer
 
Comments
Member 4581741 21-Mar-11 16:25pm    
Application.DoEvents() solved the problem.
Thanks
OriginalGriff 21-Mar-11 16:34pm    
It will, but please forget it as soon as you get on to threading: it is a very nasty solution to use and it really will cause problems if used inappropriately!
charles henington 21-Mar-11 20:14pm    
you get my 5 Griff very true when i first started programming I used the Application.DoEvents() when trying to do a conversion of a byte[] to a encryption method that required a string input it blew right by the error but the output was not pretty :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900