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() +...
Well, the first question is, how did those other strings even get into the combobox in the first place.
 
Do you put all your clothes on in the morning and then take off the ones you don't want, or do you only put on the ones you want in the first place? The answer is fairly obvious, so why would you do it the other way for list items?
 
item in combobox in the first place will load random word,but that "good" list word will be load at the same time,i just want remove other list that not have "good" word.

it posibe using regex
 
item in combobox in the first place will load random word
OK but why? Why are you loading items that don't contain the word "good" if you know that you are going to remove them afterwards? What is the point of adding items that you know you don't want?
 
You can't have stuff load at the same time in WinForms. The UI is single threaded. Something has to happen before another.
 
OK but why? Why are you loading items that don't contain the word "good" if you know that you are going to remove them afterwards? What is the point of adding items that you know you don't want?
that item will use to easy to do other process

like this
if comboBox1.SelectedItem = "test - good" then

do bla222...
 
im not load that item,but the process before that load that word automatic . i know is difficult to know my process.im sorry
 
Let's just assume that you have a valid reason for adding items that you don't want and that you do need to remove them. In that case, think about the logic involved here. You have a list of items. You need to test each item and, if it doesn't contain a specific substring, remove it. Have you considered each part and, more importantly, researched each part? They can all be addressed independently. How do you access each item in a list? How do you test whether a string contains a substring? How do you remove an item from a 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.
 
can i change the question

I want to know How to keep only specific item at Combobox when click button1


that item was load automatic random word to input combobox1,but will contain "-test" word
2.png


or example in this picture i just want keep list have "-test" word only in ouput combobox2 when click button1


to become like this


3.png
 
It's still the same question. It's only one implementation detail that has changed. Go back to post #9. That is still the process you need to implement so you need to put some though into that. Break the problem down as I explained. Address each part as I explained. Ask a specific question about a specific part if you and when you encounter one as I explained. As it stands, You're really just saying "I want to do X, tell me how". It's not really for us to do your work for you. We're here to help with the stuff that you can do on your own but, if you haven't tried, how do we or you know that you can't do it? The first part is looping through the items in the control and I find it very hard to believe that you couldn't do that if you tried so, if you haven't done that, I can only assume that you haven't tried. If you're not going to try, there's little chance that we will be inspired to do so for you. Do what you can for yourself and then, when you encounter an actual issue, show us what you've done and explain where you're stuck. If you do that, you'll find us ready and willing to help you.
 
If you are testing for a particular word that exists in a string, and you have a list that populates the ComboBox, then you have to check each item in that list for that word and remove those that do not contain it.

It would be a foreach statement for the list that populates the combo box....something like below but you would have to research to ensure it is accurate, I make no claims that it is....

C#:
List<string> comboBoxFiller = new List<string>

foreach (var item in comboBoxFiller)
{
    if (item.Contains("Good") != true )
    {
           comboBoxFiller.Remove(item);
    }
   
}

Edited it because I realised, could just check whether it was not true....

Just so you know, unlike the other posters here, I am a rank amateur as well and just learning, the above seems to fit the idea that you have in mind.

If you want to remove a selected item from the list, then google "remove selected item from combobox" and you will find the answer as it is a pretty basic task.
 
Last edited:
If you are testing for a particular word that exists in a string, and you have a list that populates the ComboBox, then you have to check each item in that list for that word and remove those that do not contain it.

It would be a foreach statement for the list that populates the combo box....something like below but you would have to research to ensure it is accurate, I make no claims that it is....

C#:
List<string> comboBoxFiller = new List<string>

foreach (var item in comboBoxFiller)
{
    if (item.Contains("Good") != true )
    {
           comboBoxFiller.Remove(item);
    }
  
}

Edited it because I realised, could just check whether it was not true....

Just so you know, unlike the other posters here, I am a rank amateur as well and just learning, the above seems to fit the idea that you have in mind.

If you want to remove a selected item from the list, then google "remove selected item from combobox" and you will find the answer as it is a pretty basic task.
Thank for ur suggestion,i dont understand very well.
but this my try code

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

but when i click button1 that item not remove
 
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.

Read post #9 more closely. Right now it feels like you are just throwing code at the screen to see what sticks without actually thinking about what you are doing.
 
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.

Read post #9 more closely. Right now it feels like you are just throwing code at the screen to see what sticks without actually thinking about what you are doing.
this is my value inside combobox1

testing
test-testing
test-test test
123 - test
ABC - test
random text - test
 

Attachments

  • my.png
    my.png
    19.7 KB · Views: 19
Back
Top Bottom