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

  • 相关阅读:
    为什么你应该(从现在开始就)写博客
    ASP.net 中使用Flexigrid详细教程之二直接使用数据库数据(有图有真相)
    保护眼睛的方法 (眼睛累了吗 来看看吧)
    程序员不如快递员?
    项目管理界面
    地址栏射击游戏!对,你没看错,就是在地址栏上玩的游戏,有图有真相!
    书写是为了更好的思考
    IT人员如何找到自己的时间?
    std::mem_fun_ref,mem_fun1_ref分析
    __declspec(selectany) 的作用是什么
  • 原文地址:https://www.cnblogs.com/hellowzl/p/6809483.html
Copyright © 2011-2022 走看看