matlab listbox列表隐藏,仿listBox写了一个Control控件为item的列表集合

1using System;

2using System.Collections.Generic;

3using System.ComponentModel;

4using System.Data;

5using System.Drawing;

6using System.Text;

7using System.Text.RegularExpressions;

8using System.Windows.Forms;

9

namespace SQLAnalysis

{

publicclass MySelfControlList : Control

{

private System.Windows.Forms.ErrorProvider err;

publicMySelfControlList()

{

InitializeComponent();

this.BackColor = Color.White;

itemList = new ListItemColloction();

if (isNeedVaidate)

{

err = new ErrorProvider();

}

}

[Browsable(false)]

publicbool IsValidated

{

get

{

returnList_Validating();

}

}

[DefaultValue(typeof(Color),"White")]

publicoverride Color BackColor

{

get

{

returnbase.BackColor;

}

set

{

base.BackColor = value;

}

}

private ListItemColloction itemList;

/// 

/// 不提供设计时绑定

/// 

[Bindable(false), Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

publicListItemColloction Items

{

get

{

if (itemList ==null)

itemList = new ListItemColloction();

returnitemList;

}

set

{

itemList = value;

}

}

publicintCount

{

get

{

returnitemList.Count;

}

}

publicvoid RemoveAll()

{

for(inti=this.itemList.Count-1;i>-1;i--)

{

Item item = itemList[i];

item.ItemControl.Parent =null;

itemList.Remove(item);

}

}

publicintAddItem(Item item)

{

itemList.Add(item);

this.Refresh();

returnitemList.IndexOf(item);

}

publicvoid InsertItem(intindex, Item item)

{

itemList.Insert(index, item);

this.Refresh();

}

publicvoid RemoveItem(Item item)

{

item.ItemControl.Parent =null;

itemList.Remove(item);

this.Refresh();

}

publicvoid RemoveItemAt(intindex)

{

itemList[index].ItemControl.Parent =null;

itemList.RemoveAt(index);

this.Refresh();

}

private string emptyText;

publicstring EmptyText

{

get

{

returnemptyText;

}

set

{

emptyText =value;

}

}

[Browsable(false)]

publicoverride string Text

{

get

{

returnbase.Text;

}

set

{

base.Text = value;

}

}

publicstring ItemText(intindex)

{

returnitemList[index].ItemControl.Text;

}

private ToolTip toolTip1;

privateintitemHeight = 25;

publicintItemHeight

{

get

{

returnitemHeight;

}

set

{

itemHeight = value;

}

}

/// 

/// 重写Paint方法

/// 

/// 

protected override void OnPaint(PaintEventArgs e)

{

this.Height = this.itemHeight * (itemList.Count+ 1);

base.OnPaint(e);

if (itemList.Count== 0)

{

e.Graphics.DrawString(this.emptyText, this.Font,Brushes.Black, e.ClipRectangle);

return;

}

//e.Graphics.DrawRectangle(new Pen(Brushes.Black),this.Left,this.Top,this.Width,this.Height);

for(inti = 0; i 

{

Point location = new Point(0, i * this.itemHeight);

Rectangle bound = new Rectangle(location, newSize(this.Size.Width, this.itemHeight));

OnDrawItem(new DrawItemEventArgsMySelf(bound, i, e.Graphics));

}

}

/// 

/// 画每一项

/// 

/// 

protected void OnDrawItem(DrawItemEventArgsMySelf e)

{

Graphics g = e.Graphics;

Pen pen = new Pen(Brushes.Wheat);

g.DrawRectangle(pen, e.Bound);

intindex= e.Index;

if (index> -1 &&index

{

Control ctr = itemList[index].ItemControl;

if (ctr.Parent ==null)

{

ctr.Parent = this;

ctr.Height = e.Bound.Height;

if( ctr.Width >this.Width)

ctr.Width = this.Width - 5;

toolTip1.SetToolTip(ctr, itemList[index].ItemDescribe);

}

ctr.Location = e.Bound.Location;

}

}

publicbool List_Validating()

{  bool isValidated =true;

foreach (Item iteminitemList)

{

isValidated = isValidated && ListItemControls_Validating(item.ItemControl, new CancelEventArgs());

}

returnisValidated;

}

publicbool ListItemControls_Validating(object sender, CancelEventArgs e)

{

Control ctr = senderasControl;

Item item = itemList[ctr];

if (string.IsNullOrEmpty(item.Regex))

returntrue;

Regex regex = new Regex(item.Regex);

string text = ctr.Text.Trim();

if (!regex.IsMatch(text))

{

err.SetIconAlignment(ctr, ErrorIconAlignment.MiddleRight);

err.SetIconPadding(ctr, 3);

err.SetError(ctr, item.ErrorMessage);

returnfalse;

}

err.Clear();

returntrue;

}

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);

this.SuspendLayout();

this.ResumeLayout(false);

}

private IContainer components;

}

publicclass ListItemColloction : List

{

publicItem this[Control itemControl]

{

get

{

for(inti = 0; i 

{

if (this[i].ItemControl.Equals(itemControl))

returnthis[i];

}

returnnull;

}

set

{

for(inti = 0; i 

{

if (this[i].ItemControl.Equals(itemControl))

this[i] = value;

}

}

}

}

[Serializable]

publicclass Item

{

private Control itemControl =null;

private string itemDescible = string.Empty;

private string regex = string.Empty;

private string errorMessage = string.Empty;

publicItem()

{

}

publicItem(Control itemControl, string itemDescible,string regex,string errorMessage)

{

this.itemControl = itemControl;

this.itemDescible = itemDescible;

this.regex = regex;

this.errorMessage = errorMessage;

}

publicItem(Control itemControl, string itemDescible)

{

this.itemControl = itemControl;

this.itemDescible = itemDescible;

}

publicstring Regex

{

get

{

returnregex;

}

set

{

regex = value;

}

}

publicControl ItemControl

{

get

{

returnitemControl;

}

set

{

itemControl = value;

}

}

publicstring ItemDescribe

{

get

{

returnitemDescible;

}

set

{

itemDescible = value;

}

}

publicstring ErrorMessage

{

get

{

returnerrorMessage;

}

set

{

errorMessage = value;

}

}

}

publicclass DrawItemEventArgsMySelf : EventArgs

{

private Rectangle bound = Rectangle.Empty;

privateintindex;

private Graphics graphics;

publicGraphics Graphics

{

get

{

returnthis.graphics;

}

}

publicDrawItemEventArgsMySelf(Rectangle bound,intindex, Graphics g)

{

this.bound = bound;

this.index=index;

this.graphics = g;

}

publicRectangle Bound

{

get

{

returnthis.bound;

}

}

publicintIndex

{

get

{

returnindex;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值