Question clear items

devilonline

New member
Joined
Nov 15, 2020
Messages
3
Programming Experience
Beginner
Hi,
I have an combox, that i fill with items.
when i fill it with items I want to clear the items if there is any there. the problem is that the combox doesnt keep the items like shown in the pic

here is the code that i have:
Capturar.JPG

C#:
if (comboBox1.SelectedIndex == 0)
{
label1.Text = "choice 1";
comboBox2.Items.Clear();

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\\Work\\tip.xml");
XmlNode idNodes = doc.SelectSingleNode("colors/pos3");
foreach (XmlNode node1 in idNodes.ChildNodes)
if (!comboBox2.Items.Contains(node1.InnerText))
{

comboBox2.Items.Add(node1.InnerText);
}


switch (comboBox2.SelectedIndex)
{
case 0:

label2.Text = "choice 21";
XmlDocument doc1 = new XmlDocument();
doc1.Load(@"C:\\Work\\tip.xml");
XmlNode idNodes1 = doc.SelectSingleNode("colors/pos4");
foreach (XmlNode node1 in idNodes1.ChildNodes)
if (!comboBox3.Items.Contains(node1.InnerText))
{
comboBox3.Items.Add(node1.InnerText);
}
break;

case 1:

label2.Text = "choice 22";
XmlDocument doc2a = new XmlDocument();
doc2a.Load(@"C:\\Work\\tip.xml");
XmlNode idNodes2a = doc.SelectSingleNode("colors/pos5");
foreach (XmlNode node1 in idNodes2a.ChildNodes)
if (!comboBox3.Items.Contains(node1.InnerText))
{
comboBox3.Items.Add(node1.InnerText);
}


switch (comboBox3.SelectedIndex)
{
case 0: label3.Text = "choice 31";break;
case 1: label3.Text = "choice 32"; break;
case 2: label3.Text = "choice 33";
button2.Visible = true;
break;
}
break;
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
I don't understand what you're asking. You say that you want to clear the items but the problem is that it doesn't keep the items. That's the whole point of clearing. You need to provide a better explanation of what you're trying to achieve and exactly how the behaviour of your code differs from that.
 

devilonline

New member
Joined
Nov 15, 2020
Messages
3
Programming Experience
Beginner
in the 1st combo box I have 3 items, then when i choose one item, its populates the 2n combobox. so if I chose A it shows a result on combox2 and so on.
so i do a clear items to clear all the information on combox2 and the populate it.
the problem is when i chose any item on combox2 it clears in items on combox2. thanks

for example this code, the problem is the same. when i choose someting on combox2 it clears everything on combox2

private void button1_Click(object sender, EventArgs e)
{

if (comboBox1.SelectedIndex == 0)
{


comboBox2.Items.Clear();
comboBox2.Items.Add(230);
comboBox2.Items.Add(231);
comboBox2.Items.Add(232);
}

ezgif-6-5743d364c718.gif
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,135
Location
Chesapeake, VA
Programming Experience
10+
The event handler you are showing in post #4 is for a button click event handler. But you are saying in post #4 that the combobox is being cleared when an item in the combobox is selected. Perhaps you should show us the code for your selected item changed event handler.
 
Top Bottom