|
-
January 26th, 2009, 02:05 PM
#1
[RESOLVED] Modify .TEXT property from Event Handler
I'm working with :
Visual Studio 2008, C#, .NET FX 3.5
I am building a C# Winform application.
In the event handler for a button on the form, I wish to change the .Text property of 2 textboxes on the form, call an external program (pkzip to do a backup), and then change the .Text property of the textboxes again (to signal start and completion of the backup)?
It seems that the controls (textboxes) on the form only get updated when the event handler exits.
How can I change the .TEXT property of the controls on the form while the event handler is still running?
Here's my event handler :
private void btnGo_Click(object sender, EventArgs e)
{
string sJobName = "";
string sOut = "";
int iTmp = 0;
iTmp = dataGridView1.CurrentRow.Index;
sJobName = dataGridView1.Rows[iTmp].Cells[0].Value.ToString();
// Set text boxes for start of job
txtStatus.Text = "Started ...";
textBox1.Text = "";
// Run the Backup Job
MyUtils.buildBackupParams(sJobName,butype.gui);
sOut = Utils.callBackup();
// Display "Done" & output of backup job
textBox1.Text = sOut;
txtStatus.Text = "Done!";
}
Thank you!
Steve
-
January 26th, 2009, 02:41 PM
#2
Re: Modify .TEXT property from Event Handler
First, please use code tags 
On your question, I'm sure that if you were to comment out everything after the first text change, you'd see that it is being updated. I'm guessing that the backup is processing very quickly, or it processes in the background and your application does not care if it's finished and continues on to it's next line of code. There's nothing in this function that forces the application to wait until the backup is complete, therefore your text property is being updated twice nearly instantly.
My question would be is the line
Code:
textBox1.Text = sOut;
functioning correctly? i.e. are you getting the output of your backup or is that not appearing either?
-
January 26th, 2009, 02:47 PM
#3
Re: Modify .TEXT property from Event Handler
Try this:
Code:
// Set text boxes for start of job
txtStatus.Text = "Started ...";
textBox1.Text = "";
txtStatus.Refresh();
textBox1.Refresh();
// and for good measure
System.Threading.Thread.Sleep(5000);
// Run the Backup Job
MyUtils.buildBackupParams(sJobName,butype.gui);
sOut = Utils.callBackup();
// Display "Done" & output of backup job
textBox1.Text = sOut;
txtStatus.Text = "Done!";
I think yraen might be right. Perhaps your backup program is just finishing very quickly and you dont see the intial textbox updates. Try the thread.sleep like I showed you for 5 seconds and I bet you see them update.
Last edited by Traps; January 26th, 2009 at 02:54 PM.
-
January 26th, 2009, 02:56 PM
#4
Re: Modify .TEXT property from Event Handler
Thank you "Traps" -- the .Refresh() did the trick!
I figured it was something simple - and it was!
Many blessings on you for your help!!
And to "yraen" - my thanks to you for taking the time to respond. The backup was returning its output correctly - but what I needed was to clear the output from a prior run and change the txtStatus control to show that the job had started.
Steve
-
January 26th, 2009, 10:51 PM
#5
Re: Modify .TEXT property from Event Handler
 Originally Posted by Traps
// and for good measure
System.Threading.Thread[SIZE=2].Sleep(5000);
Try the thread.sleep like I showed you for 5 seconds and I bet you see them update.
No no no. Don't use the Sleep() like he showed you. Infact, don't use Sleep() at all, and certainly never ever on a UI thread. Sleeping the UI thread stops the message pump, causign the app to become "(Not Responding)" and is actually a great way of ensuring that your text boxes dont update properly.
That's some terrible advice Traps. You should read this:
http://msmvps.com/blogs/peterritchie...d-program.aspx
Then edit the nonsense out of your post..
Lastly, OP, if youre doing some long running, intensive op, don't do it on the UI thread (in a button click event handler) - use a backgroundworker! Youre only going to hang your app if you tie the UI thread up for long periods of time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|