【问题标题】:The name 'fe' does not exist in the current context [closed]当前上下文中不存在名称“fe”[关闭]
【发布时间】:2013-04-26 20:31:44
【问题描述】:

请帮助我有两种形式,其中我必须从第一种形式调用第二种形式的方法......但由于上述错误,我被卡住了。当我的第二个表单关闭时,我需要关闭表单。

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

}

【问题讨论】:

  • 为什么你会惊讶于注释掉一个变量的声明会导致编译错误呢?你认为会发生什么?
  • 不,我评论了,所以你们可以看看
  • 相信我,我们知道当您取消必要的声明时会发生什么。
  • 我想在关闭 Flightentry 时关闭乘客详细信息,我还想将插入数据方法传递给 flightentry 表单。

标签: c# .net winforms multiple-forms


【解决方案1】:

这里发生了吗?

//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 的声明


鉴于您的 cmets,我认为您想要以下内容

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

【讨论】:

  • 是的......它发生在这里......
  • 但如果我删除这一行 Flightentry fe = new Flightentry(this);我将无法以其他形式访问该方法
  • @MoniXx 您可以将该行放在PassengerDetail的构造函数中。
  • @MoniXx:Ctrl-C 后跟 Ctrl-V。
  • @MoniXx 我已经编辑了我的帖子。有 3 个潜在的解决方案
猜你喜欢
  • 2015-07-26
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2013-10-02
  • 2014-04-22
  • 2017-06-17
  • 2015-09-11
  • 1970-01-01
相关资源
最近更新 更多