2017-06-13 44 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。我希望這能幫助有人下線。