下面演示如何利用列表控件 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. LoadRunner脚本编写

    性能測试project师要懂代码么?答案是必须的.好多測试员觉得在loadrunner中编写脚本非常难非常牛X ,主要是大多測试人员并未做过开发工作,大学的那点程序基础也忘记的几乎相同了. 还有非计算 ...

  2. linux man 1,2,3 命令

    原文: http://blog.sina.com.cn/s/blog_969c52730101c0p7.html ------------------------------------------- ...

  3. bzoj 1266 [AHOI2006] 上学路线 route 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23989499 [原题] 1266: [AHOI2006]上学路线route Time ...

  4. python内置全局变量

    vars()查看内置全局变量 以字典方式返回内置全局变量 #!/usr/bin/env python # -*- coding:utf8 -*- print(vars()) #输出 # {'__bui ...

  5. js的调用函数前先执行某语句问题

    js的调用函数前先执行某语句问题 标签: web前端面试 2015-09-29 17:48 1455人阅读 评论(0) 收藏 举报  分类: js(5)  版权声明:本文为博主原创文章,未经博主允许不 ...

  6. SSL和SSH的差别

    有人说,SSH通常是用来提供安全的登录用的.SSL仅仅是一个在协议层中增加的一层用来提供安全.    SSH工作在TCP之上,能够在启动一个SSH应用后.在其通道里执行其他协议的应用.如邮件.    ...

  7. 图像处理之基础---用Shader实现的YUV到RGB转换:使用3重纹理实现 .

    上一篇中,我是用一个RGB格式的纹理来存储每一帧的画面,其中纹理为m_FrameWidth * m_FrameHeight大小,这样,在内存中,就必须要先对YUV的数据进行排序,然后才能当做RGB的数 ...

  8. VC++ 模拟&quot;CLICK事件&quot;关闭指定窗体

    今天改动一个工具时遇到一个有意思的问题,打开某个窗体时弹出一些不相关的窗体.须要用户自己去手动点击后才干继续.保证不了自己主动处理,如今解说决方案记录一下,例如以下 主要使用windows提供的Fin ...

  9. DSPC6748中某问题的解决方式

    因为之前没有做过DSP相关的开发,属于菜鸟中的菜鸟.出现故障后.不知道从哪方面来解决这些小问题. 开发环境:CCS5.5.0 开发板:TI公司的TMS320C6748 问题: 控制台出现初始化结束后多 ...

  10. C#计算运行时间

    using System.Diagnostics; private Stopwatch stw = new Stopwatch(); stw.Start(); stw.Stop(); MessageB ...