如何从另一个事件访问一个事件中创建的数据?

时间:2016-11-26 18:52:40

标签: c# events parameters

我想访问Load事件中TextBox.TextChanged事件中创建的获取数据。

我该怎么做?

private void Login_Load(object sender, EventArgs e)
{
    //Instantiating DB Obj
    DataBaseConnection obtainData = new DataBaseConnection();

    //Filling dataset
    obtainData.passSqlCmdandFillDs = "select * from [tblLogin]";
}

private void txtDisplay_TextChanged(object sender, EventArgs e)
{
    //Want to user obtainData here
}

1 个答案:

答案 0 :(得分:1)

obtainData事件之外声明Login_Load,以便其他方法可用。

DataBaseConnection obtainData;

private void Login_Load(object sender, EventArgs e)
{
    //Instantiating DB Obj
    obtainData = new DataBaseConnection();

    //Filling dataset
    obtainData.passSqlCmdandFillDs = "select * from [tblLogin]";
}

private void txtDisplay_TextChanged(object sender, EventArgs e)
{
    if (obtainData != null)
    {
        // Use obtainData here
    }
}