2 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
您應(yīng)該能夠使用 FindStringExact 方法來確定某個(gè)項(xiàng)目是否已存在于 ComboBox 中。
if (questionList.FindStringExact(customQ.Text.Trim()) < 0)
{
// The item was not found to already exist
}
您可以在 .NET 文檔中閱讀有關(guān) FindStringExact 方法的更多信息:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.findstringexact?view=netframework-4.7.2
希望這可以幫助。

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
將 Items 轉(zhuǎn)換為字符串將適用于您想要的。項(xiàng)目作為 ObjectCollection 處理,它們的 IEnumerable 甚至不會(huì)嘗試檢查是否包含字符串。
private void Button1_Click(object sender, EventArgs e)
{
if (!questionList.Items.Cast<string>().Contains(customQ.Text.Trim()))
{
dbconnect.addQ(customQ.Text);
refreshBox();
}
}
上面的代碼在我的測試中正常工作,希望它對你有用。
- 2 回答
- 0 關(guān)注
- 125 瀏覽
添加回答
舉報(bào)