2 回答

TA贡献1757条经验 获得超7个赞
您应该能够使用 FindStringExact 方法来确定某个项目是否已存在于 ComboBox 中。
if (questionList.FindStringExact(customQ.Text.Trim()) < 0)
{
// The item was not found to already exist
}
您可以在 .NET 文档中阅读有关 FindStringExact 方法的更多信息:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.findstringexact?view=netframework-4.7.2
希望这可以帮助。

TA贡献1828条经验 获得超4个赞
将 Items 转换为字符串将适用于您想要的。项目作为 ObjectCollection 处理,它们的 IEnumerable 甚至不会尝试检查是否包含字符串。
private void Button1_Click(object sender, EventArgs e)
{
if (!questionList.Items.Cast<string>().Contains(customQ.Text.Trim()))
{
dbconnect.addQ(customQ.Text);
refreshBox();
}
}
上面的代码在我的测试中正常工作,希望它对你有用。
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报