C# 如果在combox1中选择了某个值,则应在所有其他组合框中禁用该值
如果在combox1中选择了某个值,则应在所有其他组合框中禁用该值。 例如,我有4个组合框。 组合框1、组合框2、组合框3、组合框4。 它们都具有相同的值,例如1,2,3,4,5 如果在ComboBox1中选择了值1,则在所有其他框中应禁用该值,并且所有框的值相同??? 谢谢,我需要尽快回复。 等待。C# 如果在combox1中选择了某个值,则应在所有其他组合框中禁用该值,c#,combobox,C#,Combobox,如果在combox1中选择了某个值,则应在所有其他组合框中禁用该值。 例如,我有4个组合框。 组合框1、组合框2、组合框3、组合框4。 它们都具有相同的值,例如1,2,3,4,5 如果在ComboBox1中选择了值1,则在所有其他框中应禁用该值,并且所有框的值相同??? 谢谢,我需要尽快回复。 等待。 M USMAN您必须从其他组合框中删除该元素,例如: comboBox2.Items.Remove(comboBox1.SelectedItem); 您可以通过执行以下操作来处理ComboBox
M USMAN您必须从其他组合框中删除该元素,例如:
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
}
在选择时,您需要将其从其他组合框中删除。 例如
如果您不仅使用1STCOMBOX选择item+从其他组合框中删除,而且还使用通用列表作为组合框数据源;我想你可以使用扩展方法
/// <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;
}
然后,您可以设置哪个组合框的数据源作为您想要的列表是非常快的
顺便说一下。。如果要确保鼠标用户活动选择的combobox项-不是以编程方式选择的,则需要使用SelectionChangeCommitted事件;未选择索引已更改。通过SelectedIndexChange事件,您还将在组合框首次加载时捕获。但使用SelectionChange Committed event Waities(更改已提交事件等待),输入键盘或将鼠标按到组合框的箭头以自动触发,您无法在组合框中停用。仅移除您需要侦听SelectionChanged事件。请实际使用@Darren Davies建议的ComboBox1.SelectedItem方法,而不是我在此建议的字符串1。如果用户选择的项目与1不同,该怎么办?@muhammadusman欢迎您。如果这对你有帮助,请投票并接受答案。
/// <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;
}