Search code examples
c#.netwinformsmultiple-forms

The name 'fe' does not exist in the current context


please help i have two forms in which i have to call a method from the first form in second form... but i am stuck because of the error stated above. And i need to close the form when my second form closes.

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
    }

}

}

the code for the second form is as follows...

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

}


Solution

  • is it happening here?

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

    because you've commented out the declaration for fe


    In light of your comments, I think you want something like the following

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