We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

C#  section of form / textbox that automatically updates when data changes

SSschultz
SSschultz asked
on
Medium Priority
701 Views
Last Modified: 2012-05-11
Hi,
Need some help with the following:

How do you make a part of a form, like textbox, automatically update when new data has changed. I've tried   This.refresh() and looping and using a Thread.Sleep in the form is not available.
I think I need to have a seperate Thread with Sleep or Timer running in the program that is
referenced back to the form??

Need some simple code examples of solution.
thanks
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
automatically update when new data has changed.
What do you mean by this? Can you provide and example?

Author

Commented:
If data changes that is written to say a streamwriter.
When streamwriter has new data it will automatically refresh on a textbox on a form.
I can get it to work if a example of the date and time when I click a event button, but
would like part of the form / textbox to automatically populate with new data as it's written from another part of program from streamwriter.

In short, when certain conditions are met, I want to view them on a form without having to click a button.

Hope this helps.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Well StreamWriter doesn't really have any public events you can handle. Is there a reason why the other part of the application which is doing the writing couldn't raise some custom event that you could handle and use to update the TextBox?

Author

Commented:
It doesn't have to be Streamwriter specifically, I guess I could just use a simple variable and if condition changes then variable flagged and have if statement on the form/ textbox.

I did just get the testbox to update by using this.textbox1.Update();  which your suggesting I thinik?
But it only works for a little while and when I click on console window and back to form it "grays out" I have it on a simple loop right now.

Do you suggest just the Update with using variable to tell when something has been written?
I have read about Invoke and background threads.. I"m using a multi-thread app and it's important that it uses least amount of resources for this textbox/ form viewing.
Thanks
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
The Update() method is used to redraw a control on the screen. It doesn't have much to do with updating a TextBox's value.

Author

Commented:
I guess that's what I need help with.  How to call public event from program threads to the form to update values in textbox.  I took the loop out and your right the update doesn't do anything.

Can you help with how to call event to the form.. I have many on other parts of the app but when I try to type Form. it doesn't recognize it.

Actually, I add a public class on the form and program/ thread will recognize the Form.class , but
now it doesn't recognize the textbox in the public class I added in the form.
Error is missing directive or assembly reference.
Is this the way you would do it?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I should have explained BackgrounWorker a bit more  : )

The DoWork event is the place where your long running process would be. In the above example, I am simulating a long-running process with the for loop. You call RunWorkerAsync() to actually start the BackgroundWorker. Once inside the handler for DoWork, you can see I called ReportProgress() to report a percentage of completion to whoever is listening. In this example, the Form is the one listening because I attached a handler to the ProgressChanged event of the BackgroundWorker. Inside this handler is where I am updating the TextBox.

Author

Commented:
Thanks, kaufmed.. I'll give it try.. It may be 9 hours or so until I get back to you as its getting late and it's going to take a bit to get this into my program.
THanks again and for the tutorial and info on background thread.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. I'll check back later.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
One other thing I omitted: you have to set ReportsProgress on the BackgroundWorker to true in order to use the ReportProgress() method.

Author

Commented:
Kaufmed,

Getting error - is a method, which is not valid in given context.
For the following 2 (also with Report Progress but trying to fix these 2)

                this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }

I also added  under Public Partial Form
   Added following :   public BackgroundWorker backgroundWorker1 = new BackgroundWorker();

Had captial B on your code for backgroundWorker1, took some time figure that simple one out :)
Thanks again

Author

Commented:
Kaufmed,

Got errors fixed, somehow I ended up with _Processchanged at end of each of errors when deleted as you posted the errors go away.

Results I"m getting are same as before using background worker with a simple loop. WOrks fine until clilcking on the form again or another window not on on the app. I'm using datetime to populate the textbox and it stops when I click form again or somethere else.

Trying to call background worker from program as seperate thread, think it has something to do with starting with the button click as shown in code.  Maybe I need to call it within the form and not the program as separate thread? Main program doesn't seem to recognize form1 background worker.
thanks

Author

Commented:
kaufmed,
Thanks alot for complete soution and understanding what the question was.

Was able to start RunWorker.Async by a checkbox with other existing checkboxes on the form.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.