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个下一个即可看到以下界面)

  • 相关阅读:
    Ruby:Hash 排序
    Rails bug: ROR + A server is already running. Check …/tmp/pids/server.pid. Exiting
    MySQL 删除数据的最好的方式
    FATAL ERROR: The persistent cache of section information does not match the current configu...
    http和https的区别
    SAP BusinessObject < Aggregate Navigation >
    BO Server Session Setting
    BusinessObject Port 配置
    重复提交
    FCKeditor
  • 原文地址:https://www.cnblogs.com/hellowzl/p/6809483.html
Copyright © 2011-2022 走看看