在另一个函数中调用表单加载事件

时间:2016-03-05 15:05:25

标签: c# .net winforms events

我需要有关如何在代码中的其他区域调用表单加载的建议,以避免过多的复制和粘贴。按下界面中的按钮后,我基本上需要在其他区域加载页面。我需要存在的所有代码,因为你可以看到它有很多次复制和粘贴。

public void FBinterface_Load(object sender, EventArgs e)
{
    txtSerial.Focus();

    try
    {
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string SerialQuery = "select SerialNumber from Inventory";
        command.CommandText = SerialQuery;

        //TO READ DATA
        OleDbDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            comboSerial.Items.Add(reader["SerialNumber"]);
        }
        connection.Close();
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message);
        connection.Close();
    }
    try
    {
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string PartQuery = "select PartNumber from Inventory";
        command.CommandText = PartQuery;

        //TO READ DATA
        OleDbDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            comboPart.Items.Add(reader["PartNumber"]);
        }
        connection.Close();
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message);
        connection.Close();
    }
    try
    {
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string ROnumberQuery = "select ROnumber from Inventory";
        command.CommandText = ROnumberQuery;

        //TO READ DATA
        OleDbDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            comboRO.Items.Add(reader["ROnumber"]);
        }
        connection.Close();
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message);
        connection.Close();
    }
    try
    {
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string LocationQuery = "select Location from Inventory";

        command.CommandText = LocationQuery;

        //TO READ DATA
        OleDbDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            comboLocation.Items.Add(reader["Location"]);
        }
        connection.Close();
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message);
        connection.Close();
    }
}

2 个答案:

答案 0 :(得分:2)

将您的逻辑移至另一个方法并在需要时调用

public void FBinterface_Load(object sender, EventArgs e)
{
     MyLogic();
}

private void MyLogic()
{
    txtSerial.Focus();
    try
    {
          //removed for brevity 
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message);
        connection.Close();
    }
}

现在可以根据需要调用MyLogic方法。

例如:

public void button1_Click(object sender, EventArgs e)
{
     //some code

     MyLogic(); //calling the whole logic you want.

     //extra code
}

答案 1 :(得分:1)

你有没有尝试过:

FBinterface_Load(this,null);
相关问题