2016-03-05 62 views
2

我需要关于如何调用我的代码中其他区域的表单加载以避免过度复制和粘贴的建议。在按下界面中的按钮后,我基本上需要在其他区域加载页面。我需要所有存在的代码,正如您可以看到它复制和粘贴多次一样。在另一个函数中调用表单加载事件

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

不要调用事件处理程序。将必要的代码放入一个单独的函数中,然后在需要时调用它。 –

回答

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

你尝试:

FBinterface_Load(this,null); 
+0

如果你*要这样做,你应该通过'EventArgs.Empty'作为第二个参数,而不是'null'。但这是糟糕的形式,事件处理程序只能在响应其相关事件时调用。更好的解决方案是将相关代码提取到单独的方法中。 –

+0

这绝对是我想要做的,但.....我不知道如何启动它,例如:“公共无效FBinterface_Load(对象发件人,EventArgs e)”,只是开始的方式。 – CamlCase

+0

只要使用这个调用,就像它从任何函数一样。 – Jerry

相关问题