使用委托参数在C#中的类和窗体之间传递数据
我必须将值从类传递到RichTextBox。这是我的密码。我必须将值传递到任何工具,如textbox、listbox,但我不知道如何传递。我必须使用委托将md值传递给这两个方法并传递到同一个richtextbox中。使用委托参数在C#中的类和窗体之间传递数据,c#,forms,class,delegates,C#,Forms,Class,Delegates,我必须将值从类传递到RichTextBox。这是我的密码。我必须将值传递到任何工具,如textbox、listbox,但我不知道如何传递。我必须使用委托将md值传递给这两个方法并传递到同一个richtextbox中。 namespace delegateEx2 { public class MyClass : Form1 { delegate void MyDelegate(string MyString); public void Show
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。我以前必须查找这个。下面是一些示例代码 C类
using System.Windows.Forms;
然后以我的形式我有
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);
}
this.rtxblog.Text+=消息
?不!不起作用。即使类继承自公共类MyClass:Form1,但它不能访问或传递。好的。我可以看到richboxtext,但它不能传递值,例如code
public void log1(字符串消息){rtxtLog.Text=message;MessageBox.Show(message);}code
这就是我提到使用委托的原因。
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);
}