Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

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
0
SSschultz
Asked:
SSschultz
  • 7
  • 7
1 Solution
 
käµfm³d 👽Commented:
automatically update when new data has changed.
What do you mean by this? Can you provide and example?
0
 
SSschultzAuthor 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.
0
 
käµfm³d 👽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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SSschultzAuthor 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
0
 
käµfm³d 👽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.
0
 
SSschultzAuthor 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?
0
 
käµfm³d 👽Commented:
And now a quick lesson on GUIs  = )

In Forms apps, all the controls and forms are typically created on one thread, the main thread. You also have the concept of a "message pump." This pump processes the Windows messages that get passed around. Examples of messages are: move form, button click, text box text changed, etc. There are numerous messages that get passed around. Whenever you have a long-running process that you perform in some handler, say a Button_Click, without using another thread, you block the message pump from processing messages. This leads to the infamous "Form not responding" messages you may have seen in the title bars of some of your forms. The best way to handle this is to use a separate thread to do the long-running process.

In .NET, you have several options for threading: use a BackgroundWorker, use the ThreadPool, or use your own thread. The easiest way to do this would be to the BackgroundWorker. For example:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }

        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.textBox1.Text = e.ProgressPercentage.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i += 10)
            {
                this.backgroundWorker1.ReportProgress(i);
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

Open in new window

0
 
käµfm³d 👽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.
0
 
SSschultzAuthor 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.
0
 
käµfm³d 👽Commented:
NP. I'll check back later.
0
 
käµfm³d 👽Commented:
One other thing I omitted: you have to set ReportsProgress on the BackgroundWorker to true in order to use the ReportProgress() method.
0
 
SSschultzAuthor 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
0
 
SSschultzAuthor 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
0
 
SSschultzAuthor 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.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now