Shadcuts
Shadcuts

Reputation: 33

Updating Form Controls from Events in another class

i'm a very beginer in C # and it's also my first post here so please be nice with me :)

Well, i'm trying to code a litle apps that read a file only if it changed and update the new content in a richtextbox control in a Windows Form.

So there is my code :

public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                string path = @"C:\MyPath\";
                Filecheck NewFileChecker = new Filecheck();
                NewFileChecker.WatchingFile(path, "myFile.txt");
            }

And This is my Class FileCheck

class Filecheck
{
        public void WatchingFile (string FilePath, string Filter)
        {
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = FilePath;
            fsw.Filter = Filter;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
            fsw.Changed += OnFileChange;
            fsw.EnableRaisingEvents = true;
        }


        private void OnFileChange(object sender, FileSystemEventArgs e)
        {
            string line;
            try
            {
                using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (StreamReader sr = new StreamReader(file, Encoding.Default))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        MessageBox.Show(line);
                        // I WOULD LIKE TO UPDATE A FORM1 RichTextBox from here....
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

So, i'll would like to update a Windows Form control from where i do the MessageBox method. Someone have an idea how can i do that ? Because when i tried to invoke like this :

Form1.richTextBoxName.Invoke(new MethodInvoker(delegate
{
Form1.richTextBoxName.Text(line);
}));

Well, i get this message: "CS0120: An object reference is required for the nonstatic field, method, or property"

Someone have an idea how can i solve that ? Thanks

Upvotes: 1

Views: 485

Answers (3)

Shadcuts
Shadcuts

Reputation: 33

Yes! That's work very well in my project for my idea Nguyen Van Thanh. But I made some modifications to get this working if it can help anothers.. Thank you very much for your input.

In the main Class:

public Form1()
{
    string path = @"C:\MyPath\";            
    Filecheck NewFileChecker = new Filecheck();
    NewFileChecker.OnUpdateData += (d => UpdateRTBoxJournal(d));
    NewFileChecker.WatchingFile(path, "myFile.txt");
}

public void UpdateRTBoxJournal(string line)
{
    richTextBoxName.Invoke(new MethodInvoker(delegate
    {
    richTextBoxName.Text = line;
    }));
}

And finally in the other class in another file...

public delegate void UpdateData(string data);
class Filecheck
{
    public event UpdateData OnUpdateData;
    public void WatchingFile (string FilePath, string Filter)
    {
        FileSystemWatcher fsw = new FileSystemWatcher();
        fsw.Path = FilePath;
        fsw.Filter = Filter;
        fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
        fsw.Changed += OnFileChange;
        fsw.EnableRaisingEvents = true;
    }

    private void OnFileChange(object sender, FileSystemEventArgs e)
    {
        string line;
        try
        {
            using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (StreamReader sr = new StreamReader(file, Encoding.Default))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    this.OnUpdateData?.Invoke(line);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Une erreur s'est produite." + ex.Message);
        }
    }
}

Thanks again for your anwser.

Upvotes: 1

Nguyen Van Thanh
Nguyen Van Thanh

Reputation: 825

There are alot of solutions for your idea, but I help you one case:

Use delegate:

Step 1: Create a new delegate with param is 01 string , return type is void, name is UpdateData:

public delegate void UpdateData(string data);

Step 2: Declare a event in Filecheck class (OnUpdateData) with the delegate created:

public event UpdateData OnUpdateData;

Step 3: Raise the event anywhen you want:

this.OnUpdateData?.Invoke(line);

Step 4: In your main function, set OnUpdateData by:

Filecheck NewFileChecker = new Filecheck();
NewFileChecker.OnUpdateData += (d => MessageBox.Show(d));

Full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public delegate void UpdateData(string data);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string path = @"C:\MyPath\";
            Filecheck NewFileChecker = new Filecheck();
            NewFileChecker.OnUpdateData += (d => MessageBox.Show(d));
            NewFileChecker.WatchingFile(path, "myFile.txt");
        }
    }
    class Filecheck
    {
        public event UpdateData OnUpdateData;
        public void WatchingFile(string FilePath, string Filter)
        {
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = FilePath;
            fsw.Filter = Filter;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
            fsw.Changed += OnFileChange;
            fsw.EnableRaisingEvents = true;
        }


        private void OnFileChange(object sender, FileSystemEventArgs e)
        {
            string line;
            try
            {
                using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (StreamReader sr = new StreamReader(file, Encoding.Default))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        //MessageBox.Show(line);
                        // I WOULD LIKE TO UPDATE A FORM1 RichTextBox from here....
                        this.OnUpdateData?.Invoke(line);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Upvotes: 0

Miles
Miles

Reputation: 1

  1. The Form1 class needs to be new when you try to use it.
  2. If you want to use the Form1 by your function, you can set the Form1 to Static Type.

Upvotes: 0

Related Questions