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() +...
I thought I would try and work this one out, and the foreach loop fails, because the index changes after the removal of the first word and throws an exception, which I didn't really consider.

So err this works...

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

The ComboBox only contains Test related words after pressing the button.

For the OP, you really should read carefully what the two other posters are telling you....I have learnt a LOT from this site in a short time.....but I read very carefully what I am told and then read more off this site to try and understand it.

I could not have written that code without having read what I was being told on by JMC and Skydiver....read what they are telling you.
 
Yes, and what happens if you set a breakpoint on your button handler and step through your code? Which lines get executed? What is the value of removeword?
 
@ConsKa what happens if you swap line 10 and 11? Seems you have a bug.
 
@JohnH Oh, I know why, it is because the index of the words still change.....so anytime Good is next to Good you skip the subsequent one, as you remove Good at 3, which means that the Good at 4 becomes 3, but you have gone past 3 now, and so are looking at 4, when Good is no longer there.

Interesting, found something new...

C#:
        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");
        }
 
Last edited:
Worth noting that the above is case sensitive ("Good" not "good")...and it isn't really a solution, as it removes all words with Good.

But it is a step closer as it now allows you to remove all words from a list in one go where those words contain a certain string.

Next step is working out how to reverse it so that it removes all words that do not contain that word.
 
And notice that it also doesn't distinguish between words and compound words. So the word "goodbye" and "hardgoods" would also be potentially found.
 
And notice that it also doesn't distinguish between words and compound words. So the word "goodbye" and "hardgoods" would also be potentially found.

In all honesty, I am not sure exactly what he wants, I just wanted to see if I could write something to remove everything from a list that contained a word and it was useful to learn about the Index problems of removing as you go from a list - and how that changes the Index.

That might be useful for me in a future project.
 
It was actually bugging me on how to remove all words that do not contain "Good", I had tried a few variations of ! to negate the code, but realised it is simply this:

C#:
return !s.Contains("Good");

From this list:

Test1
Good2
Good4
Test3
Test5

Just Good2 and Good4 are left.

Still not case sensitive and as noted will return any words that contain Good wherever they appear in the word but feels good to achieve the goal.

And as an achievable goal.

C#:
private static bool Contains(String s)
        {
            string word = "Good";
            word = word.ToLower();
            return !s.Contains(word);
        }

Puts the selected word to lower.

And

C#:
lowerCase = Filler.Select(x => x.ToLower()).ToList();

comboBox1.DataSource = lowerCase;

Puts the combobox to lower case too.

Still have no idea whether this is what the OP wanted, but has been a neat little exercise.
 
Last edited:
See the other variant of Contains():

It still won't solve your substring issue, but at least it'll solve the case sensitivity issue.
 
Thank for all,finally i got what i want
But the code not very nice because i try and error combine multi code,checkedListBox,textbox,
in this code i use regex


before

2.png



after click button1

3.png


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 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);
            }
        }
    }
}


if your all help to fix it,i very appreciated.Thank again.
Sorry for your miss understand n my mistake.
 
It would be useful to know the type of data you actually want to remove and where it is coming from.

The code I provided below actually removes "- test" in a single combobox from a single button press.

However, if the actual data you want to remove is more varied then the code may need to be as complex as you have written above.

Code:

C#:
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 - test");
            myList.Add("ABC - test");
            myList.Add("random text - test");

            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");
        }

Outcome:

Starting Combobox:

List Full.jpg


After button press:

List Cut.jpg
 
Last edited:
@e3xpic : You said that you wanted to remove items from a combobox, but in the code you presented in post #25, you are actually removing items from a textbox.

Using UI for data storage is really a bad idea.
 
It would be useful to know the type of data you actually want to remove and where it is coming from.

The code I provided below actually removes "- test" in a single combobox from a single button press.

However, if the actual data you want to remove is more varied then the code may need to be as complex as you have written above.
If u ask me where data come from,Sorry i cannot tell you more,But data will come random name, but "- test " will come together into combobox1.not we or i add by manual like your code (myList.Add("testing");).
the problem now i not found code to remove item from combobox1 directly,that why i tranfer that data to textbox1.because textbox easy to remove that data.
 
the problem now i not found code to remove item from combobox1 directly
And there in lies the problem. You are trying to "find" code instead of actually puzzling it out for yourself and trying to "write" code.
 
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




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 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);
            }
        }
    }
}
 
Last edited:
Back
Top Bottom