我需要关于如何调用我的代码中其他区域的表单加载以避免过度复制和粘贴的建议。在按下界面中的按钮后,我基本上需要在其他区域加载页面。我需要所有存在的代码,正如您可以看到它复制和粘贴多次一样。在另一个函数中调用表单加载事件
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();
}
}
不要调用事件处理程序。将必要的代码放入一个单独的函数中,然后在需要时调用它。 –