13
Reply

ComboBoxes and selected items - Urgent help Please

Rano AH

Rano AH

Sep 18 2013 1:40 PM
3.6k
Hi 


I need urgent help please on the following. this is a homework and after several hours I need to submit. I spent lot of time to check what is going wrong, but couldn't 

Im attaching my implementation for four comboBoxes which will be filled automatically by items. the application runs fine when I click on show items button first time.. it will show which item I selected from each comboBox, and when I need to select different items I need to select first from the first comboBox then second then third then fourth, otherwise I will get unhandled exception. you will find also some snapshots for my application and the error when I try to select from other comboBoxes before i select from comboBox1.

I really hope to have some help on this. Please correct my implentation to allow me to do this:

- each comboBox will show all the items first time i run my application
- ComboBoxes 1, 2,3 and 4 each must show Items [0],[1],[2],[3] respectively first time I run the applicaiton
- When I click on showItems button, it will give the current selection of items (first and default is items[0][1][2][3],
- When I select different item from one or more comboBoxes then the text box value will be changed as well based on the new selection.  please and let me know what is the right implementation for this. 



namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        string[] Items = { "Customer ID", "Name", "Age","Gender", "Address1", "Address2", "Country" };
        string[] selectedItems = new string[4];


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDefault();
        }
        

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedItem = comboBox1.SelectedItem.ToString(); 
            string[] exclusions = { selectedItem }; 
            Fill(comboBox2, exclusions);
            Fill(comboBox3, exclusions);
            Fill(comboBox4, exclusions);
            selectedItems = exclusions;
            selectedItems = exclusions;

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1) comboBox2.SelectedIndex = -1;
            if (comboBox2.SelectedIndex == -1) return;
            string selectedItem1 = comboBox1.SelectedItem.ToString();
            string selectedItem2 = comboBox2.SelectedItem.ToString();
            string[] exclusions = { selectedItem1, selectedItem2 };
            Fill(comboBox3, exclusions);
            Fill(comboBox4, exclusions);
            selectedItems = exclusions;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1) comboBox3.SelectedIndex = -1;
            if (comboBox3.SelectedIndex == -1) return;
            string selectedItem1 = comboBox1.SelectedItem.ToString();
            string selectedItem2 = comboBox2.SelectedItem.ToString();
            string selectedItem3 = comboBox3.SelectedItem.ToString();
            string[] exclusions = { selectedItem1, selectedItem2, selectedItem3 };
            Fill(comboBox4, exclusions);



        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1 || comboBox3.SelectedIndex == -1) comboBox4.SelectedIndex = -1; 
            if (comboBox4.SelectedIndex == -1) return;
            string selectedItem1 = comboBox1.SelectedItem.ToString();
            string selectedItem2 = comboBox2.SelectedItem.ToString();
            string selectedItem3 = comboBox3.SelectedItem.ToString();
            string selectedItem4 = comboBox4.SelectedItem.ToString();
            string[] exclusions = { selectedItem1, selectedItem2, selectedItem3, selectedItem4 };
            selectedItems = exclusions;
        }



       
        private void Fill(ComboBox cb, string[] exclusions)
        {
            cb.Items.Clear();

            foreach (string item in Items)
            {
                bool exclude = false;

                foreach (string exclusion in exclusions)
                {
                    if (item == exclusion)
                    {
                        exclude = true;
                        break;
                    }
                }

                if (!exclude) cb.Items.Add(item);
            }

            cb.SelectedIndex = -1;
        }



        private void LoadDefault()
        {
            comboBox1.Items.AddRange(Items);
            comboBox2.Items.AddRange(Items);
            comboBox3.Items.AddRange(Items);
            comboBox4.Items.AddRange(Items);

            comboBox1.Text = Items[0];
            comboBox2.Text = Items[1];
            comboBox3.Text = Items[2];
            comboBox4.Text = Items[3];

            selectedItems[0] = Items[0];
            selectedItems[1] = Items[1];
            selectedItems[2] = Items[2];
            selectedItems[3] = Items[3];
            
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
            comboBox3.Enabled = true;
            comboBox4.Enabled = true;


            checkBox1.Checked = Enabled;
            checkBox2.Checked = Enabled;
            checkBox3.Checked = Enabled;
            checkBox4.Checked = Enabled;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString() + comboBox2.SelectedItem.ToString() + comboBox3.SelectedItem.ToString() + comboBox4.SelectedItem.ToString();
        }



    }
}










Answers (13)