下面演示如何利用列表控件 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. gulp基本语法

    pipe:用管道输送 1.gulp.src(glops[, options]) 输出(Emits)符合所提供的匹配模式(glob)或者匹配模式的数组(array of globs)的文件. 将返回一个 ...

  2. POJ1328 Radar Installation 解题报告

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  3. Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)

    1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来 ...

  4. Ubuntu 16.04安装RapidSVN

    使用RabbitVCS有一些不完美,比如没有把文件增加到版本库的功能,导致无法提交等问题,现在再次安装RapidSVN来弥补这些缺点. 安装: sudo apt-get install rapidsv ...

  5. Mysql的timestamp类型,自动记录数据的更新时间

    datetime也可以设置自动更新的

  6. 详细图解mongodb 3.4.1 win7x64安装

    原文:http://www.cnblogs.com/yucongblog/p/6895983.html 详细图解,记录 win7 64 安装mongo数据库的过程.安装的版本是 MongoDB-win ...

  7. Angular CLI: 1.6.7 入门

    当你使用npm或者yarn也安装不了angular-cli时,请使用淘宝镜像. Step1 npm i -g cnpm --registry=https://registry.npm.taobao.o ...

  8. Linux下完美使用find+grep实现全局代码搜索

    作者:zhanhailiang 日期:2014-10-11 背景 在Window下有大量方便的图形化工具能够实现全局搜索,可是Linuxserver中因为使用命令行操作导致全局搜索是一个比較高的门槛. ...

  9. Python输入输出及其他

    print用法 print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果.回车 \r 本义是光标重 ...

  10. Swift新手教程系列5-函数+selector在swift中的使用方法

    原创blog.转载请注明出处 近期在用swift写代码,尽管遇到一些问题,可是代码量确实减了不少. swfit新手教程系列会随着我使用swfit中的积累,不断地去修正更新 之前的教程 swift单例模 ...