資源地址:https://download.youkuaiyun.com/download/losedguest/11461674
功能代碼1:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WebPublicControls.Properties;
namespace WebPublicControls
{
public partial class SimpelCombox : UserControl
{
private ToolStripControlHost SimpleBoxHost;
private ToolStripControlHost ListBoxHost;
private ListBox List = new ListBox();
private System.Windows.Forms.TextBox HideBox = new System.Windows.Forms.TextBox();
private ToolStripDropDown dropDown;
private string _textKey = "";
public event EventHandler TextValueChanged;
public string TextValue
{
get
{
return this.SimpleText.Text;
}
set
{
bool flag = false;
if (this.SimpleText.Text != value)
{
flag = true;
}
this.SimpleText.Text = value;
this._textKey = "";
for (int i = 0; i < this.items.Count; i++)
{
if (!(this.items[i] is ListBoxItems))
{
this._textKey = value;
break;
}
ListBoxItems listBoxItems = (ListBoxItems)this.items[i];
if (listBoxItems.Name == value)
{
this._textKey = listBoxItems.ID;
break;
}
}
if (flag)
{
this.OnTextChanged();
}
}
}
public string TextKey
{
get
{
if (this.List.SelectedItem != null)
{
ListBoxItems listBoxItems = (ListBoxItems)this.List.SelectedItem;
return listBoxItems.ID;
}
return this._textKey;
}
set
{
bool flag = false;
if (this._textKey != value)
{
flag = true;
}
this.SimpleText.Text = "";
for (int i = 0; i < this.items.Count; i++)
{
ListBoxItems listBoxItems = (ListBoxItems)this.items[i];
if (listBoxItems.ID == value)
{
this.SimpleText.Text = listBoxItems.Name;
}
}
this._textKey = value;
if (flag)
{
this.OnTextChanged();
}
}
}
public ListBox.ObjectCollection items
{
get
{
return this.List.Items;
}
}
protected virtual void OnTextChanged()
{
if (this.TextValueChanged != null)
{
this.TextValueChanged(this, EventArgs.Empty);
}
}
public SimpelCombox()
{
this.List.Click += new EventHandler(this.List_Click);
this.List.BorderStyle = BorderStyle.None;
this.List.DisplayMember = "Name";
this.List.ValueMember = "ID";
this.List.MouseMove -= new MouseEventHandler(this.List_MouseMove);
this.List.MouseMove += new MouseEventHandler(this.List_MouseMove);
this.ListBoxHost = new ToolStripControlHost(this.List);
this.ListBoxHost.AutoSize = false;
this.HideBox.BorderStyle = BorderStyle.None;
this.HideBox.Enabled = false;
this.HideBox.Font = new Font("細明體", 1f);
this.SimpleBoxHost = new ToolStripControlHost(this.HideBox);
this.SimpleBoxHost.AutoSize = false;
this.dropDown = new ToolStripDropDown();
this.dropDown.AutoSize = false;
this.dropDown.Items.Add(this.ListBoxHost);
this.dropDown.Items.Add(this.SimpleBoxHost);
this.InitializeComponent();
this.AutoSize = false;
this.DropDownButton.Click += new EventHandler(this.DropDownButton_Click);
}
public void ShowDropDown()
{
if (this.dropDown != null)
{
this.dropDown.Width = base.Width;
this.dropDown.AutoSize = true;
this.dropDown.Show(this, 0, base.Height);
this.List_AutoSColl();
}
}
/// <summary>
/// 點擊選中后,自動關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void List_Click(object sender, EventArgs e)
{
try
{
if (this.List.SelectedIndex > -1)
{
this.dropDown.AutoSize = false;
this.TextValue = this.List.SelectedItem.ToString();
this.List.Width = 10;
this.HideBox.Width = 10;
this.dropDown.Width = 10;
this.dropDown.Hide();
}
}
catch (Exception ex)
{
throw ex;
}
}
private void DropDownButton_Click(object sender, EventArgs e)
{
this.dropDown.AutoSize = true;
this.List.Width = base.Width;
this.HideBox.Width = base.Width;
this.dropDown.Width = base.Width;
this.dropDown.Show(this, 0, base.Height);
this.List_AutoSColl();
}
private void List_MouseMove(object sender, MouseEventArgs e)
{
if (this.items.Count > 0)
{
this.List.Focus();
this.List.SelectedIndex = this.List.IndexFromPoint(e.Location);
}
}
/// <summary>
/// 滾動條,自動滾動到this.TextValue值的位置且將當前項選中
/// </summary>
private void List_AutoSColl()
{
try
{
if (this.List.SelectedItem != null)
{
this.List.TopIndex = this.List.SelectedIndex;
if (this.List.SelectedItem.ToString() == this.TextValue)
{
return;
}
}
if (this.List.Items.Count > 0)
{
for (int i = 0; i < this.items.Count; i++)
{
if (this.items[i] is ListBoxItems)
{
ListBoxItems listBoxItems = (ListBoxItems)this.items[i];
if (listBoxItems.Name == this.TextValue)
{
this.List.SelectedIndex = i;
this.List.TopIndex = i;
break;
}
}
else if (this.items[i].ToString() == this.TextValue)
{
this.List.SelectedIndex = i;
this.List.TopIndex = i;
break;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
功能代碼:2
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 System.Threading;
namespace WebPublicControls
{
public partial class CheckComBox : UserControl
{
/// <summary>
/// 下拉控件 空白填充
/// </summary>
private ToolStripControlHost SimpleBoxHost;
/// <summary>
/// 下拉控件 多選項容器
/// </summary>
private ToolStripControlHost ListBoxHost;
/// <summary>
/// 多選項
/// </summary>
private CheckedListBox List = new CheckedListBox();
private System.Windows.Forms.TextBox HideBox = new System.Windows.Forms.TextBox();
//下拉控件
private ToolStripDropDown dropDown;
/// <summary>
/// 選中項發生變化后發生
/// </summary>
[Category("Property Changed"), Description("選中項發生變化后發生"), DisplayName("ItemCheckedChanged")]
public event EventHandler ItemCheckedChanged;
/// <summary>
/// 當前控件值
/// </summary>
[Category("Appearance"), Description("多個值,逗號隔開"), DisplayName("TextValue")]
public string TextValue
{
get {
return this.SimpleText.Text;
}
}
/// <summary>
/// 設置指定索引項的選中狀態
/// </summary>
/// <param name="inds">索引數組</param>
/// <param name="stat">選中狀態</param>
public void SetCheckedIndexState(int [] inds,bool stat)
{
if (inds.Length > 0)
{
foreach (int val in inds)
{
if (this.items.Count > val)
{
this.List.SetItemChecked(val, stat);
}
}
}
}
public CheckComBox()
{
this.List.BorderStyle = BorderStyle.None;
this.List.DisplayMember = "Name";
this.List.ValueMember = "ID";
this.List.MouseMove -= new MouseEventHandler(this.List_MouseMove);
this.List.MouseMove += new MouseEventHandler(this.List_MouseMove);
this.List.MouseLeave -= new EventHandler(this.List_MouseLeave);
this.List.MouseLeave += new EventHandler(this.List_MouseLeave);
//ItemCheck,發生在選中前
this.List.ItemCheck -= new ItemCheckEventHandler(this.List_ItemCheck);
this.List.ItemCheck += new ItemCheckEventHandler(this.List_ItemCheck);
this.ListBoxHost = new ToolStripControlHost(this.List);
this.ListBoxHost.AutoSize = false;
this.HideBox.BorderStyle = BorderStyle.None;
this.HideBox.Enabled = false;
this.HideBox.Font = new Font("細明體", 1f);
this.SimpleBoxHost = new ToolStripControlHost(this.HideBox);
this.SimpleBoxHost.AutoSize = false;
this.dropDown = new ToolStripDropDown();
this.dropDown.AutoSize = false;
this.dropDown.Items.Add(this.ListBoxHost);
this.dropDown.Items.Add(this.SimpleBoxHost);
this.InitializeComponent();
this.AutoSize = false;
this.DropDownButton.Click += new EventHandler(this.DropDownButton_Click);
}
//下拉選項
public CheckedListBox.ObjectCollection items
{
get
{
return this.List.Items;
}
}
/// <summary>
/// 顯示下拉窗
/// </summary>
public void ShowDropDown()
{
if (this.dropDown != null)
{
//下拉窗寬度為開窗控件的寬度,
this.dropDown.Width = base.Width;
this.dropDown.AutoSize = true;
this.dropDown.Show(this, 0, base.Height);
}
}
private void DropDownButton_Click(object sender, EventArgs e)
{
this.dropDown.AutoSize = true;
this.List.Width = base.Width;
this.HideBox.Width = base.Width;
this.dropDown.Width = base.Width;
this.dropDown.Show(this, 0, base.Height);
}
/// <summary>
/// 鼠標移動下拉窗時,自動獲取焦點,防止點兩次才能選中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void List_MouseMove(object sender, MouseEventArgs e)
{
if (this.items.Count > 0)
{
this.List.Focus();
this.List.SelectedIndex = this.List.IndexFromPoint(e.Location);
}
}
/// <summary>
/// 鼠標離開下拉窗自動關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void List_MouseLeave(object sender, EventArgs e)
{
if (this.dropDown != null)
{
this.dropDown.AutoSize = false;
this.List.Width = 10;
this.HideBox.Width = 10;
this.dropDown.Width = 10;
this.dropDown.Hide();
}
}
/// <summary>
/// 下拉選項發生變化時
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void List_ItemCheck(object sender, ItemCheckEventArgs e)
{
//當前CheckedItems的還未發生變化
//在此需要延時觸發itemCheckChange()
this.BeginInvoke(new EventHandler(delegate{itemCheckChange(); }));
}
private void itemCheckChange()
{
try
{
Thread.Sleep(1);
this.SimpleText.Text = "";
if (this.List.CheckedItems != null)
{
for (int i = 0; i < this.List.CheckedItems.Count; i++)
{
if (this.List.CheckedItems[i] is ListBoxItems)
{
ListBoxItems listBoxItems = (ListBoxItems)this.List.CheckedItems[i];
if (this.SimpleText.Text == "")
{
this.SimpleText.Text = listBoxItems.ID;
}
else
{
this.SimpleText.Text += "," + listBoxItems.ID;
}
}
}
}
if (ItemCheckedChanged != null)
{
ItemCheckedChanged(this, EventArgs.Empty);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
功能代碼2