using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
{
foreach (var item in comboBox1.Items)
{
sb.Append(item.ToString() +...
ComboBox
? That's three unrelated issues that you should be tackling independently. The main reason that people have trouble solving a problem is that they actually try to solve multiple problems at the same time. Solve one at a time.List<string> comboBoxFiller = new List<string>
foreach (var item in comboBoxFiller)
{
if (item.Contains("Good") != true )
{
comboBoxFiller.Remove(item);
}
}
感谢您的建议,我不太了解。如果要测试字符串中存在的特定单词,并且有一个填充ComboBox的列表,则必须检查该列表中该单词的每个项目,并删除不包含该单词的项目。
这将是填充组合框的列表的foreach语句..类似下面的内容,但是您必须进行研究以确保它是准确的,我没有宣称它是...。
C#:List<string> comboBoxFiller = new List<string> foreach (var item in comboBoxFiller) { if (item.Contains("Good") != true ) { comboBoxFiller.Remove(item); } }
编辑它是因为我意识到,可以只检查它是否不正确。
请注意,与这里的其他海报不同,我也是一名业余爱好者,并且只是学习而已,上面的内容似乎符合您的想法。
如果您要从列表中删除选定的项目,请使用google"从组合框中删除所选项目"您将找到答案,因为这是一项非常基本的任务。
private void button1_Click(object sender, EventArgs e)
{
var removeword = comboBox1.Items.Contains("test");
if (comboBox1.Items.Contains("test") == true)
{
}
else
{
comboBox1.Items.Remove(removeword);
}
removeword
is a boolean that will result in true or false. The combo box items collection will likely not contain a true or false.这是我在combobox1中的价值That is because you are trying to remove a boolean value that doesn't exist inside the combo box.removeword
is a boolean that will result in true or false. The combo box items collection will likely not contain a true or false.
仔细阅读文章#9。现在,感觉就像您只是在向屏幕上抛出代码以查看存在哪些问题而没有实际考虑您在做什么。
public partial class Form1 : Form
{
List<string> Filler = new List<string>();
public Form1()
{
InitializeComponent();
Filler.Add("Test1");
Filler.Add("Good2");
Filler.Add("Test3");
Filler.Add("Good4");
Filler.Add("Test5");
comboBox1.DataSource = Filler;
}
private void button1_Click(object sender, EventArgs e)
{
var word = "Good";
for (int i = 0; i < Filler.Count; i++)
{
if(Filler[i].Contains(word))
{
Filler.RemoveAt(i);
}
}
comboBox1.DataSource = null;
comboBox1.DataSource = Filler;
}
}
private void button1_Click(object sender, EventArgs e)
{
Filler.RemoveAll(Contains);
comboBox1.DataSource = null;
comboBox1.DataSource = Filler;
}
private static bool Contains(String s)
{
return s.Contains("Good");
}