标识按钮中的可见列表



>我正在创建一个程序,该程序将提供一个前端来从服务器位置打开文件,而无需导航到服务器位置。该程序具有打开某些类别列表的按钮,然后是一个按钮从列表中当前选定的项目中打开文件。

我目前的问题是能够识别当前正在查看的 ListBox,以便程序知道将哪个列表用作打开正确文件的参考。

有没有办法创建一个名为"列表"的"对象",然后将列表框分配给它并引用它?它似乎不喜欢我所做的。

object ListBox;
int IdCheck =  0;
string DriveLoc;
private void ButtAirInfo_Click(object sender, EventArgs e)
{
GBAirInfo.Visible = true;
GBAir.Visible = false;
ListBox = LBAirInfo; //Here is where I load the List on to the screen, then 
//assign the list to the object ListBox
}
private void button2_Click(object sender, EventArgs e)
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Files", connection))
{
DataTable FilesTable = new DataTable();
adapter.Fill(FilesTable);
if (ListBox.SelectedIndex == -1)
MessageBox.Show("No Items selected");
else
IdCheck = ListBox.SelectedIndex;
DriveLoc = (FilesTable.Rows[IdCheck]["Location"].ToString());
if (DriveLoc == "")
MessageBox.Show("Item does not have a location");
else
System.Diagnostics.Process.Start(@DriveLoc);
}

好的,所以我自己的研究发现,您可以使用以下方法将列表框分配给通用术语:

Listbox ListBx
this.ListBx = Listbox1;

这样,我可以确保一段代码适用于所有列表框,我需要做的就是在通过按钮加载每个列表时更改分配给 ListBx 的列表框。我希望这有助于某人降低他的路线。

最新更新