下面演示如何利用列表控件 ListBox 实现多选与移动选项:

using IMS.WinFormClient.UserControls;
using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace IMS.WinFormClient
{
public partial class ListBoxForm : Form
{
UCReport _uCReport = null; // parentForm public ListBoxForm(UCReport uCReport, List<KeyValuePair<string, string>> selectedPairs, List<KeyValuePair<string, string>> unselectedPairs)
{
InitializeComponent(); _uCReport = uCReport; if (selectedPairs != null)
{
foreach (var field in selectedPairs)
{
this.listBox2.Items.Add(field.Key);
}
} if (unselectedPairs != null)
{
foreach (var field in unselectedPairs)
{
this.listBox1.Items.Add(field.Key);
}
}
} //全部移动:左->右
private void button1_Click(object sender, EventArgs e)
{
foreach (object o in listBox1.Items)
{
listBox2.Items.Add(o);
}
listBox1.Items.Clear();
} //只移动选中项:左->右
private void button2_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedItems.Count > )
{
object[] items = new object[this.listBox1.SelectedItems.Count]; this.listBox1.SelectedItems.CopyTo(items, ); foreach (var item in items)
{
string selectedItem = item.ToString();
//判断是否添加到listbox1
if (!this.listBox2.Items.Contains(selectedItem))
{
//添加人员到listbox2中
this.listBox2.Items.Add(selectedItem);
//移除listbox1中
this.listBox1.Items.Remove(selectedItem);
}
}
}
} //只移动选中项:右->左
private void button3_Click(object sender, EventArgs e)
{
if (this.listBox2.SelectedItems.Count > )
{
object[] items = new object[this.listBox2.SelectedItems.Count]; this.listBox2.SelectedItems.CopyTo(items, ); foreach (var item in items)
{
string selectedItem = item.ToString();
//判断是否添加到listbox1
if (!this.listBox1.Items.Contains(selectedItem))
{
//添加人员到listbox1中
this.listBox1.Items.Add(selectedItem);
//移除listbox2中
this.listBox2.Items.Remove(selectedItem);
}
}
}
} //全部移动:右->左
private void button4_Click(object sender, EventArgs e)
{
foreach (object o in listBox2.Items)
{
listBox1.Items.Add(o);
}
listBox2.Items.Clear();
} private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
} private void btnOk_Click(object sender, EventArgs e)
{
List<string> selectedFieldsList = new List<string>();
if (this.listBox2.Items.Count > )
{
foreach (var item in this.listBox2.Items)
{
selectedFieldsList.Add(item.ToString());
}
}
else
{
MessageBox.Show("请至少选择一项");
return;
} List<string> unselectedFieldsList = new List<string>();
if (this.listBox1.Items.Count > )
{
foreach (var item in this.listBox1.Items)
{
unselectedFieldsList.Add(item.ToString());
}
} // 通知父窗体更新需要显示的字段
_uCReport.CustomFeildsEvent(selectedFieldsList, unselectedFieldsList); this.Close();
}
}
}

运行结果:

更全面的功能可以参考微软的 SQL Server Business Inteligence 的 Integration Services 项目下的选择界面:

(打开vs->新建Integration Services项目->添加数据源视图->选择3个下一个即可看到以下界面)

WinForm ListBox 控件用法的更多相关文章

  1. asp.net Listbox控件用法

    2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...

  2. C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...

  3. 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15    587   很少写WinForm程序第一次使用ListBox控件就遇到了比 ...

  4. WinForm将一个窗体的值传到另一个窗体的listbox控件,C#

    做arcgisengine二次开发,读取当前图层文件的字段值,别名,类型.  读取文件是在有地图图层的窗体(假设为Form1),由于窗体有限,所以想把读取的数据在另一个窗体(假设为Form2)显示出来 ...

  5. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  6. WinForm 公共控件

    一.窗体属性: 1.AcceptButton - 窗体的“接受”按钮.如果设置该属性,每次用户按“Enter”键都相当于“单击”了该按钮. 需要设置哪个键,就在后面选择. 2.CancelButton ...

  7. listbox控件使用

    1. 属性列表: SelectionMode    组件中条目的选择类型,即多选(Multiple).单选(Single)    Rows             列表框中显示总共多少行    Sel ...

  8. winform基础控件总结

    转自:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 基础 - 常用控件 C# WinForm开发系列 - CheckBox/B ...

  9. ComboxBox控件、checklistbox控件和listbox控件的组合开发

    第一步:先创建一个WinForm窗体应用程序,按照下图所示的进行布局. 第二步:为ComboxBox控件.checklistbox控件和listbox控件和button控件设置属性 第三步:在代码中的 ...

随机推荐

  1. codevs4437 YJQ Arranges Sequences

    题目描述 Description 神犇YJQ有两个长度均为n的数列A和B,并且A是一个单调不增的数列.他认为这两个数列的优美度为.有一天YJQ很无聊,他把Bi进行重新排列,得到了许多不同的优美度.他想 ...

  2. linux程序分析工具

    ldd和nm是Linux下两个非常实用的程序分析工具.ldd是用来分析程序运行时需要依赖的动态链接库的工具,nm是用来查看指定程序中的符号表信息的工具,objdump用来查看源代码与汇编代码,-d只查 ...

  3. POJ 2186 tarjan+缩点 基础题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37111   Accepted: 15124 De ...

  4. JDBC的流数据

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html: PreparedStatement对象必须具备使用输入和输出流 ...

  5. Mybatis中的ognl表达式。及myabtis where标签/if test标签/trim标签

    1.mybatis默认支持使用ognl表达式来生成动态sql语句 MyBatis中可以使用OGNL的地方有两处: 动态SQL表达式中 ${param}参数中 上面这两处地方在MyBatis中处理的时候 ...

  6. C# 复制和克隆认识浅谈

    如有雷同,不胜荣欣.若转载,请注明 在C#中,用HashTable,DataTable等复制和克隆浅谈,以下直接看样例 HashTable ht = null; ht = new HashTable( ...

  7. Java中Comparator接口和Comparable接口的使用

    普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...

  8. promise && than

    Promise 构造函数接受一个函数作为参数,该函数的2个参数分别是 resolve 和 reject.他们是2个函数,有 JavaScript 引擎提供,不用自己部署.   var promise ...

  9. selenium第三课(selenium八种定位页面元素方法)

    selenium webdriver进行元素定位时,通过seleniumAPI官方介绍,获取页面元素的方式一共有以下八种方式,现按照常用→不常用的顺序分别介绍一下. 官方api地址:https://s ...

  10. 打开google 新地址

    还在为谷歌打不开而发愁吗? 那就试试这个吧 91.213.30.151