如果在combox1中选择了一个值,那么它应该在所有其他组合框中禁用

时间:2012-05-25 11:18:02

标签: c# combobox

如果在combox1中选择了一个值,那么它应该在所有其他组合框中禁用。 例如,我有4个组合框。 ComboBox1,ComboBox2,ComboBox3,ComboBox4。 所有都具有相同的值,例如(1,2,3,4,5) 如果在ComboBox1中选择了值1,那么它应该在所有其他框中禁用并且对于所有框都是相同的??? 谢谢,我需要回复Quickl。 等候。 M USMAN

3 个答案:

答案 0 :(得分:0)

您必须从其他组合框中删除该元素,例如:

comboBox2.Items.Remove(comboBox1.SelectedItem);

您可以执行以下操作处理ComboBox1 OnChange事件:

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
     // remove the item in the other lists based upon ComboBox1 selection
}

答案 1 :(得分:0)

在选择时,您需要将其从其他组合框中删除。 例如

//On item selected in ComboBox1
private void showSelectedButton_Click(object sender, System.EventArgs e) 
{

    comboBox2.Items.Remove(comboBox1.SelectedIndex.ToString());
    comboBox3.Items.Remove(comboBox1.SelectedIndex.ToString());
    comboBox4.Items.Remove(comboBox1.SelectedIndex.ToString());
}

答案 2 :(得分:0)

如果您不仅使用1stcomboBox从其他人中选择项目+删除,并使用通用列表作为组合框数据源;我想你可以使用扩展方法

    /// <summary>
    /// returns a new List<T> without the List<T> which won't have the given parameter 
    ///
    /// Example Usage of the extension method :
    ///
    /// List<int> nums = new List<int>() { 1, 2, 3, 4, 5 };
    /// 
    /// List<int> i = nums.Without(3);
    /// 
    /// </summary>
    /// <typeparam name="TList"> Type of the Caller Generic List </typeparam>
    /// <typeparam name="T"> Type of the Parameter </typeparam>
    /// <param name="list"> Name of the caller list </param>
    /// <param name="item"> Generic item name which exclude from list </param>
    /// <returns>List<T> Returns a generic list </returns>

    public static TList Without<TList, T>(this TList list, T item) where TList : IList<T>, new()
        {
            TList l = new TList();

            foreach (T i in list.Where(n => !n.Equals(item)))
            {
                l.Add(i);
            }

            return l;
        }

然后您可以根据需要设置哪个组合框的数据源(列表非常快)

按顺序..如果你想确定鼠标选择的组合框项目(用户活动 - 不是以编程方式),你需要使用SelectionChangeCommitted事件; not SelectedIndexChanged。使用SelectedIndexChange事件,您还将在组合框第一次加载时捕获。但是使用SelectionChange Committed事件等待输入键盘或按下鼠标到组合框的箭头以自行激活