Solved

Why textbox  value not showing up immediately?

Posted on 2013-02-06
5
377 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 44

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 44

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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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 44

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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