2016-07-14 14 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); 
    } 
} 

が私のconfig形式である:ここで

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修飾子にあります。

+1

現在表示されているインスタンスとは別の、別のインスタンスのテキストボックスを設定しています。あなたは 'Config.Show();'を表示し、もう一つはGeneralFormHelper: 'GeneralForm generalform2 = new GeneralForm();' –

+0

でこれを見て、この行をMainForm: 'GeneralForm General = new GeneralForm(); 'public GeneralForm myForm; public GeneralFormHelper(GeneralFormフォーム) { this.myForm = form; } これは間違いありませんか? – FatalError

+0

デザインを再設計します。 @RezaAghaeiが述べたように、あなたは 'GeneralForm'に対して複数のインスタンスを持ち、' ​​GeneralFormHelper.LoadConfig() 'はあなたが提供したコードでは使われていないようです。 –

答えて

1

これはこれはあなたの `ConfigFormクラスになりますあなたの方法でGeneralFormHelperGetGeneralForm()

class GeneralFormHelper 
    { 
     GeneralForm generalform2; 
     public void GetGeneralForm(GeneralForm g) 
     { 
      this.generalform2 = g; 
     } 
     public void LoadConfig() 
     { 
      this.generalform2.txtDSN1.Text = "test"; 
     } 
    } 

次のようになります。必要であれば

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

ok私はこの答えをより明確にするために変更します:) – Saeid

0

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クラスクラスからオブジェクトを取得するこのようにする必要があります。

 
    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関数は決して呼び出されません。 ConfigFormで

public void LoadConfig() 
{ 
this.generalform2.txtDSN1.Text = "test"; 
} 

このコードは、だから私はあなたの最初の問題は単なるタイプミスだと思い、そしてあなただけのLoadConfigかLoadTemplateのいずれかと一致する必要がある代わりにLoadConfig

private void comboTemplate_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    generalhelper.LoadTemplate(); 
} 

のLoadTemplateを呼び出します。

2番目の問題は、SelectedIndexChangedイベントが発生したときにのみLoadTemplate(またはLoadConfig)が呼び出されることです。だから、それまでは、 "テスト"テキストを取得しません。

+0

私はLoadConfigであるLoadTemplateの前に別の関数があります。申し訳ありません。ただ提供する必要はありません.. thanks btw – FatalError

関連する問題