类中声明
MainForm severform = new MainForm();//MainForm是窗体
severform.InsertLogInfo(provice, provice + "客户端连接成功!");//插入MainForm窗体中的datagridview
string uploadFolder = severform.GetSavePath();//获取MainForm窗体中保存路径文本框值但是出现上述问题!不知道如何可以实现?
MainForm severform = new MainForm();//MainForm是窗体
severform.InsertLogInfo(provice, provice + "客户端连接成功!");//插入MainForm窗体中的datagridview
string uploadFolder = severform.GetSavePath();//获取MainForm窗体中保存路径文本框值但是出现上述问题!不知道如何可以实现?
这里错了
你需要的不是MainForm的对象,而且MainForm的实例
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public void SetText(string str)
{
textBox1.Text = str;
} public string GetText()
{
return textBox1.Text;
} private void button1_Click(object sender, EventArgs e)
{
MyClass cls = new MyClass(this);
cls.SetValve();
} private void button2_Click(object sender, EventArgs e)
{
MyClass cls = new MyClass(this);
cls.GetValue();
}
}
public class MyClass
{
private Form1 m_frm = null;
public MyClass(Form1 frm)
{
if (frm != null)
{
m_frm = frm;
}
} public void SetValve()
{
if (m_frm!=null)
m_frm.SetText("123");
} public void GetValue()
{
if (m_frm!=null)
MessageBox.Show(m_frm.GetText());
}
}}
这样声明吗?可是提示未将对象的应用设置到对象的实例?
不是这样声明实例吗?
你仔细看看我的例子,有一个MyClass构造函数,有一个参数
because it does not have a default (parameter-less) constructor.
To fix the problem, add a default constructor to the type,
or pass an instance of the type to the host.