❔ newbie here. how to check for duplicate string vs a combobox list?

im not too sure how to write this. so from combobox textbox i should be able to write something e.g. category is math then if it's still not on the combobox list, add it. ignorecase Math or math this is visual studio windows forms app combobox
3 Replies
TheRanger
TheRanger16mo ago
iterate through each string (with a for loop or a foreach) in your combobox and check if each is equal to the combobox's text alternatively you can use a LINQ method that helps you do that eg
if (comboBoxCategory.Items.Cast<string>().Any(x => comboBox.Text.Equals(x, StringComparison.InvariantCultureIgnoreCase))
if (comboBoxCategory.Items.Cast<string>().Any(x => comboBox.Text.Equals(x, StringComparison.InvariantCultureIgnoreCase))
assuming all of the items are a type of string in your list or simply use a foreach
bool ContainsCategory(string item)
{
foreach(string foo in comboBoxCategory.Items)
{
if (comboBox.Text.Equals(foo, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
return false;
}
bool ContainsCategory(string item)
{
foreach(string foo in comboBoxCategory.Items)
{
if (comboBox.Text.Equals(foo, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
return false;
}
and use that method
glenncommentator24
glenncommentator24OP16mo ago
ty
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server