我试图从班级中访问对象TextBox
。我尝试使用构造函数但没有任何反应。它一直在工作,直到我添加了面板并添加了更多表格。
加载表单的我的主表单:
public partial class MenuForm : Form
{
public MenuForm()
{
InitializeComponent();
}
ConfigForm Config = new ConfigForm();
GeneralForm General = new GeneralForm();
private void Menu_Load(object sender, EventArgs e)
{
//Load of Config Form
Config.MdiParent = this.MdiParent;
Config.Show();
//Load of General Form
General.Show();
General.TopLevel = false;
Config.Controls["panel1"].Controls.Add(General);
}
}
这是我的配置表单:
public partial class ConfigForm : Form
{
private ConfigFormHelper confighelper = null;
private GeneralFormHelper generalhelper = new GeneralFormHelper();
public ConfigForm()
{
InitializeComponent();
}
private void comboTemplate_SelectedIndexChanged(object sender, EventArgs e)
{
generalhelper.LoadTemplate();
}
}
这是我的一般助手类:
class GeneralFormHelper
{
GeneralForm generalform2 = new GeneralForm();
public void LoadConfig()
{
this.generalform2.txtDSN1.Text = "test";
}
}
没有错误,但txtDSN1
没有得到"测试"文本。
txtDSN1
位于public
修饰符上。
答案 0 :(得分:1)
这将是GeneralFormHelper
方法GetGeneralForm()
class GeneralFormHelper
{
GeneralForm generalform2;
public void GetGeneralForm(GeneralForm g)
{
this.generalform2 = g;
}
public void LoadConfig()
{
this.generalform2.txtDSN1.Text = "test";
}
}
这将是你的`ConfigForm类:
public partial class ConfigForm : Form
{
private ConfigFormHelper confighelper = null;
private GeneralFormHelper generalhelper;
public ConfigForm(GeneralForm g) /* your Constructor get the General */
{
this.generalhelper = g;
InitializeComponent();
}
private void comboTemplate_SelectedIndexChanged(object sender, EventArgs e)
{
generalhelper.LoadTemplate();
}
}
最后是你的MenuForm
课程:
public partial class MenuForm : Form
{
public MenuForm()
{
InitializeComponent();
}
GeneralForm General = new GeneralForm();
ConfigForm Config = new ConfigForm(General); /* you send General */
private void Menu_Load(object sender, EventArgs e)
{
//Load of Config Form
Config.MdiParent = this.MdiParent;
Config.Show();
//Load of General Form
General.Show();
General.TopLevel = false;
Config.Controls["panel1"].Controls.Add(General);
}
}
答案 1 :(得分:1)
永远不会调用GeneralFormHelper的LoadConfig函数。
public void LoadConfig()
{
this.generalform2.txtDSN1.Text = "test";
}
ConfigForm中的此代码调用LoadTemplate而不是LoadConfig
private void comboTemplate_SelectedIndexChanged(object sender, EventArgs e)
{
generalhelper.LoadTemplate();
}
所以我猜你的第一个问题只是一个错字,你只需要匹配LoadConfig或LoadTemplate。
第二个问题是只有在引发SelectedIndexChanged事件时才调用LoadTemplate(或LoadConfig)。所以,在那之前,你没有得到"测试"文本。
答案 2 :(得分:0)
如果你想从一个类中获取对象,你需要这样做..
In a class: using System.Windows.Forms; namespace getObjectFromaClass { class Class1 { public static TextBox txt1 = new TextBox(); public void getText() { try { txt1.Text = "this is my text"; } catch (Exception er) { string x = er.Message; } } } } In a form: namespace getObjectFromaClass { public partial class Form1 : Form { Class1 cls1 = new Class1(); public Form1() { textBox1=Class1.txt1; InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cls1.getText(); textBox1.Text = Class1.txt1.Text; } } }