基本上我試圖使它所以如果從列表框中選定的指數(例如可以說選擇的指數是「服務器1」)包含一個數字(「1」)或某些字(「服務器」)它會做某件事。例如:如果從列表框中選擇的索引包含數字1,則會在文本框中輸入1。如果選定的索引包含數字2,它將打開一個應用程序,僅作爲示例。如果ListBox.SelectedIndex包含數字1
我的嘗試:
if (csrListBox.SelectedIndex = "1");
和:
{
List<string> items = ListBox.Items.Cast<string>().ToList();
foreach (string item in ListBox.Items)
{
Regex regex = new Regex(@"1");
Match match = regex.Match(item);
if (match.Success)
{
*Doing Something*
}
}
ListBox.DataSource = items;
}
'SelectedIndex'is一個int。嘗試'SelectedItem'或'SelectedValue' – Pikoh
'SelectedIndex'是一個int,所以使用'if(csrListBox.SelectedIndex == 1){...}'。我假設你不熟悉C#並且通常使用VB.NET。如果您想比較,請始終使用相同的類型並使用'=='而不是'='。 –
這個問題真的不清楚。如果選定**索引處的**項目包含「1」或某個單詞,您是否嘗試執行您的代碼?或者如果**選擇的索引**本身是1,你想執行代碼嗎? – Louis