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() +...
return !s.Contains("Good");
private static bool Contains(String s)
{
string word = "Good";
word = word.ToLower();
return !s.Contains(word);
}
lowerCase = Filler.Select(x => x.ToLower()).ToList();
comboBox1.DataSource = lowerCase;
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 WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (object item in comboBox1.Items)
{
checkedListBox1.Items.Add(item);
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
StringBuilder sb = new StringBuilder();
foreach (object checkedItem in checkedListBox1.CheckedItems)
{
{
sb.Append(checkedItem.ToString() + Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
comboBox2.Items.Clear();
var regex = new Regex(@"[\n].*- test");
var vv = textBox1.Text;
MatchCollection matches = regex.Matches(vv);
foreach (Match match in matches)
{
comboBox2.Items.Add(match);
}
}
}
}
List<string> myList = new List<string>();
public Form1()
{
InitializeComponent();
myList.Add("testing");
myList.Add("testing");
myList.Add("test-testing");
myList.Add("test-test test");
myList.Add("123 -测试");
myList.Add("ABC -测试");
myList.Add("random text -测试");
comboBox1.DataSource = myList;
}
private void button1_Click(object sender, EventArgs e)
{
myList.RemoveAll(Contains);
comboBox1.DataSource = null;
comboBox1.DataSource = myList;
}
private static bool Contains(String s)
{
return !s.Contains("- test");
}
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 WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (object item in comboBox1.Items)
{
checkedListBox1.Items.Add(item);
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
StringBuilder sb = new StringBuilder();
foreach (object checkedItem in checkedListBox1.CheckedItems)
{
{
sb.Append(checkedItem.ToString() + Environment.NewLine);
}
textBox1.Text = sb.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
comboBox2.Items.Clear();
var regex = new Regex(@"(.*?- test*)");
var vv = textBox1.Text;
MatchCollection matches = regex.Matches(vv);
foreach (Match match in matches)
{
comboBox2.Items.Add(match);
}
}
}
}
我同意跳伞运动员的观点,即1,通过创建文本框和组合框将数据存储在UI中是个坏主意,而2,这并不是必须的。我的更新代码我添加新的正则表达式代码
从
= var regex =新的Regex(@"[\n].*- test");
至
= var regex =新的Regex(@"(.*?- test*)");
支持很多文字
public void button1_Click(object sender, EventArgs e)
{
comboBox1.DataSource = null;
var regex = new Regex(@"(.*?- test.*)");
StringBuilder sb = new StringBuilder();
foreach (string item in myList)
{
sb.Append(item + Environment.NewLine);
}
string tested = sb.ToString();
MatchCollection matches = regex.Matches(tested);
foreach (Match match in matches)
{
comboBox1.Items.Add(match);
}
}
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() + Environment.NewLine);
}
var regex = new Regex(@"(.*?- test*)");
string tested = sb.ToString();
MatchCollection matches = regex.Matches(tested);
foreach (Match match in matches)
{
comboBox2.Items.Add(match);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace SimpleWinForms
{
class TestForm : Form
{
public TestForm()
{
var flow = new FlowLayoutPanel()
{
FlowDirection = FlowDirection.TopDown,
Dock = DockStyle.Fill,
AutoSize = true,
};
SuspendLayout();
flow.SuspendLayout();
Controls.Add(flow);
var comboBox = new ComboBox() { Width = 200 };
flow.Controls.Add(comboBox);
var btnPopulate = new Button() { Text = "Populate" };
btnPopulate.Click += (o, e) => PopulateComboBox(comboBox);
flow.Controls.Add(btnPopulate);
var btnClean = new Button() { Text = "Clean" };
btnClean.Click += (o, e) => CleanComboBox(comboBox);
flow.Controls.Add(btnClean);
flow.ResumeLayout(false);
ResumeLayout(false);
}
Random _random = new Random();
string[] _adjectives = { "large", "small", "glorious", "heavy", "light", "cursed" };
string[] _colors = { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
string[] _weapons = { "wand", "bow", "sword", "mace", "dagger", "hammer" };
string GenerateRandomObject()
{
var adjective = _adjectives[_random.Next(_adjectives.Length)];
var color = _colors[_random.Next(_colors.Length)];
var weapon = _weapons[_random.Next(_weapons.Length)];
return $"{adjective} {color} {weapon}";
}
string GenerateRandomTesSuffix()
=> _random.Next(100) > 50 ? " -测试" : "";
void PopulateComboBox(ComboBox cmb)
{
var count = _random.Next(10, 20);
for (int i = 0; i < count; i++)
cmb.Items.Add(GenerateRandomObject() + GenerateRandomTesSuffix());
}
bool 接受Item(object item)
=> !item.ToString().EndsWith(" -测试");
List<object> GetItemsToRemove(ComboBox cmb)
{
var itemsToRemove = new List<object>();
foreach(var item in cmb.Items)
{
if (!AcceptItem(item))
itemsToRemove.Add(item);
}
return itemsToRemove;
}
void CleanComboBox(ComboBox cmb)
{
foreach (var item in GetItemsToRemove(cmb))
cmb.Items.Remove(item);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
GetItemsToRemove()
returns a list of objects that should be removed 从 the combobox. 什么 it does is iterate over the combobox items and calls 接受Item()
至 determine whether 至 keep the item or not. In my particular implementation, 接受Item()
gets the string representation of the combobox item in question and checks 至 see if it ends with " -测试". If it does, then it does not consider it acceptable. CleanComboBox()
just takes the results of GetItemsToRemove()
和removes them 从 the combo box items collection.接受Item()
can be as simple or as sophisticated as you want it 至 be. It could be a regular expression. It could be a switch statement. It could be a really long complicated call that passes the object into some AI for acceptance or rejection.