C#.Net自定义控件 - CheckedListBoxEditor(支持多值勾选及新增)
作者:C/S原创  发布日期:2011/03/26 10:35:48
C#.Net自定义控件 - CheckedListBoxEditor(支持多值勾选及新增)

我们在开发中经常有此类应用: 一个字段存储以分隔符间隔的字符串.如",A,B,C,D,", 调号或分号是分隔符. 但在用户界面如何方便快捷的操作这组数据? 为了满足C/S框架快速开发的需求, 我编写了这个自定义控件,命名为CheckedListBoxEditor.


贴图图片

这个控件不仅支持复选还能输入一组新值.



ItemSeperator 属性: 定义一个分隔符,预设为","调号.


关键源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;

namespace CheckedListBoxEditor
{
   /// <summary>
   /// 支持输入的CheckedListBox控件.
   /// </summary>
   public partial class ucCheckedListBoxEditor : UserControl
   {
      private string _EditValue = "";
      private string _Seperator = ",";
      
      public ucCheckedListBoxEditor()
      {
         InitializeComponent();
         
         this.Height = 22;
      }
      
      private void ucCheckedListBoxEditor_Load(object sender, EventArgs e)
      {
         popupEdit.Width = this.Width;
      }
      
      private void ucCheckedListBoxEditor_SizeChanged(object sender, EventArgs e)
      {
         popupEdit.Width = this.Width;
      }
      
      [DefaultValue(",")]
      [Description("多个值中间的分隔符.如:AA,BB,CC")]
      public string ItemSeperator
      {
         get { return _Seperator; }
         set { _Seperator = value; }
      }
      
      [Description("返回列表中已勾选的值,以分隔符隔开")]
      public string EditValue
      {
         get
         {
            _EditValue = this.GetItemCheckedValue();
            return _EditValue;
         }
         set
         {
            _EditValue = value;
            this.SetItemChecked(value);
         }
      }
      
      /// <summary>
      /// 绑定数据源(字符串)
      /// </summary>
      public void BindingDataSource(string[] items)
      {
         foreach (string item in items)
         {
            lists.Items.Add(item, item, CheckState.Unchecked, true);
         }
      }
      
      /// <summary>
      /// 绑定数据源
      /// </summary>
      /// <param name="dataSource">数据源</param>
      /// <param name="valueMember">取值字段</param>
      /// <param name="displayMember">显示字段</param>
      public void BindingDataSource(DataTable dataSource, string valueMember, string displayMember)
      {
         foreach (DataRow row in dataSource.Rows)
         {
            lists.Items.Add(row[valueMember], row[displayMember].ToString(), CheckState.Unchecked, true);
         }
      }
      
      //分解字符串
      private void SetItemChecked(string value)
      {
         string[] items = value.Split(new string[] { _Seperator }, StringSplitOptions.None);
         foreach (CheckedListBoxItem item in lists.Items)
         {
            foreach (string s in items)
            {
               if (s.ToLower() == item.Value.ToString().ToLower())
               {
                  item.CheckState = CheckState.Checked;
                  break;
               }
            }
         }
      }
      
      //获取字符串组合
      private string GetItemCheckedValue()
      {
         string result = "";
         foreach (CheckedListBoxItem item in lists.Items)
         {
            if (item.CheckState == CheckState.Checked)
            {
               result = result "," item.Value;
            }
         }
         
         return result == "" ? "" : result _Seperator;
      }
      
      private EventHandler _OnCheckStateChanged = null;
      
      /// <summary>
      /// 勾选时触发的事件(可用户自定义)
      /// </summary>
      public event EventHandler OnCheckStateChanged
      {
         add { _OnCheckStateChanged = value; }
         remove { _OnCheckStateChanged -= value; }
      }
      
      private void lists_SelectedValueChanged(object sender, EventArgs e)
      {
         if (_OnCheckStateChanged != null) _OnCheckStateChanged(lists, e);
      }
      
      /// <summary>
      /// 勾选时触发的事件
      /// </summary>
      private void lists_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
      {
         this.popupEdit.EditValue = this.EditValue;
         
         if (_OnCheckStateChanged != null) _OnCheckStateChanged(lists, e);
      }
      
      /// <summary>
      /// 自动调整新增项输入框的宽度
      /// </summary>
      private void popupContainerControl1_SizeChanged(object sender, EventArgs e)
      {
         txtNewItem.Width = popupContainerControl1.Width - btnAdd.Width - 16;
      }
      
      /// <summary>
      /// 新增项
      /// </summary>
      private void btnAdd_Click(object sender, EventArgs e)
      {
         if (txtNewItem.Text != string.Empty)
         {
            int index = lists.Items.Add(txtNewItem.Text, txtNewItem.Text, CheckState.Checked, true);
            lists.SelectedIndex = index;
            
            this.popupEdit.EditValue = this.EditValue;
            
            txtNewItem.Text = "";
            txtNewItem.Focus();
         }
      }
      
      /// <summary>
      /// 自定调整宽度
      /// </summary>
      private void popupEdit_QueryPopUp(object sender, CancelEventArgs e)
      {
         if (this.popupContainerControl1.Width < this.Width)
         this.popupContainerControl1.Width = this.Width;
      }
      
   }
}


// 来源:www.CSFramework.com, C/S结构框架学习网




点击下载附件 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