仿listBox写了一个Control控件为item的列表集合

简介:
  仿listBox写了一个Control控件为item的列表集合,由于最近做个项目要用,微软提供的控件实现起来不行,但自己写了一个,效果如下:

代码
复制代码
  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Drawing;
  6 using  System.Text;
  7 using  System.Text.RegularExpressions;
  8 using  System.Windows.Forms;
  9
 10 namespace  SQLAnalysis
 11 ExpandedBlockStart.gif {
 12    public class MySelfControlList : Control
 13ExpandedSubBlockStart.gif    {
 14        private System.Windows.Forms.ErrorProvider err;
 15        public MySelfControlList()
 16ExpandedSubBlockStart.gif        {
 17            InitializeComponent();
 18            this.BackColor = Color.White;
 19            itemList = new ListItemColloction();  
 20            if (isNeedVaidate)
 21ExpandedSubBlockStart.gif            {
 22                err = new ErrorProvider();
 23            }

 24        }

 25       
 26
 27        
 28       
 29        [Browsable(false)]
 30        public bool IsValidated
 31ExpandedSubBlockStart.gif        {
 32            get
 33ExpandedSubBlockStart.gif            {
 34                return List_Validating(); 
 35            }

 36        }

 37        [DefaultValue(typeof(Color), "White")]
 38        public override Color BackColor
 39ExpandedSubBlockStart.gif        {
 40            get
 41ExpandedSubBlockStart.gif            {
 42                return base.BackColor; 
 43            }

 44            set
 45ExpandedSubBlockStart.gif            {
 46                base.BackColor = value;
 47            }

 48        }

 49        private ListItemColloction itemList;
 50        
 51ExpandedSubBlockStart.gif       /// <summary>
 52       /// 不提供设计时绑定
 53       /// </summary>

 54        [Bindable(false), Browsable(false)]
 55        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
 56        public ListItemColloction Items
 57ExpandedSubBlockStart.gif        {
 58            get
 59ExpandedSubBlockStart.gif            {
 60                if (itemList == null)
 61                    itemList = new ListItemColloction();
 62                return itemList;
 63            }

 64            set
 65ExpandedSubBlockStart.gif            {
 66                itemList = value;
 67            }

 68        }

 69        public int Count
 70ExpandedSubBlockStart.gif        {
 71            get
 72ExpandedSubBlockStart.gif            {
 73                return itemList.Count;
 74            }

 75        }

 76        public void RemoveAll()
 77ExpandedSubBlockStart.gif        {
 78            forint i=this.itemList.Count -1;i>-1;i--)
 79ExpandedSubBlockStart.gif            {
 80                Item item = itemList[i];
 81                item.ItemControl.Parent = null;
 82                itemList.Remove(item);
 83            }

 84            
 85        }

 86        public int AddItem(Item item)
 87ExpandedSubBlockStart.gif        {
 88            itemList.Add(item);
 89            this.Refresh();
 90            return itemList.IndexOf(item);
 91        }

 92        public void InsertItem(int index, Item item)
 93ExpandedSubBlockStart.gif        {
 94            itemList.Insert(index, item);
 95            this.Refresh();
 96
 97        }

 98        public void RemoveItem(Item item)
 99ExpandedSubBlockStart.gif        {
100            item.ItemControl.Parent = null;
101            itemList.Remove(item);
102            this.Refresh();
103        }

104        public void RemoveItemAt(int index)
105ExpandedSubBlockStart.gif        {
106            itemList[index].ItemControl.Parent = null;
107            itemList.RemoveAt(index);
108            this.Refresh();
109        }

110        private string emptyText;
111        public string EmptyText
112ExpandedSubBlockStart.gif        {
113            get
114ExpandedSubBlockStart.gif            {
115             return emptyText;
116            }

117            set
118ExpandedSubBlockStart.gif            {
119             emptyText =value;
120            }

121        }

122        [Browsable(false)]
123        public override string Text
124ExpandedSubBlockStart.gif        {
125            get
126ExpandedSubBlockStart.gif            {
127                return base.Text;
128            }

129            set
130ExpandedSubBlockStart.gif            {
131                base.Text = value;
132            }

133        }

134
135       
136
137        public string ItemText(int index)
138ExpandedSubBlockStart.gif        {
139            return itemList[index].ItemControl.Text;
140        }

141
142        private ToolTip toolTip1;
143        private int itemHeight = 25;
144        public int ItemHeight
145ExpandedSubBlockStart.gif        {
146            get
147ExpandedSubBlockStart.gif            {
148                return itemHeight;
149            }

150
151            set
152ExpandedSubBlockStart.gif            {
153                itemHeight = value;
154            }

155        }

156ExpandedSubBlockStart.gif        /// <summary>
157        /// 重写Paint方法
158        /// </summary>
159        /// <param name="e"></param>

160        protected override void OnPaint(PaintEventArgs e)
161ExpandedSubBlockStart.gif        {
162
163            this.Height = this.itemHeight * (itemList.Count + 1);
164            base.OnPaint(e);
165            if (itemList.Count == 0)
166ExpandedSubBlockStart.gif            {
167                e.Graphics.DrawString(this.emptyText, this.Font,Brushes.Black, e.ClipRectangle);
168                return;
169            }

170            
171           
172            //e.Graphics.DrawRectangle(new Pen(Brushes.Black),this.Left,this.Top,this.Width,this.Height);
173            for (int i = 0; i < itemList.Count; i++)
174ExpandedSubBlockStart.gif            {
175                Point location = new Point(0, i * this.itemHeight);
176                Rectangle bound = new Rectangle(location, new Size(this.Size.Width, this.itemHeight));
177                OnDrawItem(new DrawItemEventArgsMySelf(bound, i, e.Graphics));
178            }

179        }

180ExpandedSubBlockStart.gif        /// <summary>
181        /// 画每一项
182        /// </summary>
183        /// <param name="e"></param>

184        protected void OnDrawItem(DrawItemEventArgsMySelf e)
185ExpandedSubBlockStart.gif        {
186            Graphics g = e.Graphics;
187            Pen pen = new Pen(Brushes.Wheat);
188            g.DrawRectangle(pen, e.Bound);
189
190            int index = e.Index;
191            if (index > -1 && index < itemList.Count)
192ExpandedSubBlockStart.gif            {
193                Control ctr = itemList[index].ItemControl;
194                if (ctr.Parent == null)
195ExpandedSubBlockStart.gif                {
196                    ctr.Parent = this;
197                
198                    ctr.Height = e.Bound.Height;
199                   if( ctr.Width >this.Width)
200                     ctr.Width = this.Width - 5;
201                    toolTip1.SetToolTip(ctr, itemList[index].ItemDescribe);
202                }

203                ctr.Location = e.Bound.Location;
204
205            }

206        }

207
208        public bool List_Validating()
209ExpandedSubBlockStart.gif        {  bool isValidated = true;
210            foreach (Item item in itemList)
211ExpandedSubBlockStart.gif            {
212                isValidated = isValidated && ListItemControls_Validating(item.ItemControl, new CancelEventArgs());
213            }

214            return isValidated;
215        }

216
217        public bool ListItemControls_Validating(object sender, CancelEventArgs e)
218ExpandedSubBlockStart.gif        {
219            Control ctr = sender as Control;
220            Item item = itemList[ctr];
221            
222            if (string.IsNullOrEmpty(item.Regex))
223                return true;
224            Regex regex = new Regex(item.Regex);
225
226            string text = ctr.Text.Trim();
227            if (!regex.IsMatch(text))
228ExpandedSubBlockStart.gif            {
229                err.SetIconAlignment(ctr, ErrorIconAlignment.MiddleRight);
230                err.SetIconPadding(ctr, 3);
231                err.SetError(ctr, item.ErrorMessage);
232                return false;
233            }

234            err.Clear();
235           
236            return true;
237        }

238        private void InitializeComponent()
239ExpandedSubBlockStart.gif        {
240
241            this.components = new System.ComponentModel.Container();
242            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
243            this.SuspendLayout();
244            this.ResumeLayout(false);
245
246        }

247
248        private IContainer components;
249    }

