2013-04-26 100 views
-5

請幫助我有兩種形式,其中我必須從第一種形式調用第二種形式的方法...但由於上述錯誤,我卡住了。當我的第二張表格關閉時,我需要關閉表格。在當前的情況下,名稱'fe'不存在

namespace WindowsFormsApplication1 
{ 
public partial class Passengerdetail : Form 
{ 
    passengerDetailClass pd = new passengerDetailClass(); 

    Flightentry fe = new Flightentry();  //if i remove this code 

    public Passengerdetail() 
    { 
     InitializeComponent(); 
     fe.FormClosed += new FormClosedEventHandler(fe_FormClosed); //this line gives error mentioned above. 
    } 

    void fe_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     this.Close(); 
    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void Passengerdetail_Load(object sender, EventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

     Flightentry fe = new Flightentry(this);  //this code lets me access the method from the other form removing it will mean no method =(

     this.Hide(); 
     fe.Owner = this; 
     fe.ShowDialog(); 
     this.Show(); 
    } 

    public void insertData() 
    { 
     pd.Insert();  //i want to access this method 
    } 

} 

}

第二形式如下代碼...

namespace WindowsFormsApplication1 
{ 
public partial class Flightentry : Form 
{ 

    flightDetail fd = new flightDetail(); 

    private Passengerdetail pd; 


    public Flightentry(Passengerdetail paDet) 
    { 
     InitializeComponent(); 

     pd = paDet; 
    } 

    private void label5_Click(object sender, EventArgs e) 
    { 

    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     pd.insertData();\\i call the insert method from the previous form here. 

     fd.Insert(comboBox1.Text,comboBox2.Text,comboBox3.Text,textBox3.Text,textBox8.Text,dateTimePicker1.Text,textBox6.Text,textBox5.Text); 
    } 

    private void Flightentry_Load(object sender, EventArgs e) 
    { 

    } 

    private void Flightentry_FormClosing(object sender, FormClosingEventArgs e) 
    { 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     this.Owner.Show(); 
     this.Hide(); 
    } 
} 

}

+6

你爲什麼驚訝的是評論出一個變量的聲明會導致編譯錯誤?你認爲會發生什麼? – siride 2013-04-26 20:33:55

+0

nope我評論說,所以你們可以看看 – MoniXx 2013-04-26 20:38:08

+0

相信我,我們知道當你擺脫必要的聲明會發生什麼。 – siride 2013-04-26 20:39:27

回答

4

是它發生在這裏?

//Flightentry fe = new Flightentry();  //if i remove this code 

public Passengerdetail() 
{ 
    InitializeComponent(); 
    fe.FormClosed += new FormClosedEventHandler(fe_FormClosed); //this line gives error mentioned above. 
} 

,因爲你已經註釋掉聲明fe


在您的意見光,我想你想的東西像下面

Flightentry fe; 

public Passengerdetail() 
{ 
    InitializeComponent(); 
    fe = new Flightentry(this) 
    fe.FormClosed += new FormClosedEventHandler(fe_FormClosed); //this line gives error mentioned above. 
} 

... 

private void button2_Click(object sender, EventArgs e) 
{ 
    this.Hide(); 
    fe.Owner = this; 
    fe.ShowDialog(); 
    this.Show(); 
} 
+0

是的......它發生在這裏...... – MoniXx 2013-04-26 20:36:17

+0

但如果我刪除這一行Flightentry fe = new Flightentry(this);我無法訪問其他形式的方法 – MoniXx 2013-04-26 20:37:03

+0

@MoniXx你可以把這行放在PassengerDetail的構造函數中。 – siride 2013-04-26 20:37:36

相關問題