我在事件中遍歷所有連接的USB驅動器並將它們填充到Combobox
中。這工作正常,但是,我現在有一個按鈕,我想用來刷新列表並將任何添加到列表中,不存在已經存在。通過USB驅動器進行迭代
這是我使用的代碼:
private void btnRefresh_Click(object sender, EventArgs e)
{
try
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady == true)
{
string dl = d.VolumeLabel;
string dt = Convert.ToString(d.DriveType);
if (comboBox1.Items.Contains(comboBox1.Text))
{
}
else
{
comboBox1.Items.Add(d.Name.Remove(2));
}
}
comboBox1.SelectedIndex = 0;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
}
catch { MessageBox.Show("Error retrieving Drive Information", "Error!"); }
}
我不知道,我已經走了,我需要另一雙眼睛,但是當我有形式打開,按下按鈕,新添加的驅動器不會填充到我的Combobox中。
任何幫助,將不勝感激。
什麼代碼(具體的)部分顯示意外/不需要的行爲?漁獲?其中一個驅動器不是'IsReady'嗎?它是否是失敗的'Contains'嵌套'if'?你應該能夠通過逐步瞭解到底發生了什麼。 –
這是answear http://stackoverflow.com/questions/14089342/refresh-combobox-items-easiest-way –
@JᴀʏMᴇᴇ當逐步通過時,它會到達嵌套的'if'語句,然後完全忽略'else ',這是我需要它做的。如果該項目存在,則忽略它,否則,添加它。 – Sean