我有两个表单(Form1和Form2)和一个类(Class1)。 Form1包含一个按钮(Button1),Form2包含一个RichTextBox(textBox1)。当我在Form1上按下Button1时,我希望调用方法(DoSomethingWithText)。但是我一直收到“NullReferenceException-对象引用未设置为对象实例”的错误。以下是代码示例:
Form1:
如何在类内调用此方法?非常感谢你的帮助。
Form1:
namespace Test1
{
public partial class Form1 : Form
{
Form2 frm2;
Class1 cl;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm2 = new Form2();
cl.DoSomethingWithText();
frm2.Show()
}
}
}
类1:
namespace Test1
{
class Class1
{
Test1.Form2 f2;
public void DoSomethingWithText()
{
f2.richTextBox1.Text = "Blah blah blah";
}
}
}
如何在类内调用此方法?非常感谢你的帮助。
Class1.DoSomethingWithText(frm2)
。 - Fredrik Mörk