Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在另一个函数中调用表单加载事件_C#_.net_Winforms_Events - Fatal编程技术网

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

C# 在另一个函数中调用表单加载事件,c#,.net,winforms,events,C#,.net,Winforms,Events,我需要关于如何在代码的其他区域调用表单加载以避免过度复制和粘贴的建议。我基本上需要在按下界面上的按钮后在其他区域加载页面。我需要现有的所有代码,正如您所看到的,要复制和粘贴多次是相当困难的 public void FBinterface_Load(object sender, EventArgs e) { txtSerial.Focus(); try { connection.Open(); OleDbCommand command =

我需要关于如何在代码的其他区域调用表单加载以避免过度复制和粘贴的建议。我基本上需要在按下界面上的按钮后在其他区域加载页面。我需要现有的所有代码,正如您所看到的,要复制和粘贴多次是相当困难的

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();
    }
}
您是否尝试过:

FBinterface_Load(this,null);

将逻辑移到另一个方法,并在需要时调用它

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
}

不要调用事件处理程序。将必要的代码放入单独的函数中,然后在需要时调用该函数。如果要执行此操作,应将
EventArgs.Empty
作为第二个参数传递,而不是
null
。但这是一种糟糕的形式,事件处理程序只应在响应其关联事件时调用。更好的解决方案是将相关代码提取到一个单独的方法中。这肯定是我想要做的,但是…..我不确定如何启动它,例如:“public void FBinterface_Load(object sender,EventArgs e)”,只是启动的方式。只需使用this调用,因为它来自任何函数。我的意思是,我是否像这样启动它:“public void ItemsLoad()”?或者我是否缺少有效的参数?如果您询问如何调用
FBinterface\u Load
函数,请使用我文章中的代码。
public void…
在函数调用中未使用。