R
R
Roman2020-08-17 09:21:34
C++ / C#
Roman, 2020-08-17 09:21:34

C#. How to correctly update the textbox on the form from another class?

I have a form with a button and a text box.

Task: when a button is pressed, a method from another class is called, which connects to the server and then launches another method from the same class into the stream, checking for new data and if something has appeared (there will be only text), you need to display them in text box on the form, the problem is that I can't figure out how to write it correctly.

Of the many options, I once managed to do it somehow through "Application.OpenForms", but then this stopped working. Teach me how to build things like this in OOP.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-08-17
@scripterasm

public partial class Form1 : Form
    {
        private LongTimeJob _ltj;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _ltj = new LongTimeJob();
            button1.Enabled = false;
            _ltj.Complete += _ltj_Complete;
            _ltj.Tick += _ltj_Tick;
            _ltj.Run();
        }

        private void _ltj_Tick(object sender, TickArgs args) =>
            progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = args.Percent; });

        private void _ltj_Complete(object sender, EventArgs args)
        {
            button1.Invoke((MethodInvoker)delegate { button1.Enabled = true; });
            textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = _ltj.Text; });
        }
    }

    public class LongTimeJob
    {
        public int Percent { get; private set; }
        public string Text { get; private set; } = "";
        public void Run()
        {
            Percent = 0;
            Task.Run(async () => await Download());
        }

        private async Task Download()
        {
            for (var i = 0; i < 9; i++)
            {
                Percent = i * 10;
                Text += $"Download: {i}\r\n";
                OnTick(Percent);
                await Task.Delay(400);
            }

            await Parse();
        }

        private async Task Parse()
        {
            Percent = 100;
            Text += "Parsing\r\n";
            OnTick(Percent);
            await Task.Delay(500);
            OnComplete();
        }
        public event TickDelegate Tick;
        protected virtual void OnTick(int percent)
        {
            Tick?.Invoke(this, new TickArgs(percent));
        }
        public event CompleteDelegate Complete;
        protected virtual void OnComplete()
        {
            Complete?.Invoke(this, new EventArgs());
        }
    }

    public delegate void TickDelegate(object sender, TickArgs args);

    public delegate void CompleteDelegate(object sender, EventArgs args);

    public class TickArgs : EventArgs
    {
        public TickArgs(int percent)
        {
            Percent = percent;
        }
        public int Percent { get; private set; }
    }

D
d-stream, 2020-08-17
@d-stream

In OOP, or rather MVVM, they change the classes / fields of classes that are associated (binding) with visual elements. Usually mvvm is linked to WPF, but the very essence of the approach can be used outside of wpf.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question