0

我必须将值从一个类传递给 RichTextBox。这是我的代码。我必须将值传递到文本框、列表框等任何工具中,但我不知道如何。我必须使用委托将 md 值传递给这两种方法并传递给同一个富文本框。


 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)
        {

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

        }

    }

4

2 回答 2

2

我以前必须查一下这个。这是一些示例代码...

类.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;
}

然后在我的表格中,我有...

    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);
    }
于 2013-05-09T18:21:47.257 回答
0

简单的问题是更改富文本框声明中的修饰符。您可以在 Form1.designer.cs 中找到声明。将修饰符从私有更改为受保护,然后您可以从 log1 方法访问richtextbox。

于 2013-01-24T05:59:41.003 回答