Link to home
Start Free TrialLog in
Avatar of ShannonCallahan
ShannonCallahan

asked on

Create an error message if Combobox.SelectedItem = same as another combo box.

Hello all!

I have a programs with 15 combo boxes that are populated with the following code:

public void Form1_Load(object sender, EventArgs e)
        {
            //Create OleDB connection
            try
            {
                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sccalla\Desktop\accessdb.accdb;
                Persist Security Info=False;";
                connection.Open();
                
                //Select all information from TaskNames Table
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "Select * From TaskNames";
                command.CommandText = query;

                //Display Tasks in Comboboxes
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    taskcombobox1.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox2.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox3.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox4.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox5.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox6.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox7.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox8.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox9.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox10.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox11.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox12.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox13.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox14.Items.Add(reader["Tasknames"].ToString());
                    taskcombobox15.Items.Add(reader["Tasknames"].ToString());
                }
                connection.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }

Open in new window


These combo boxes list a bunch of different tasks that an agent could be working on. Each combo box has a corresponding button and timer that displays the amount of total time spent in that tack by day. Due to the nature of the Item selection, I cannot have two combo boxes share the same value. I need to setup the combo boxes to error message if a user tries to start the corresponding stopwatch by button click, if the current combo box selected item is the same as another combo boxes selected item.
Something like:

//putting this on the start timer button for that combo box
If (taskcombobox2.selecteditem = taskcombobox1.selecteditem) {error and nothing happens}

however, I need to make sure any one of the 15 will not allow any other combo box to have the same value.

Please let me know if further clarification is needed. Thank you!!
Shannon
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You need to use two equals in the if statement.

If (taskcombobox2.selecteditem == taskcombobox1.selecteditem) {error and nothing happens}
ASKER CERTIFIED SOLUTION
Avatar of Craig Wardman
Craig Wardman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShannonCallahan
ShannonCallahan

ASKER

I am missing something. I tried:

private bool HasDuplicateSelections(IEnumerable<ComboBox> allComboBoxes)
        {
            var takenValues = new HashSet<string>();
    
            foreach (var comboBox in allComboBoxes)
            {
                if (!takenValues.Contains(comboBox.SelectedValue))
            {
               takenValues.Add(comboBox.SelectedValue.ToString());
            }
            else
            {
              return true;
            }
        }

            return false;
        }

Open in new window


And

 private void button1_Click(object sender, EventArgs e)
        {
            HasDuplicateSelections();
{

Open in new window


I am getting an error: Error No overload for method 'HasDuplicateSelections' takes 0 arguments
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically Craig has just changed to code with the selected values being stored in a hashset and you check if the selected value is used in that rather than your original method (with the incorrect use of equality checking you had) of checking directly the combobox selections.
What you do after the finding a duplicate is up to you.


ps.  I don't know more details of just what you have but your description sounds like it could be a poor design of the GUI which is making life hard for you now in writing the code.
Thank you!!