The text from the combobox will not be deleted

Sanel Pandzic 41 Reputation points
2022-05-07T14:29:47.797+00:00

Hi, I have a problem and that is that I will not be able to delete items from the combobox called "city". That is, my city does not work.Items.Clear (); It used to work on other projects while I was working and now it doesn't. It only works if I add more city.ResetText ();

int drz , grd;

  private void Form1_Load(object sender, EventArgs e)
        {
            drzava.Items.Add("BIH");
             drzava.Items.Add("HR");
            drzava.Items.Add("SR");


        }
private void drzava_SelectedIndexChanged(object sender, EventArgs e)
        {

            drz = drzava.SelectedIndex.CompareTo(-1);

            grd = 0;
            grad.Items.Clear();

            if(drzava.SelectedItem == "BIH") { grad.Items.Add("Sarajevo"); grad.Items.Add("Gradačac"); }
            if(drzava.SelectedItem == "HR") { grad.Items.Add("Zagreb"); grad.Items.Add("Split"); }
            if(drzava.SelectedItem == "SR") { grad.Items.Add("Beograd"); grad.Items.Add("Niš");  }




        }
 private void grad_SelectedIndexChanged(object sender, EventArgs e)
        {

           /// grd = grad.SelectedIndex.CompareTo(-1);
            grd = grad.SelectedIndex.CompareTo(-1);

        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,100 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,016 Reputation points
    2022-05-14T10:04:07.013+00:00

    Another way to handle this is first create a data source

    public List<List<string>> DataSources { get; } = new List<List<string>>()  
    {  
        new List<string>() { "Balkan", "Europa" },  
        new List<string>() { "BIH", "SRB" },  
        new List<string>() { "Hrvatska", "Turska" }  
    };  
    

    Then in form load setup initial values for each ComboBox using the DataSource property.

    private void Form1_Load(object sender, EventArgs e)  
    {  
        comboBox1.DataSource = DataSources[0];  
        comboBox2.DataSource = DataSources[1];  
        comboBox1.SelectedIndexChanged += ComboBox1OnSelectedIndexChanged;  
        comboBox2.SelectionChangeCommitted += ComboBox2OnSelectionChangeCommitted;  
    }  
    

    Finally events

    private void ComboBox2OnSelectionChangeCommitted(object sender, EventArgs e)  
    {  
        drzava = comboBox2.SelectedIndex;  
    }  
      
    private void ComboBox1OnSelectedIndexChanged(object sender, EventArgs e)  
    {  
        comboBox2.DataSource = comboBox1.Text == DataSources[0][0] ?   
            DataSources[1] :   
            DataSources[2];  
    }  
    

    By going with this path

    • We have all items coming from one source
    • When comparing current item in ComboBox1 using DataSources[0][0] insures we have a proper match.

    201857-demo1.gif

    Edit

    Noticed there are three items for ComboBox1 so we need to edit a little for ComboBox1OnSelectedIndexChanged

    using System;  
    using System.Collections.Generic;  
    using System.Windows.Forms;  
      
    namespace Demo1  
    {  
        public partial class Form1 : Form  
        {  
            private int drzava;  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                comboBox1.DataSource = DataSources[0];  
                comboBox2.DataSource = DataSources[1];  
                comboBox1.SelectedIndexChanged += ComboBox1OnSelectedIndexChanged;  
                comboBox2.SelectionChangeCommitted += ComboBox2OnSelectionChangeCommitted;  
            }  
      
            private void ComboBox2OnSelectionChangeCommitted(object sender, EventArgs e)  
            {  
                drzava = comboBox2.SelectedIndex;  
            }  
      
            private void ComboBox1OnSelectedIndexChanged(object sender, EventArgs e)  
            {  
      
                if (comboBox1.Text == DataSources[0][0])  
                {  
                    comboBox2.DataSource = DataSources[1];  
                }  
                else if (comboBox1.Text == DataSources[0][1])  
                {  
                    comboBox2.DataSource = DataSources[2];  
                }  
                else if (comboBox1.Text == DataSources[0][2])  
                {  
                    comboBox2.DataSource = DataSources[3];  
                }  
            }  
      
            public List<List<string>> DataSources { get; } = new List<List<string>>()  
            {  
                new List<string>() { "Balkan", "Europa", "SR" },  
                new List<string>() { "BIH", "SRB" },  
                new List<string>() { "Hrvatska", "Turska" },  
                new List<string>() { "Beograd", "Niš" },  
            };  
        }  
    }  
      
    

1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2022-05-09T06:38:31.647+00:00

    @Sanel Pandzic , Welcome to Microsoft Q&A, based on your code, I find that you want to clear the second combobox item after you choose item of the first combobox.

    I recommend that you could clear the item before you add the item to the second combobox.

    Here is a code example you could refer to.

     int drz, grd;  
      
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                drz = comboBox1.SelectedIndex.CompareTo(-1);  
      
                grd = 0;  
      
                if (comboBox1.SelectedItem.ToString() == "BIH") { comboBox2.Items.Clear(); comboBox2.Items.Add("Sarajevo"); comboBox2.Items.Add("Gradačac"); }  
                if (comboBox1.SelectedItem.ToString() == "HR") { comboBox2.Items.Clear(); comboBox2.Items.Add("Zagreb"); comboBox2.Items.Add("Split"); }  
                if (comboBox1.SelectedItem.ToString()== "SR") { comboBox2.Items.Clear(); comboBox2.Items.Add("Beograd"); comboBox2.Items.Add("Niš"); }  
            }  
      
            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                grd = comboBox2.SelectedIndex.CompareTo(-1);  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                comboBox1.Items.Add("BIH");  
                comboBox1.Items.Add("HR");  
                comboBox1.Items.Add("SR");  
            }  
    

    Result:

    200133-1.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.