2016-07-14 125 views
0

我試圖從類訪問對象TextBox。我嘗試使用構造函數,但沒有發生。它一直在工作,直到我添加面板並添加更多表單。C#無法從類中訪問對象

我的主要加載我的表格形式:

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沒有得到「測試」文本。

txtDSN1public修飾符。

+1

您正在設置另一個實例的文本框,與您當前所看到的實例不同。看看MainForm中的這一行:'GeneralForm General = new GeneralForm();'你顯示了'Config.Show();'另一個在GeneralFormHelper:'GeneralForm generalform2 = new GeneralForm();' –

+0

這是正確的嗎? 'public GeneralForm myForm; public GeneralFormHelper(GeneralForm form) this.myForm = form; { this.myForm = form; } ' 這是正確的嗎? – FatalError

+0

返修您的設計。正如@RezaAghaei提到的,​​你有'GeneralForm'多個實例,'GeneralFormHelper.LoadConfig()'似乎沒有用在你提供的代碼中。 –

回答

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

感謝btw,它說General在當前上下文中不存在 – FatalError

+0

它說對象引用沒有設置爲對象的一個​​實例this.generalform2.txtDSN1.Text =「test 「; – FatalError

+0

好吧我改變這個答案是更清楚:) – Saeid

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


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)。所以,在那之前,你不會得到「測試」文本。

+0

對不起有LoadTemplate LoadConfig之前還有另一個功能..只是不需要提供..謝謝btw – FatalError