2017-06-13 47 views
0

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

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

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

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); 
     } 

回答

0

好了,我自己的研究发现,你可以使用一个列表框分配给一个通用术语 -

Listbox ListBx 

this.ListBx = Listbox1; 

这样一来,我可以保证一段代码将为所有列表框工作,当我通过按钮加载每个List时,我需要做的就是更改分配给ListBx的Listbox。我希望这能帮助有人下线。