250
251    public class ListItemColloction : List<Item>
252ExpandedSubBlockStart.gif    {
253        public Item this[Control itemControl]
254ExpandedSubBlockStart.gif        {
255            get
256ExpandedSubBlockStart.gif            {
257                for (int i = 0; i < this.Count; i++)
258ExpandedSubBlockStart.gif                {
259                    if (this[i].ItemControl.Equals(itemControl))
260                        return this[i];
261                }

262                return null;
263            }

264
265            set
266ExpandedSubBlockStart.gif            {
267                for (int i = 0; i < this.Count; i++)
268ExpandedSubBlockStart.gif                {
269                    if (this[i].ItemControl.Equals(itemControl))
270                        this[i] = value;
271                }

272            }

273        }

274    }

275
276    [Serializable]
277    public class Item
278ExpandedSubBlockStart.gif    {
279        private Control itemControl = null;
280        private string itemDescible = string.Empty;
281        private string regex = string.Empty;
282        private string errorMessage = string.Empty;
283        public Item()
284ExpandedSubBlockStart.gif        {
285        }

286
287        public Item(Control itemControl, string itemDescible,string regex,string errorMessage)
288ExpandedSubBlockStart.gif        {
289            this.itemControl = itemControl;
290            this.itemDescible = itemDescible;
291            this.regex = regex;
292            this.errorMessage = errorMessage;
293        }

294        public Item(Control itemControl, string itemDescible)
295ExpandedSubBlockStart.gif        {
296            this.itemControl = itemControl;
297            this.itemDescible = itemDescible;
298           
299        }

300        public string Regex
301ExpandedSubBlockStart.gif        {
302            get
303ExpandedSubBlockStart.gif            {
304                return regex;
305            }

306            set
307ExpandedSubBlockStart.gif            {
308                regex = value;
309            }

310        }

311        public Control ItemControl
312ExpandedSubBlockStart.gif        {
313            get
314ExpandedSubBlockStart.gif            {
315                return itemControl;
316            }

317            set
318ExpandedSubBlockStart.gif            {
319                itemControl = value;
320            }

321        }

322
323        public string ItemDescribe
324ExpandedSubBlockStart.gif        {
325            get
326ExpandedSubBlockStart.gif            {
327                return itemDescible;
328            }

329
330            set
331ExpandedSubBlockStart.gif            {
332                itemDescible = value;
333            }

334        }

335
336        public string ErrorMessage
337ExpandedSubBlockStart.gif        {
338            get
339ExpandedSubBlockStart.gif            {
340                return errorMessage;
341            }

342            set
343ExpandedSubBlockStart.gif            {
344                errorMessage = value;
345            }

346        }

347
348    }

