使用委托参数在C#中的类和形式之间传递数据

问题描述:

我必须从一个类传入值到RichTextBox。这里是我的代码。我必须传递值到任何工具像文本框,列表框,但我不知道如何。我必须使用代理将md值传递到两个方法并进入同一个富文本框。

I have to pass value into RichTextBox from a class. Here is my code. I have to pass values into any tools like textbox, listbox but I don't know how. I have to use delegates to pass md value to both methods and into the same richtextbox.


 namespace delegateEx2
{
    public class MyClass : Form1
    {
        delegate void MyDelegate(string MyString);

        public void ShowThoseMessages()
        {
            MyDelegate md = new MyDelegate(log1);
            md += log2;
            md("Error Log Text");
        }

        public void log1(string message) {

            //what can I write here to pass the md into the RichTextBox on Form1.cs
            //I tried something like Systems.Windows.Form.rtxblog but did not work 
            //......................................


        }

        public void log2(string message2)
        {

          //.....................................

        }

    }


简单的问题是更改richtextbox声明中的修饰符。
你可以在Form1.designer.cs中找到声明。
将修饰符从private更改为protected,然后您可以从log1方法访问richtextbox。

The simple questions is to change the modifier in your richtextbox declaration. You can find the declaration in Form1.designer.cs. Change the modifier from private to protected, then you can access the richtextbox from log1 method.