Resolved How to remove unwanted item at Combobox

e3xpic

Member
Joined
Dec 30, 2020
Messages
11
Programming Experience
Beginner
Heloo in newbie here,and new in programing

I want to know
How to remove unwanted item at Combobox when click button1

3.png


for example in this picture i just want keep list have "good" word only

test - good
test - very good
test- ok good


if i click button1 it will be like this

44.png


Thank for your help :)
 
Solution
Thank 4 ur help i think i solve my problem.Thank all try to help me.



C#:
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() +...
my update code i add new regex code
from
= var regex = new Regex(@"[\n].*- test");

to
= var regex = new Regex(@"(.*?- test*)");

to support many text
I agree with Skydiver, that 1, it is a bad idea to store data in the UI by creating textboxes and comboboxes and 2, it isn't really necessary.

Taking your code from above:

C#:
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);
            }
        }

Does the same thing within a single Combobox.
 
Also, so you understand.

When I asked where does the data come from in ComboBox1, I did not mean, work, school etc. I meant:

1. Text file;
2. List;
3. SQL database;
4. Excel file;
5. DataTable;

At the moment my answer is based on the combobox being populated by a List....but without knowing how you are populating the combobox, it is hard to say whether that answer is correct.
 
I just re-read this thread and while interesting to try new code, I think you missed advice early on from @jmcilhinney

Look at your application steps:

1. You receive data.
2. You put data into combobox.

You do that, it doesn't magically appear in the combobox, you put it in the combobox.

Why not have 3 steps?

1. You receive data.
2. You remove items you do not want.
3. You put data into combobox.

Then you have a combobox that only contains the data that you want.
 
Thank 4 ur help i think i solve my problem.Thank all try to help me.



C#:
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);



                }

            }
        }


    }
    }

but if i use single combobox
will became like this


Untitled.png
 
Solution
I know English isn't your first language, but really difficult to assist you in anyway, when you don't appear to read what is written or reply to what is written.
 
I can't take the flailing around I'm seeing on this thread anymore. There is no need for that StringBuilder. There is no need for a second combobox. You can filter the items directly. See code below:

C#:
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());
        }
    }
}

GetItemsToRemove() returns a list of objects that should be removed from the combobox. What it does is iterate over the combobox items and calls AcceptItem() to determine whether to keep the item or not. In my particular implementation, AcceptItem() gets the string representation of the combobox item in question and checks to see if it ends with " - test". If it does, then it does not consider it acceptable. CleanComboBox() just takes the results of GetItemsToRemove() and removes them from the combo box items collection.

Obviously, the implementation of AcceptItem() can be as simple or as sophisticated as you want it to 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.

Anyway, this gets us back to what the OP wanted in his original post: the data in the combobox is filtered down to his acceptable values.
 
Back
Top Bottom