English 中文(简体)
如果在 combox1 中选中一个数值, 那么它应该在所有其他组合框中禁用
原标题:if a value selected in combox1 then it should be disable in all other combo boxes
  • 时间:2012-05-25 11:18:02
  •  标签:
  • c#
  • combobox

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

问题回答

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

comboBox2.Items.Remove(comboBox1.SelectedItem);

您可以通过下列方式处理 ComboBox1 on Change 事件:

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

On selection you would then need to remove it from the other combo boxes. eg.

//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());
}

如果您不仅使用 1stcomboBox 来选择项目 + 删除, 并且使用通用列表作为组合框数据源; i 猜测您可以使用扩展方法

    /// <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;
        }

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

by the way. 。 如果您想要确定鼠标选择的组合框项( 用户活动 - 不是程序化的) 您需要使用选中的更改事件; 不是选中的 Index 更改 。 在选中的 Index Change 事件第一次装入组合框时, 您也会抓住它。 但是, 选择的改变承诺事件会等待键盘或按鼠标键到组合框箭头射击本身 。





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签