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 ? " - test" : "";
void PopulateComboBox(ComboBox cmb)
{
var count = _random.Next(10, 20);
for (int i = 0; i < count; i++)
cmb.Items.Add(GenerateRandomObject() + GenerateRandomTesSuffix());
}
bool AcceptItem(object item)
=> !item.ToString().EndsWith(" - test");
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());
}
}
}