广州科技网站建设,网站制作公司dedecms,注册新公司网上核名,wordpress更换背景博客开通有一阵了#xff0c;就是没有时间写#xff0c;遗憾啊。#xff01; 这几天做了个排序的自定义控件#xff0c;在listbox item里放是button 并支持图标的交替变换 效果如下#xff1a; 把代码贴上#xff1a;1using System; 2using System.Collections.… 博客开通有一阵了就是没有时间写遗憾啊。 这几天做了个排序的自定义控件在listbox item里放是button 并支持图标的交替变换 效果如下 把代码贴上 1using System; 2using System.Collections.Generic; 3using System.Collections; 4using System.Text; 5using System.ComponentModel; 6using System.Windows.Forms; 7using System.Drawing; 8using System.Drawing.Text; 9using System.Data; namespace SQLAnalysis { public class ListBoxEx : ListBox { public ListBoxEx() { this.DrawMode DrawMode.OwnerDrawFixed; btnList new ListButton(); } public override DrawMode DrawMode { get { return DrawMode.OwnerDrawFixed; } set { base.DrawMode DrawMode.OwnerDrawFixed; } } protected override void OnResize(EventArgs e) { base.OnResize(e); this.Refresh(); } public Button FindItemButton(int index) { // return this.Controls.Find(this.Name $ItemComboBox index.ToString(), true)[0]; if (index btnList.Count) return null; return btnList[index]; } /// summary /// 提供删除item项 /// /summary /// param nameindex/param public void RemoveItem(int index) { this.Items.RemoveAt(index); btnList[index].Parent null; btnList[index] null; //btnList[index].Dispose(); btnList.RemoveAt(index); this.Refresh(); } /// summary /// 提供移动item项 /// /summary /// param nameoffset/param public void MoveItem(int offset) { int index this.SelectedIndex offset; if (index -1 index this.Items.Count) { int oldSelectedIndex this.SelectedIndex; Object item this.SelectedItem; this.Items.RemoveAt(oldSelectedIndex); this.Items.Insert(index, item); Button btn btnList[oldSelectedIndex]; btnList.RemoveAt(oldSelectedIndex); btnList.Insert(index, btn); this.Refresh(); } else { MessageBox.Show(不是可用的移动矢量); } } private ToggleItem GetNexttoggleItem(ToggleItem tog) { int index -1; for (int i 0; i ToggleItemList.Count; i) { ToggleItem item (ToggleItem)ToggleItemList[i]; if (item.Tag tog.Tag) { iindex i; break; } } return ((ToggleItem)ToggleItemList[(index 1) % ToggleItemList.Count]); } private ListToggleItem toggleItemList ;//ListToggleItem toggleItemList new ListToggleItem(); [Bindable(false), Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public ListToggleItem ToggleItemList { get { if (toggleItemList null) toggleItemList new ListToggleItem(); return toggleItemList; } set { if (toggleItemList null) toggleItemList new ListToggleItem(); toggleItemList value; } } //主要重写事件 protected override void OnDrawItem(DrawItemEventArgs e) { this.DrawMode DrawMode.OwnerDrawFixed; this.ItemHeight 22; e.DrawBackground(); e.DrawFocusRectangle(); Brush myBrush Brushes.Black; if (e.Index -1 e.Index this.Items.Count) { string drawString (e.Index 1) this.Items[e.Index].ToString(); e.Graphics.DrawString(drawString, e.Font, new SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault); AddButtonToItem(e); } base.OnDrawItem(e); } private ListButton btnList null; protected void AddButtonToItem(DrawItemEventArgs e) { if (btnList.Count e.Index) { btnList.Add(new Button()); Button btn btnList[e.Index]; btn.BackColor System.Drawing.Color.LightSkyBlue ; btn.Name this.Name $ItemButton e.Index.ToString(); btn.Width 20; btn.Height 20; btn.ImageAlign ContentAlignment.MiddleCenter; btn.TextImageRelation TextImageRelation.ImageAboveText; if (toggleItemList.Count 0) { btn.BackgroundImage ((ToggleItem)toggleItemList[0]).BGImage; //btn.Width toggleItemList[0].BGImage.Width; //btn.Height toggleItemList[0].BGImage.Height; btn.Tag ((ToggleItem)toggleItemList[0]).Tag; } btn.Parent this; btn.Click new EventHandler(ItemButtonClickHandler); } Button bt btnList[e.Index]; bt.Left this.Width - bt.Width - 20; bt.Top e.Bounds.Top; } /// summary /// item button双击事件 /// /summary /// param namesender/param /// param namee/param protected void ItemButtonClickHandler(object sender, EventArgs e) { int index -1; if (sender is Button) index this.IndexFromPoint(((Button)sender).Location); if (index -1) return; Button btn sender as Button; ToggleItem togold new ToggleItem((string)btn.Tag, btn.BackgroundImage); ToggleItem tognew this.GetNexttoggleItem(togold); //btn.Width tognew.BGImage.Width; //btn.Height tognew.BGImage.Height; btn.BackgroundImage tognew.BGImage; btn.Tag tognew.Tag; ItemEventHandler handler (ItemEventHandler)Events[ItemLabelClickObj]; if (handler ! null) { handler(sender, new ItemButtonClickEventArgs(index)); ; } } } [Serializable] public class ToggleItem { private string tag; private Image bgImage; public ToggleItem() { } public ToggleItem(string tag, Image bg) { this.tag tag; this.bgImage bg; } public string Tag { get { return tag; } set { tag value; } } public Image BGImage { get { return bgImage; } set { bgImage value; } } } } 本文转自 破狼 51CTO博客原文链接http://blog.51cto.com/whitewolfblog/832641如需转载请自行联系原作者