Link to home
Start Free TrialLog in
Avatar of dustock
dustockFlag for United States of America

asked on

2 combo boxes populated with 1 array, remove vaule from combo 2 if its selected in combo1

I have 2 combo boxes on a form for approvals.  I have 1 array loading the names of everyone from 2 sepeate binding sources as you can see in the code below.  Everything works fine, except when I try to remove the item from the second box if its been selected in the first.

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };
BindingSource source1 = new BindingSource();
BindingSource source2 = new BindingSource();
source1.DataSource = approvers;
cmbFirstApprover.DataSource = source1;
source2.DataSource = approvers;
cmbSecondApprover.DataSource = source2;
        
private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Remove person from second combo
            string firstApprover = cmbFirstApprover.Text;

             //source2.Remove(firstApprover);

             //cmbSecondApprover.Items.Remove(firstApprover);
          }

Open in new window



source2.Remove(firstApprover);
This generats the error below when the approval form loads, even though its in the selectedIndexChanged    
NotSupportedException was unhandled by user code
Collection was of a fixed size  

This generates the error below when I make a selection on the first combo box.
ArgumentException was unhandled
Items collection cannot be modified when the DataSource property is set.
Avatar of strickdd
strickdd
Flag of United States of America image

As the error says, you can't remove items when a combo box is bound to a data source. You will have to manually add the items.

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

foreach(string approver in approvers)
{
    cmdFirstApprover.Items.Add(new ListItem(approver));
    cmbSecondApprover.Items.Add(new ListItem(approver));
}

Open in new window

Avatar of dustock

ASKER

strickdd, all this does for me is get rid of the error messages.  It doest remove the item from the second combo box.  My code to display it worked, its my code to remove the item that's not working.

Thanks,

Dustin
Did you add back in the code to remove the item from the combo box when the first one is selected?
Avatar of dustock

ASKER

I sure did, it didnt do anything.
Can you post your code?
Avatar of dustock

ASKER

I tried both ways to remove again, neither one does anything.

private void TE_IIDB_APPROVAL_Load(object sender, EventArgs e)
{
string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

foreach (string approver in approvers)
{
     cmbFirstApprover.Items.Add(new ListItem(approver));
     cmbSecondApprover.Items.Add(new ListItem(approver));
}
}

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
         source2.Remove(firstApprover);
         //cmbSecondApprover.Items.Remove(firstApprover);
       }
}

Open in new window

You're no longer using source2 because you are manually binding the information. Just remove the item from the cmbSecondApprover:

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
         cmbSecondApprover.Items.Remove(firstApprover);
       }
} 

Open in new window

Avatar of dustock

ASKER

I tried the cmbSecondApprove.Items.Remove, and that didnt work either.
That is interesting... the other suggestion I have would be this:

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

private void TE_IIDB_APPROVAL_Load(object sender, EventArgs e)
{
foreach (string approver in approvers)
{
     cmbFirstApprover.Items.Add(new ListItem(approver));
     cmbSecondApprover.Items.Add(new ListItem(approver));
}
}

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
           cmdSecondApprover.Items.Clear();

           foreach (string approver in approvers)
{
if(approver != firstApprover)
{
   cmbSecondApprover.Items.Add(new ListItem(approver));
}
}
       }
} 

Open in new window

Avatar of dustock

ASKER

I'll give that a shot and update you tomorrow.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of dustock
dustock
Flag of United States of America 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 dustock

ASKER

using List <string> I was able to remove the item before populating the second combo box.