2016-03-05 36 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

相關問題