question

SanelPandzic-5621 avatar image
0 Votes"
SanelPandzic-5621 asked SanelPandzic-5621 commented

The text from the combobox will not be deleted

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);
    
         }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered SanelPandzic-5621 commented

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š" },
         };
     }
 }






demo1.gif (171.9 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@karenpayneoregon Thanks, it works great. Only one thing is unclear to me. And it is these functions: ComboBox1OnSelectedIndexChanged and ComboBox2OnSelectionChangeCommitted. Which is why these two are used, and do I really have to put names like you put them. Or can I change?

0 Votes 0 ·

First off if this works for you mark it as the answer.

ComboBox1OnSelectedIndexChanged and ComboBox2OnSelectionChangeCommitted are not functions, they are events.

ComboBox2OnSelectionChangeCommitted is not part of what this work, I put it in there because you wanted to track drzava.

ComboBox1OnSelectedIndexChanged is the heart in that when a selection changes in the primary ComboBox it sets up the secondary ComboBox data.

do I really have to put names like you put them. Or can I change?

Feel free to change it if so desired like read from a json file for instance. If this is what you want close out this question and start a new question.




0 Votes 0 ·

I counted, thank you for your help

0 Votes 0 ·
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered SanelPandzic-5621 commented

@SanelPandzic-5621, 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.



1.gif (102.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JackJJun-MSFT
I tried it and it doesn't work .. I even made a new project and entered that code, your code still won't work. Right now my brain is blocked because this has raided me before.
I am currently repeating the material etc ... as I studied before. This worked for me before, I found it was what I used to do, this is how I did it and it worked for me but now it doesn't work:

  public Učenje9()
             {
                 InitializeComponent();
                 comboBox1.Items.Add("Balkan");
                 comboBox1.Items.Add("Europa");
             }
        
      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
             {
                 kontinent = comboBox1.SelectedIndex.CompareTo(-1);
                 drzava = 0;
                 comboBox2.Items.Clear();
                 if (comboBox1.SelectedItem == "Balkan") { comboBox2.Items.Add("BIH"); comboBox2.Items.Add("SRB"); }
                 if (comboBox1.SelectedItem == "Europa") { comboBox2.Items.Add("Hrvatska"); comboBox2.Items.Add("Turska"); }
             }
        
        
     private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
             {
                    
                 drzava = comboBox2.SelectedIndex.CompareTo(-1);
        
                  
             }


0 Votes 0 ·