349    public class DrawItemEventArgsMySelf : EventArgs
350ExpandedSubBlockStart.gif    {
351        private Rectangle bound = Rectangle.Empty;
352
353        private int index;
354        private Graphics graphics;
355        public Graphics Graphics
356ExpandedSubBlockStart.gif        {
357            get
358ExpandedSubBlockStart.gif            {
359                return this.graphics;
360            }

361        }

362        public DrawItemEventArgsMySelf(Rectangle bound, int index, Graphics g)
363ExpandedSubBlockStart.gif        {
364            this.bound = bound;
365
366            this.index = index;
367            this.graphics = g;
368        }

369        public Rectangle Bound
370ExpandedSubBlockStart.gif        {
371            get
372ExpandedSubBlockStart.gif            {
373                return this.bound;
374            }

375        }

376
377
378        public int Index
379ExpandedSubBlockStart.gif        {
380            get
381ExpandedSubBlockStart.gif            {
382                return index;
383            }

384        }

385       
386    }

387}

388
复制代码


作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2009/10/12/1581928.html


相关文章
|
5月前
MFC基本控件4-列表框控件List Box
今天和大家分享一下列表框控件的几个使用方法, 有些基本操作比如实例化对象,继承类的创建相信大家都已经灰了, 不会的可以看第一二篇文章。
|
API Windows 容器
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
161 0
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(下)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
139 0
Qt listwigwt item 加入自定义元素
Qt listwigwt item 加入自定义元素
132 0
|
C#
【WPF】拖拽ListBox中的Item
原文:【WPF】拖拽ListBox中的Item 整理了两个关于WPF拖拽ListBox中的Item的功能。项目地址 https://github.com/Guxin233/WPF-DragItemInListBox 需求一: 两个ListBox,拖拽其中一个ListBox的Item,放置到另一个ListBox中。
1537 0