C#无法从类访问对象

C#无法从类访问对象,c#,winforms,constructor,C#,Winforms,Constructor,我正在尝试从类访问对象文本框。我尝试使用构造函数,但什么也没发生。它一直在工作,直到我添加了面板和更多表单 加载我的表单的我的主表单: public partial class MenuForm : Form { public MenuForm() { InitializeComponent(); } ConfigForm Config = new ConfigForm(); GeneralForm General = new Genera

我正在尝试从类访问对象
文本框
。我尝试使用构造函数,但什么也没发生。它一直在工作,直到我添加了面板和更多表单

加载我的表单的我的主表单:

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
修改器上。

这将是您的
GeneralFormHelper
方法
GetGeneralForm()

这将是您的`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();
    }
}
最后是您的
菜单窗体
类:

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);
     }
}

这将是使用方法
GetGeneralForm()

这将是您的`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();
    }
}
最后是您的
菜单窗体
类:

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);
     }
}

如果你想从类中获取对象,你需要这样做

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; } } } 在课堂上: 使用System.Windows.Forms; 命名空间getObjectFromaClass { 一班 { 公共静态文本框txt1=新文本框(); public-void-getText() { 尝试 { txt1.Text=“这是我的文本”; } 捕获(异常er) { 字符串x=er.Message; } } } } 以某种形式: 命名空间getObjectFromaClass { 公共部分类Form1:Form { Class1 cls1=新的Class1(); 公共表格1() { textBox1=Class1.txt1; 初始化组件(); } 私有void Form1\u加载(对象发送方、事件参数e) { cls1.getText(); textBox1.Text=Class1.txt1.Text; } } }
如果你想从类中获取对象,你需要这样做

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; } } } 在课堂上: 使用System.Windows.Forms; 命名空间getObjectFromaClass { 一班 { 公共静态文本框txt1=新文本框(); public-void-getText() { 尝试 { txt1.Text=“这是我的文本”; } 捕获(异常er) { 字符串x=er.Message; } } } } 以某种形式: 命名空间getObjectFromaClass { 公共部分类Form1:Form { Class1 cls1=新的Class1(); 公共表格1() { textBox1=Class1.txt1; 初始化组件(); } 私有void Form1\u加载(对象发送方、事件参数e) { cls1.getText(); textBox1.Text=Class1.txt1.Text; } } }
从未调用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)。因此,在此之前,您不会得到“test”文本。

从未调用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)。因此,在此之前,您不会获得“测试”文本。

您正在设置另一个实例的文本框,与您当前看到的实例不同。查看MainForm中的这一行:
GeneralForm General=new GeneralForm()您显示的
Config.Show()
和GeneralFormHelper中的另一个:
GeneralForm generalform2=new GeneralForm()这是正确的吗<代码>公共通用格式myForm;公共GeneralFormHelper(GeneralForm form){this.myForm=form;}
是否正确?请重新设计。正如@RezaAghaei所提到的,您有多个
GeneralForm
的实例,而且
GeneralFormHelper.LoadConfig()
似乎没有在您提供的代码中使用。@FatalError是的,这可能是一个解决方案。但是在我的配置表单中,参数上应该放什么呢<代码>专用GeneralFormHelper generalhelper=新建GeneralFormHelper()您正在设置另一个实例的文本框,与当前看到的实例不同。查看MainForm中的这一行:
GeneralForm General=new GeneralForm()您显示的
Config.Show()
和GeneralFormHelper中的另一个:
GeneralForm generalform2=new GeneralForm()这是正确的吗<代码>公共通用格式myForm;公共GeneralFormHelper(GeneralForm form){this.myForm=form;}
是否正确?请重新设计。正如@RezaAghaei所提到的,您有多个
GeneralForm
的实例,而且
GeneralFormHelper.LoadConfig()
似乎没有在您提供的代码中使用。@FatalError是的,这可能是一个解决方案。但是在我的配置表单中,参数上应该放什么呢<代码>专用GeneralFormHelper generalhelper=新建GeneralFormHelper()谢谢,顺便说一句,它表示当前上下文中不存在General。它表示对象引用未设置为指向this.generalform2.txtDSN1.Text=“test”的对象实例;你必须通过你想要的方法发送你的对象,但是如果我想要它是动态的呢?因为我不仅仅使用一般形式,我还有其他形式将使用配置我希望得到你的意思,因为使用其他类型你可以使用GenericsThank顺便说一句,它说当前上下文中不存在General它说对象引用未设置为该对象的实例。generalform2.txtDSN1.Text=“test”;你必须通过你想要的方法发送你的对象,但是如果我想要它是动态的呢?因为我不是