我需要關於如何調用我的代碼中其他區域的表單加載以避免過度複製和粘貼的建議。在按下界面中的按鈕後,我基本上需要在其他區域加載頁面。我需要所有存在的代碼,正如您可以看到它複製和粘貼多次一樣。在另一個函數中調用表單加載事件
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();
}
}
不要調用事件處理程序。將必要的代碼放入一個單獨的函數中,然後在需要時調用它。 –