Solved

Why textbox  value not showing up immediately?

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now