Link to home
Start Free TrialLog in
Avatar of SSschultz
SSschultz

asked on

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

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

automatically update when new data has changed.
What do you mean by this? Can you provide and example?
Avatar of SSschultz
SSschultz

ASKER

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.
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?
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
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
NP. I'll check back later.
One other thing I omitted: you have to set ReportsProgress on the BackgroundWorker to true in order to use the ReportProgress() method.
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
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
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.