WinForm ListBox 控件用法
下面演示如何利用列表控件 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 控件用法的更多相关文章
- asp.net Listbox控件用法
2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...
- C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题
解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...
- 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”
解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15 587 很少写WinForm程序第一次使用ListBox控件就遇到了比 ...
- WinForm将一个窗体的值传到另一个窗体的listbox控件,C#
做arcgisengine二次开发,读取当前图层文件的字段值,别名,类型. 读取文件是在有地图图层的窗体(假设为Form1),由于窗体有限,所以想把读取的数据在另一个窗体(假设为Form2)显示出来 ...
- winform窗体控件(全)
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
- WinForm 公共控件
一.窗体属性: 1.AcceptButton - 窗体的“接受”按钮.如果设置该属性,每次用户按“Enter”键都相当于“单击”了该按钮. 需要设置哪个键,就在后面选择. 2.CancelButton ...
- listbox控件使用
1. 属性列表: SelectionMode 组件中条目的选择类型,即多选(Multiple).单选(Single) Rows 列表框中显示总共多少行 Sel ...
- winform基础控件总结
转自:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 基础 - 常用控件 C# WinForm开发系列 - CheckBox/B ...
- ComboxBox控件、checklistbox控件和listbox控件的组合开发
第一步:先创建一个WinForm窗体应用程序,按照下图所示的进行布局. 第二步:为ComboxBox控件.checklistbox控件和listbox控件和button控件设置属性 第三步:在代码中的 ...
随机推荐
- 2015山东信息学夏令营 Day5T3 路径
问题描述: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入: 第一行包含一个正整数n,表示点数. 接下来 ...
- mysql控制流程函数(case,if,ifnull,nullif)
1.case...when... 用法 参考:http://www.cnblogs.com/qlqwjy/p/7476533.html CASE value WHEN [compare-value] ...
- Uva - 11181 Probability|Given (条件概率)
设事件B为一共有r个人买了东西,设事件Ai为第i个人买了东西. 那么这个题目实际上就是求P(Ai|B),而P(Ai|B)=P(AiB)/P(B),其中P(AiB)表示事件Ai与事件B同时发生的概率,同 ...
- Django学习系列之模板
什么是django模板 模板是一个文本,用于分离文档的表现形式和内容,模板定义了占位符以及各种用于规范文档该如何显示的各部分基本逻辑(模板标签) 模板通常用于产生HTML 如何使用模板 创建一个Tem ...
- Scala-LIST/Tuple/Map
环境: CentOS 6.3 LIST(列表) 代码: $ cat list.scala var mylist = List(1,2,3) println(mylist) var mylist1 = ...
- react 项目实战(四)组件化表单/表单控件 高阶组件
高阶组件:formProvider 高阶组件就是返回组件的组件(函数) 为什么要通过一个组件去返回另一个组件? 使用高阶组件可以在不修改原组件代码的情况下,修改原组件的行为或增强功能. 我们现在已经有 ...
- docker+zabbix,使用docker搭建zabbix服务
Zabbix 介绍 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...
- HDU 5475An easy problem 离线set/线段树
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 有意思的RTL函数RegisterClass(在持久化中,你生成的一个新类的对象,系统并不知道他是如何来的,因此需要你注册)good
例子1:Delphi中使用纯正的面向对象方法(这个例子最直接) Delphi的VCL技术使很多程序员能够非常快速的入门:程序员门只要简单的拖动再加上少量的几个Pascal语句,呵呵,一个可以运行得非常 ...
- Bootstrap 过渡效果 transition.js源码分析
前言: 阅读建议:去github下载一个完整dom然后把,本篇代码复制进去然后运行就好了以地址 Bootstrap 自带的 JavaScript 插件的动画效果几乎都是使用 CSS 过渡实现的,那么判 ...