2012-02-19 27 views
0

*我不知道如何讓form1傳遞給form3和form2也可以傳遞給form3。請幫忙!2種不同的形式,都可以打開form3

它具有這樣的錯誤: 不包含一個構造函數 '1' 的參數的FF線:

F3 =新Form3(本); 在Form1上

f3 = new Form3(this); Form 2上

下面是代碼:

鹼形式(選擇到Form1或窗口2):

namespace WindowsFormsApplication1 
{ 
    public partial class baseform : Form 
    { 
     Form1 f1; 
     Form2 f2; 

     public baseform() 
     { 
      InitializeComponent(); 
      f1 = new Form1(this); 
      f2 = new Form2(this); 
     } 

     private void baseform_Load(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      f1.Show(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      f2.Show(); 
     } 
    } 
} 

Form1中:

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
      baseform bf; 
      Form3 f3; 

      public Form1(baseform bf1) 
      { 
        InitializeComponent(); 
        f3 = new Form3(this); 
        bf = bf1; 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 

      } 
    } 
} 

窗體2:

namespace WindowsFormsApplication1 
{ 
    public partial class Form2 : Form 
    { 
      baseform bs; 
      Form3 f3; 

      public Form2(baseform bs1) 
      { 
        InitializeComponent(); 
        f3 = new Form3(this); 
        bs = bs1; 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 

      } 
    } 
} 

Form3:

namespace WindowsFormsApplication1 
{ 
    public partial class Form3 : Form 
    { 
      Form1 f1; 
      Form2 f2; 

      public Form3(Form1 f1a, Form2 f2a) 
      { 
        InitializeComponent(); 
        f1 = f1a; 
        f2 = f2a; 
      } 

      private void Form3_Load(object sender, EventArgs e) 
      { 

      } 
    } 
} 
+0

你是什麼試圖實現?什麼**正確**錯誤? – ChrisF 2012-02-19 12:33:14

+0

它有這個錯誤:不包含一個構造函數,它在ff行上帶有'1'參數:f3 = new Form3(this);在Form1上f3 = new Form3(this);在Form2上 – mhaine 2012-02-19 12:46:58

+0

什麼是baseform?你從它繼承? – 2012-02-19 12:36:45

回答

1

那麼你只能在Form3類的構造函數有兩個參數:

public Form3(Form1 f1a, Form2 f2a) 

,但在這兩個地方你想只用一個來稱呼它:

f3 = new Form3(this); 

因此,可以用兩個參數(可能是null?)調用它或創建一個構造函數(或兩個),該構造函數接受一個參數。在前者的情況下,您需要擁有:

f3 = new Form3(this, null); 

Form1調用當:

f3 = new Form3(null, this); 

Form2

0

調用你在這裏有循環引用時。我建議不要使用構造函數參數來使用公共屬性來讓每個表單知道其他表單。在做這樣的引用檢查之前,檢查它是否被賦值(!= null)。您需要首先創建所有表單,然後分配其屬性以建立引用。

public partial class Form3 : Form 
{ 
    public Form1 f1 { get;set;} 
    public Form2 f2 { get;set;} 

    ... 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if(f1 != null) 
      f1.Show(); 
    } 
} 

然後做到這一點:

Form f1 = new Form1(); 
Form f2 = new Form2(); 
Form f3 = new Form3(); 
f3.f1 = f1; 
f3.f2 = f2; 

按照與其他形式的模式了。

0

顯然,錯誤很明顯:Form3沒有帶一個參數的構造函數。它只需要2個參數。

但是,你的問題比這更深。最簡單的解決方法,您的問題將會使字段靜態,然後添加額外的方法來初始化它們:

public partial class Form3 : Form 
{ 
    static Form1 f1; 
    static Form2 f2; 

    public Form3() 
    { 
     InitializeComponent(); 
    } 

    public static void InitForms(Form1 f1a, Form2 f2a) 
    { 
     f1 = f1a; 
     f2 = f2a; 
    } 

    private void Form3_Load(object sender, EventArgs e) 
    { 
     //use f1 and f2... 
    } 
} 

然後有這樣的:

public baseform() 
    { 
     InitializeComponent(); 
     f1 = new Form1(this); 
     f2 = new Form2(this); 
     Form3.InitForms(f1, f2); 
    } 
0

您嘗試創建與構造帶form3 1名參與者。

f3 = new Form3(this); 

但是你在Form3構造函數2個參數

public Form3(Form1 f1a, Form2 f2a) 

chnage你的代碼,這個定義的。(對於給定的form3和窗口2)

namespace WindowsFormsApplication1 
{ 
    public partial class Form3 : Form 
    { 
      public Form1 f1{get;set;} 
      public Form2 f2{get;set;} 

      public Form3() 
      { 
        InitializeComponent(); 
      } 

      private void Form3_Load(object sender, EventArgs e) 
      { 

      } 
    } 
} 


namespace WindowsFormsApplication1 
{ 
    public partial class Form2 : Form 
    { 
      baseform bs; 
      Form3 f3; 

      public Form2(baseform bs1) 
      { 
        InitializeComponent(); 
        f3 = new Form3(); 
        //and same for for form 2 
        f3.f2=this; 
        bs = bs1; 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 

      } 
    } 
} 
0
  //This code is placed where you want to Switch Back to Form 1, so 
      // wherever in Form2 that you determine you have completed using Form2, such as 
      // a submit button or whatever... 

      private void submitButton_Click(object sender, EventArgs e) 
      { 
       Form1 switchBack = (Form1)Application.OpenForms["Form1"]; 
       switchBack.childFormMethodCall(submitItemTextBox.Text); 
      } 

      //Then in Form1.cs, define the Method you wish to call from the second Form 
      //In my case, I did a simple method called childFormMethodCall with a single passed 
      //in parameter, a string 
      //I then set the string that was Passed in from Form2 as the Text for richTextBox1 
      //richTextBox1 is located on Form1 

      internal void childFormMethodCall(string p) 
      { 
       richTextBox1.Text = p; 
      } 
相關問題