?
Solved

Why textbox  value not showing up immediately?

Posted on 2013-02-06
5
Medium Priority
?
387 Views
Last Modified: 2013-02-21
In my windows application the text box value not getting updated immediately. why ?

When i click the button2 i need to display given information in the text box.
But it is not displaying the value in the text box  instead it's  executing another method (below the text box )
and finally its displaying the value in the text box. Why the assigned values not showing in the text box immediately but it is executing another method ? I need to display the each every action to the user in the text box. How  can i do this ?


 private void button2_Click(object sender, EventArgs e)
        {
           
     
            textBox1.Text +=  Environment.NewLine + formatErrorMessage(" Connecting database....");

            ///getting  the values from db
            ftpValue = FtpValues();
   
                      assignFtpvalues(ftpValue);/// calling this method
         

            textBox1.Text +=  Environment.NewLine + formatErrorMessage(" getting version information ....");

}
0
Comment
Question by:Varshini S
  • 3
5 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38858576
private void button2_Click(object sender, EventArgs e)
        {
           
     
            textBox1.Text +=  Environment.NewLine + formatErrorMessage(" Connecting database....");


Application.DoEvents();  //Let the interface update by temporarily interrupting the code here

            ///getting  the values from db
            ftpValue = FtpValues();
   
                      assignFtpvalues(ftpValue);/// calling this method
         

            textBox1.Text +=  Environment.NewLine + formatErrorMessage(" getting version information ....");

}
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38858581
Explanation:
When you change the graphical interface the changes are shown only when the application is not busy.
When you execute a piece of code it runs from start to finish UNLESS you interrupt it somehow - the user interface is not updated.  

(The above assumes the code runs in the main thread along with the UI)
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38858582
Hi,

You could use a backgroundworker to do the db operation in its DoWork method. Actually it utilises another thread, so you may refresh your UI during its operation.

You may have a look at http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Giannis
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 38859303
If you're using a loop, then you'd need to call DoEvents() after each update to the TextBox within the loop.

For the BackgroundWorker(), you need to set WorkerReportsProgress() to True, call the ReportProgress() method, and update your TextBox from the ProgressChanged() event.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38915536
Any reason why my first comment instructing you to use DoEvents was not considered suitable ?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

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