简体   繁体   English

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

[英]Passing data between class and form in C# using delegate parameter

I have to pass value into RichTextBox from a class. 我必须将值从类传递到RichTextBox中。 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. 我必须使用委托将md值传递给这两种方法并传递到同一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)
        {

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

        }

    }

I had to look this up before. 我必须先查一下。 Here is some example code... 这是一些示例代码...

TheClass.cs TheClass.cs

using System.Windows.Forms;

... ...

public bool validateForm(TextBox txtTitle, TextBox txtPath, CheckedListBox ckList)
{
    bool title = false;
    bool path = false;

    if (txtTitle.Text == String.Empty)
    {
       title = false;
       txtTitle.Text = "Title is empty!";
       paintred(txtTitle);
    } else { title = true; paintwhite(txtTitle); }
    if (txtPath.Text == String.Empty)
    {
       path = false;
       txtPath.Text = "Path is empty!";
       paintred(txtPath); 
    } else { path = true; paintwhite(txtPath); }   

    bool ckItem1 = ckList.GetItemChecked(0);
    bool ckItem2 = ckList.GetItemChecked(1);
    bool ckItem3 = ckList.GetItemChecked(2);
    bool ckItem4 = ckList.GetItemChecked(3);
    bool ckItem5 = ckList.GetItemChecked(4);

    if (title && path && ckItem1 && ckItem2 
        && ckItem3 && ckItem4 &&
           ckItem5 )
        return true;
    else
        return false;
}

Then in my form I have... 然后以我的形式...

    if (TheClass.validateForm(txtBox, txtBox2, listCheckList))
    {
        txtBox3.Text = TheClass.generateItem1(something, something2);
        txtBox4.Text = TheClass.generateItem2(something, something2, txtPath.Text, txtTitle.Text, listCheckList.GetItemChecked(5));
    }
    else
    {
        MessageBox.Show("Please check fields marked in red, if any. Double check your Check List required items.", "Title Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

The simple questions is to change the modifier in your richtextbox declaration. 简单的问题是更改Richtextbox声明中的修饰符。 You can find the declaration in Form1.designer.cs. 您可以在Form1.designer.cs中找到该声明。 Change the modifier from private to protected, then you can access the richtextbox from log1 method. 将修饰符从专用更改为受保护,然后可以从log1方法访问richtextbox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM