zoukankan      html  css  js  c++  java
  • 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 > 0)
                {
                    object[] items = new object[this.listBox1.SelectedItems.Count];
    
                    this.listBox1.SelectedItems.CopyTo(items, 0);
    
                    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 > 0)
                {
                    object[] items = new object[this.listBox2.SelectedItems.Count];
    
                    this.listBox2.SelectedItems.CopyTo(items, 0);
    
                    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 > 0)
                {
                    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 > 0)
                {
                    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个下一个即可看到以下界面)

  • 相关阅读:
    注册登录过程点滴(一):初始的想法分享是王道
    根据Cron表达式,通过Spring自带的CronSequenceGenerator类获取下次执行时间
    解决jqGrid中,当前页一直显示为0的问题
    使用JDK自带功能,实现一个简单的Web Service接口发布
    Linux 僵尸进程 ( Zombie or defunct )
    C语言赋值操作符
    面试题 ( ++a )和( a++ )
    关于学习Linux的经典书籍
    C语言中的 sizeof 问题
    条件变量 pthread_cond_wait ()
  • 原文地址:https://www.cnblogs.com/hellowzl/p/6809483.html
Copyright © 2011-2022 走看看