反转c#.net中两个下拉列表中的选定项目

时间:2018-12-19 11:08:43

标签: c# .net

我设计了带有两个下拉列表的Windows窗体应用程序。

每个删除列表包含四个经过硬编码的项目。

当单击一个按钮时,它应该反转所选项目。我想到了使用if条件得出解决方案。但是,任何简短的代码都会有所帮助。

谢谢

1 个答案:

答案 0 :(得分:-1)

When the button is clicked, you could reasign the data sources of both drop downs:

 // create new list of items in order to not lose them when clearin
 var oldSource = comboBox1.Items.ToList();

 // clear old items to make them not reappear
 comboBox1.Items.Clear();

 // reverse items
 var reversedSource = oldSource.Reverse();

 // iterating over the reversedItems and adding all items
 foreach(var item in reversedSource)
 {
   comboBox1.Items.Add(item);
 }