开发者

Preventing selection of the same value in seperate comboboxes on a C# form

开发者 https://www.devze.com 2023-03-12 20:12 出处:网络
I\'m building a form application in c#. I have between 6-12 different comboboxes (varying depending on other options specific to this program, but that\'s beside the point), each with the same 6 sel开

I'm building a form application in c#. I have between 6-12 different comboboxes (varying depending on other options specific to this program, but that's beside the point), each with the same 6 sel开发者_C百科ections.

What would be the easiest way to prevent a user from selecting the same item in more than a single combobox?


What if you were to just create a method to handle the SelectedIndexChanged event for the combo boxes, set them all up to use the same method, and do some quick validation, like so:

private void TestUniqueSelection(object sender, System.EventArgs e)
{
      var controls = new List<System.Windows.Forms.ComboBox>();
      controls.Add(...); // <-- Add all of your controls here.

      ComboBox changedBox = (ComboBox) sender;

      if (controls
           .Where(a => a != changedBox && a.SelectedItem == changedBox.SelectedItem)
           .Count() > 0)
           MessageBox.Show("Selected Option has already been chosen");
}

Obviously you could make it more efficient by having the List<...> be a private member variable and only set it up once, but I wanted to be succinct and understandable in the sample code.


You have 2 options that I can come up with off hand; preemptive and responsive.

@Steve has given a quick example of responsive (react to a select event an make sure it doesn't duplicate)

In a preemptive approach you call a method to remove the value from all other combo boxes on the form so that it can't be selected.

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace SO_Forms_Demo
    {
        public partial class ComboBoxes : Form
        {
            public ComboBoxes()
            {
                InitializeComponent();
            }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //do whatever else you want to do here....

                removeDuplicateValues((ComboBox)sender, ((ComboBox)sender).SelectedItem.ToString());
            }

//....Through ...//
            private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
            {
                //do whatever else you want to do here....

                removeDuplicateValues((ComboBox)sender, ((ComboBox)sender).SelectedItem.ToString());
            }

            private void removeDuplicateValues(ComboBox sender, string value)
            {
                //get all of the comboboxes in a collection
                List<ComboBox> comboboxes = this.Controls.OfType<ComboBox>().ToList<ComboBox>();
                foreach (ComboBox cb in comboboxes)
                {
                    if (cb != sender)
                    {
                        cb.Items.Remove(value);
                    }
                }
            }
        }
    }

once you have this inplace there are many ways to make it more robust (keep track of previous values and insert them back if changes are made, etc.) but this answers your basic question.

0

精彩评论

暂无评论...
验证码 换一张
取 消