【问题标题】:Identify visible list in a button识别按钮中的可见列表
【发布时间】:2017-06-13 11:02:39
【问题描述】:

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

我当前的问题是能够识别当前正在查看哪个 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);
        }

【问题讨论】:

    标签: c# indexing listbox


    【解决方案1】:

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

    Listbox ListBx
    
    this.ListBx = Listbox1;
    

    这样,我可以确保一段代码适用于所有 Listbox,而我需要做的就是在通过按钮加载每个 List 时更改分配给 ListBx 的 Listbox。我希望这可以帮助某人下线。

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多