在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# listview 扩展 示例源码

C# listview 扩展 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.03M
  • 下载次数:124
  • 浏览次数:3535
  • 发布时间:2013-10-02
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C#

实例介绍

【实例简介】

【实例截图】


【核心代码】


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Runtime.InteropServices;

namespace EXControls {
    
    public class EXListView : ListView {
        
        private ListViewItem.ListViewSubItem _clickedsubitem; //clicked ListViewSubItem
        private ListViewItem _clickeditem; //clicked ListViewItem
        private int _col; //index of doubleclicked ListViewSubItem
        private TextBox txtbx; //the default edit control
        private int _sortcol; //index of clicked ColumnHeader
        private Brush _sortcolbrush; //color of items in sorted column
        private Brush _highlightbrush; //color of highlighted items
        private int _cpadding; //padding of the embedded controls
            
        private const UInt32 LVM_FIRST = 0x1000;
        private const UInt32 LVM_SCROLL = (LVM_FIRST   20);
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;
        private const int WM_MOUSEWHEEL = 0x020A;
        private const int WM_PAINT = 0x000F;
            
        private struct EmbeddedControl {
            public Control MyControl;
            public EXControlListViewSubItem MySubItem;
        }
            
        private ArrayList _controls;
            
        [DllImport("user32.dll")]
        private static extern bool SendMessage(IntPtr hWnd, UInt32 m, int wParam, int lParam);
        
        protected override void WndProc(ref Message m) {
            if (m.Msg == WM_PAINT) {
                foreach (EmbeddedControl c in _controls) {
                    Rectangle r = c.MySubItem.Bounds;
                    if (r.Y > 0 && r.Y < this.ClientRectangle.Height) {
                        c.MyControl.Visible = true;
                        c.MyControl.Bounds = new Rectangle(r.X   _cpadding, r.Y   _cpadding, r.Width - (2 * _cpadding), r.Height - (2 * _cpadding));
                    } else {
                        c.MyControl.Visible = false;
                    }
                }
            }
            switch (m.Msg) {
                case WM_HSCROLL:
                case WM_VSCROLL:
                case WM_MOUSEWHEEL:
                    this.Focus();
                    break;
            }
            base.WndProc(ref m);
        }
        
        private void ScrollMe(int x, int y) {
            SendMessage((IntPtr) this.Handle, LVM_SCROLL, x, y);
        }
        
        public EXListView() {
            _cpadding = 4;
            _controls = new ArrayList();
            _sortcol = -1;
            _sortcolbrush = SystemBrushes.ControlLight;
            _highlightbrush = SystemBrushes.Highlight;
            this.OwnerDraw = true;
            this.FullRowSelect = true;
            this.View = View.Details;
            this.MouseDown  = new MouseEventHandler(this_MouseDown);
            this.MouseDoubleClick  = new MouseEventHandler(this_MouseDoubleClick);
            this.DrawColumnHeader  = new DrawListViewColumnHeaderEventHandler(this_DrawColumnHeader);
            this.DrawSubItem  = new DrawListViewSubItemEventHandler(this_DrawSubItem);
            this.MouseMove  = new MouseEventHandler(this_MouseMove);
            this.ColumnClick  = new ColumnClickEventHandler(this_ColumnClick);
            txtbx = new TextBox();
            txtbx.Visible = false;
            this.Controls.Add(txtbx);
            txtbx.Leave  = new EventHandler(c_Leave);
            txtbx.KeyPress  = new KeyPressEventHandler(txtbx_KeyPress);
        }
        
        public void AddControlToSubItem(Control control, EXControlListViewSubItem subitem) {
            this.Controls.Add(control);
            subitem.MyControl = control;
            EmbeddedControl ec;
            ec.MyControl = control;
            ec.MySubItem = subitem;
            this._controls.Add(ec);
        }
        
        public void RemoveControlFromSubItem(EXControlListViewSubItem subitem) {
            Control c = subitem.MyControl;
            for (int i = 0; i < this._controls.Count; i  ) {
                if (((EmbeddedControl) this._controls[i]).MySubItem == subitem) {
                    this._controls.RemoveAt(i);
                    subitem.MyControl = null;
                    this.Controls.Remove(c);
                    c.Dispose();
                    return;
                }
            }
        }
        
        public int ControlPadding {
            get {return _cpadding;}
            set {_cpadding = value;}
        }
        
        public Brush MySortBrush {
            get {return _sortcolbrush;}
            set {_sortcolbrush = value;}
        }
        
        public Brush MyHighlightBrush {
            get {return _highlightbrush;}
            set {_highlightbrush = value;}
        }
        
        private void txtbx_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char) Keys.Return) {
                _clickedsubitem.Text = txtbx.Text;
                txtbx.Visible = false;
                _clickeditem.Tag = null;
            }
        }
        
        private void c_Leave(object sender, EventArgs e) {
            Control c = (Control) sender;
            _clickedsubitem.Text = c.Text;
            c.Visible = false;
            _clickeditem.Tag = null;
        }
        
        private void this_MouseDown(object sender, MouseEventArgs e) {
            ListViewHitTestInfo lstvinfo = this.HitTest(e.X, e.Y);
            ListViewItem.ListViewSubItem subitem = lstvinfo.SubItem;
            if (subitem == null) return;
            int subx = subitem.Bounds.Left;
            if (subx < 0) {
                this.ScrollMe(subx, 0);
            }
        }
        
        private void this_MouseDoubleClick(object sender, MouseEventArgs e) {
            EXListViewItem lstvItem = this.GetItemAt(e.X, e.Y) as EXListViewItem;
            if (lstvItem == null) return;
            _clickeditem = lstvItem;
            int x = lstvItem.Bounds.Left;
            int i;
            for (i = 0; i < this.Columns.Count; i  ) {
                x = x   this.Columns[i].Width;
                if (x > e.X) {
                    x = x - this.Columns[i].Width;
                    _clickedsubitem = lstvItem.SubItems[i];
                    _col = i;
                    break;
                }
            }
            if (!(this.Columns[i] is EXColumnHeader)) return;
            EXColumnHeader col = (EXColumnHeader) this.Columns[i];
            if (col.GetType() == typeof(EXEditableColumnHeader)) {
                EXEditableColumnHeader editcol = (EXEditableColumnHeader) col;
                if (editcol.MyControl != null) {
                    Control c = editcol.MyControl;
                    if (c.Tag != null) {
                        this.Controls.Add(c);
                        c.Tag = null;
                        if (c is ComboBox) {
                            ((ComboBox) c).SelectedValueChanged  = new EventHandler(cmbx_SelectedValueChanged);
                        }
                        c.Leave  = new EventHandler(c_Leave);
                    }
                    c.Location = new Point(x, this.GetItemRect(this.Items.IndexOf(lstvItem)).Y);
                    c.Width = this.Columns[i].Width;
                    if (c.Width > this.Width) c.Width = this.ClientRectangle.Width;
                    c.Text = _clickedsubitem.Text;
                    c.Visible = true;
                    c.BringToFront();
                    c.Focus();
                } else {
                    txtbx.Location = new Point(x, this.GetItemRect(this.Items.IndexOf(lstvItem)).Y);
                    txtbx.Width = this.Columns[i].Width;
                    if (txtbx.Width > this.Width) txtbx.Width = this.ClientRectangle.Width;
                    txtbx.Text = _clickedsubitem.Text;
                    txtbx.Visible = true;
                    txtbx.BringToFront();
                    txtbx.Focus();
                }
            } else if (col.GetType() == typeof(EXBoolColumnHeader)) {
                EXBoolColumnHeader boolcol = (EXBoolColumnHeader) col;
                if (boolcol.Editable) {
                    EXBoolListViewSubItem boolsubitem = (EXBoolListViewSubItem) _clickedsubitem;
                    if (boolsubitem.BoolValue == true) {
                        boolsubitem.BoolValue = false;
                    } else {
                        boolsubitem.BoolValue = true;
                    }
                    this.Invalidate(boolsubitem.Bounds);
                }
            }
        }
        
        private void cmbx_SelectedValueChanged(object sender, EventArgs e) {
            if (((Control) sender).Visible == false || _clickedsubitem == null) return;
            if (sender.GetType() == typeof(EXComboBox)) {
                EXComboBox excmbx = (EXComboBox) sender;
                object item = excmbx.SelectedItem;
                //Is this an combobox item with one image?
                if (item.GetType() == typeof(EXComboBox.EXImageItem)) {
                    EXComboBox.EXImageItem imgitem = (EXComboBox.EXImageItem) item;
                    //Is the first column clicked -- in that case it's a ListViewItem
                    if (_col == 0) {
                        if (_clickeditem.GetType() == typeof(EXImageListViewItem)) {
                            ((EXImageListViewItem) _clickeditem).MyImage = imgitem.MyImage;
                        } else if (_clickeditem.GetType() == typeof(EXMultipleImagesListViewItem)) {
                            EXMultipleImagesListViewItem imglstvitem = (EXMultipleImagesListViewItem) _clickeditem;
			                imglstvitem.MyImages.Clear();
			                imglstvitem.MyImages.AddRange(new object[] {imgitem.MyImage});
                        }
                    //another column than the first one is clicked, so we have a ListViewSubItem
                    } else {
                        if (_clickedsubitem.GetType() == typeof(EXImageListViewSubItem)) {
                            EXImageListViewSubItem imgsub = (EXImageListViewSubItem) _clickedsubitem;
                            imgsub.MyImage = imgitem.MyImage;
                        } else if (_clickedsubitem.GetType() == typeof(EXMultipleImagesListViewSubItem)) {
                            EXMultipleImagesListViewSubItem imgsub = (EXMultipleImagesListViewSubItem) _clickedsubitem;
                            imgsub.MyImages.Clear();
                            imgsub.MyImages.Add(imgitem.MyImage);
			                imgsub.MyValue = imgitem.MyValue;
                        }
                    }
                    //or is this a combobox item with multiple images?
                } else if (item.GetType() == typeof(EXComboBox.EXMultipleImagesItem)) {
                    EXComboBox.EXMultipleImagesItem imgitem = (EXComboBox.EXMultipleImagesItem) item;
                    if (_col == 0) {
                        if (_clickeditem.GetType() == typeof(EXImageListViewItem)) {
                            ((EXImageListViewItem) _clickeditem).MyImage = (Image) imgitem.MyImages[0];
                        } else if (_clickeditem.GetType() == typeof(EXMultipleImagesListViewItem)) {
                            EXMultipleImagesListViewItem imglstvitem = (EXMultipleImagesListViewItem) _clickeditem;
			                imglstvitem.MyImages.Clear();
			                imglstvitem.MyImages.AddRange(imgitem.MyImages);
                        }
                    } else {
                        if (_clickedsubitem.GetType() == typeof(EXImageListViewSubItem)) {
                            EXImageListViewSubItem imgsub = (EXImageListViewSubItem) _clickedsubitem;
                            if (imgitem.MyImages != null) {
                                imgsub.MyImage = (Image) imgitem.MyImages[0];
                            }
                        } else if (_clickedsubitem.GetType() == typeof(EXMultipleImagesListViewSubItem)) {
                            EXMultipleImagesListViewSubItem imgsub = (EXMultipleImagesListViewSubItem) _clickedsubitem;
                            imgsub.MyImages.Clear();
			                imgsub.MyImages.AddRange(imgitem.MyImages);
			                imgsub.MyValue = imgitem.MyValue;
                        }
                    }
                }
            }
            ComboBox c = (ComboBox) sender;
            _clickedsubitem.Text = c.Text;
            c.Visible = false;
            _clickeditem.Tag = null;
        }
        
        private void this_MouseMove(object sender, MouseEventArgs e) {
            ListViewItem item = this.GetItemAt(e.X, e.Y);
            if (item != null && item.Tag == null) {
                this.Invalidate(item.Bounds);
                item.Tag = "t";
            }
        }
        
        private void this_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
            e.DrawDefault = true;
        }
        
        private void this_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) {
            e.DrawBackground();
            if (e.ColumnIndex == _sortcol) {
                e.Graphics.FillRectangle(_sortcolbrush, e.Bounds);
            }
            if ((e.ItemState & ListViewItemStates.Selected) != 0) {
                e.Graphics.FillRectangle(_highlightbrush, e.Bounds);
            }
            int fonty = e.Bounds.Y   ((int) (e.Bounds.Height / 2)) - ((int) (e.SubItem.Font.Height / 2));
            int x = e.Bounds.X   2;
            if (e.ColumnIndex == 0) {
                EXListViewItem item = (EXListViewItem) e.Item;
                if (item.GetType() == typeof(EXImageListViewItem)) {
                    EXImageListViewItem imageitem = (EXImageListViewItem) item;
                    if (imageitem.MyImage != null) {
                        Image img = imageitem.MyImage;
                        int imgy = e.Bounds.Y   ((int) (e.Bounds.Height / 2)) - ((int) (img.Height / 2));
                        e.Graphics.DrawImage(img, x, imgy, img.Width, img.Height);
                        x  = img.Width   2;
                    }
                }
                e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(e.SubItem.ForeColor), x, fonty);
                return;
            }
            EXListViewSubItemAB subitem = e.SubItem as EXListViewSubItemAB;
            if (subitem == null) {
                e.DrawDefault = true;
            } else {
                x = subitem.DoDraw(e, x, this.Columns[e.ColumnIndex] as EXColumnHeader);                
                e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(e.SubItem.ForeColor), x, fonty);
            }
        }
        
        private void this_ColumnClick(object sender, ColumnClickEventArgs e) {
            if (this.Items.Count == 0) return;
            for (int i = 0; i < this.Columns.Count; i  ) {
                this.Columns[i].ImageKey = null;
            }
            for (int i = 0; i < this.Items.Count; i  ) {
                this.Items[i].Tag = null;
            }
            if (e.Column != _sortcol) {
                _sortcol = e.Column;
                this.Sorting = SortOrder.Ascending;
                this.Columns[e.Column].ImageKey = "up";
            } else {
                if (this.Sorting == SortOrder.Ascending) {
                    this.Sorting = SortOrder.Descending;
                    this.Columns[e.Column].ImageKey = "down";
                } else {
                    this.Sorting = SortOrder.Ascending;
                    this.Columns[e.Column].ImageKey = "up";
                }
            }
            if (_sortcol == 0) {
                //ListViewItem
                if (this.Items[0].GetType() == typeof(EXListViewItem)) {
                    //sorting on text
                    this.ListViewItemSorter = new ListViewItemComparerText(e.Column, this.Sorting);
                } else {
                    //sorting on value
                    this.ListViewItemSorter = new ListViewItemComparerValue(e.Column, this.Sorting);
                }
            } else {
                //ListViewSubItem
                if (this.Items[0].SubItems[_sortcol].GetType() == typeof(EXListViewSubItemAB)) {
                    //sorting on text
                    this.ListViewItemSorter = new ListViewSubItemComparerText(e.Column, this.Sorting);
                } else {
                    //sorting on value
                    this.ListViewItemSorter = new ListViewSubItemComparerValue(e.Column, this.Sorting);
                }
            }
        }
        
        class ListViewSubItemComparerText : System.Collections.IComparer {
            
            private int _col;
            private SortOrder _order;

            public ListViewSubItemComparerText() {
                _col = 0;
                _order = SortOrder.Ascending;
            }

            public ListViewSubItemComparerText(int col, SortOrder order) {
                _col = col;
                _order = order;
            }
            
            public int Compare(object x, object y) {
                int returnVal = -1;
                
                string xstr = ((ListViewItem) x).SubItems[_col].Text;
                string ystr = ((ListViewItem) y).SubItems[_col].Text;
                
                decimal dec_x;
                decimal dec_y;
                DateTime dat_x;
                DateTime dat_y;
                
                if (Decimal.TryParse(xstr, out dec_x) && Decimal.TryParse(ystr, out dec_y)) {
                    returnVal = Decimal.Compare(dec_x, dec_y);
                } else if (DateTime.TryParse(xstr, out dat_x) && DateTime.TryParse(ystr, out dat_y)) {
                    returnVal = DateTime.Compare(dat_x, dat_y);
                } else {
                    returnVal = String.Compare(xstr, ystr);
                }
                if (_order == SortOrder.Descending) returnVal *= -1;
                return returnVal;
            }
        
        }
	
	    class ListViewSubItemComparerValue : System.Collections.IComparer {
            
            private int _col;
            private SortOrder _order;

            public ListViewSubItemComparerValue() {
                _col = 0;
                _order = SortOrder.Ascending;
            }

            public ListViewSubItemComparerValue(int col, SortOrder order) {
                _col = col;
                _order = order;
            }
            
            public int Compare(object x, object y) {
                int returnVal = -1;
                
                string xstr = ((EXListViewSubItemAB) ((ListViewItem) x).SubItems[_col]).MyValue;
                string ystr = ((EXListViewSubItemAB) ((ListViewItem) y).SubItems[_col]).MyValue;
                
                decimal dec_x;
                decimal dec_y;
                DateTime dat_x;
                DateTime dat_y;
                
                if (Decimal.TryParse(xstr, out dec_x) && Decimal.TryParse(ystr, out dec_y)) {
                    returnVal = Decimal.Compare(dec_x, dec_y);
                } else if (DateTime.TryParse(xstr, out dat_x) && DateTime.TryParse(ystr, out dat_y)) {
                    returnVal = DateTime.Compare(dat_x, dat_y);
                } else {
                    returnVal = String.Compare(xstr, ystr);
                }
                if (_order == SortOrder.Descending) returnVal *= -1;
                return returnVal;
            }
        
        }
	
	    class ListViewItemComparerText : System.Collections.IComparer {
            
            private int _col;
            private SortOrder _order;

            public ListViewItemComparerText() {
                _col = 0;
                _order = SortOrder.Ascending;
            }

            public ListViewItemComparerText(int col, SortOrder order) {
                _col = col;
                _order = order;
            }
            
            public int Compare(object x, object y) {
                int returnVal = -1;
                
                string xstr = ((ListViewItem) x).Text;
                string ystr = ((ListViewItem) y).Text;
                
                decimal dec_x;
                decimal dec_y;
                DateTime dat_x;
                DateTime dat_y;
                
                if (Decimal.TryParse(xstr, out dec_x) && Decimal.TryParse(ystr, out dec_y)) {
                    returnVal = Decimal.Compare(dec_x, dec_y);
                } else if (DateTime.TryParse(xstr, out dat_x) && DateTime.TryParse(ystr, out dat_y)) {
                    returnVal = DateTime.Compare(dat_x, dat_y);
                } else {
                    returnVal = String.Compare(xstr, ystr);
                }
                if (_order == SortOrder.Descending) returnVal *= -1;
                return returnVal;
            }
        
        }
	
	    class ListViewItemComparerValue : System.Collections.IComparer {
            
            private int _col;
            private SortOrder _order;

            public ListViewItemComparerValue() {
                _col = 0;
                _order = SortOrder.Ascending;
            }

            public ListViewItemComparerValue(int col, SortOrder order) {
                _col = col;
                _order = order;
            }
            
            public int Compare(object x, object y) {
                int returnVal = -1;
                
                string xstr = ((EXListViewItem) x).MyValue;
                string ystr = ((EXListViewItem) y).MyValue;
                
                decimal dec_x;
                decimal dec_y;
                DateTime dat_x;
                DateTime dat_y;
                
                if (Decimal.TryParse(xstr, out dec_x) && Decimal.TryParse(ystr, out dec_y)) {
                    returnVal = Decimal.Compare(dec_x, dec_y);
                } else if (DateTime.TryParse(xstr, out dat_x) && DateTime.TryParse(ystr, out dat_y)) {
                    returnVal = DateTime.Compare(dat_x, dat_y);
                } else {
                    returnVal = String.Compare(xstr, ystr);
                }
                if (_order == SortOrder.Descending) returnVal *= -1;
                return returnVal;
            }
        
        }
        
    }
    
    public class EXColumnHeader : ColumnHeader {
        
        public EXColumnHeader() {
            
        }
        
        public EXColumnHeader(string text) {
            this.Text = text;
        }
        
        public EXColumnHeader(string text, int width) {
            this.Text = text;
            this.Width = width;
        }
        
    }
    
    public class EXEditableColumnHeader : EXColumnHeader {
        
        private Control _control;
        
        public EXEditableColumnHeader() {
            
        }
        
        public EXEditableColumnHeader(string text) {
            this.Text = text;
        }
        
        public EXEditableColumnHeader(string text, int width) {
            this.Text = text;
            this.Width = width;
        }
        
        public EXEditableColumnHeader(string text, Control control) {
            this.Text = text;
            this.MyControl = control;
        }
        
        public EXEditableColumnHeader(string text, Control control, int width) {
            this.Text = text;
            this.MyControl = control;
            this.Width = width;
        }
        
        public Control MyControl {
            get {return _control;}
            set {
                _control = value;
                _control.Visible = false;
                _control.Tag = "not_init";
            }
        }
        
    }
    
    public class EXBoolColumnHeader : EXColumnHeader {
        
        private Image _trueimage;
        private Image _falseimage;
        private bool _editable;
            
        public EXBoolColumnHeader() {
            init();
        }
        
        public EXBoolColumnHeader(string text) {
            init();
            this.Text = text;
        }
        
        public EXBoolColumnHeader(string text, int width) {
            init();
            this.Text = text;
            this.Width = width;
        }
        
        public EXBoolColumnHeader(string text, Image trueimage, Image falseimage) {
            init();
            this.Text = text;
            _trueimage = trueimage;
            _falseimage = falseimage;
        }
        
        public EXBoolColumnHeader(string text, Image trueimage, Image falseimage, int width) {
            init();
            this.Text = text;
            _trueimage = trueimage;
            _falseimage = falseimage;
            this.Width = width;
        }
        
        private void init() {
            _editable = false;
        }
        
        public Image TrueImage {
            get {return _trueimage;}
            set {_trueimage = value;}
        }
        
        public Image FalseImage {
            get {return _falseimage;}
            set {_falseimage = value;}
        }
        
        public bool Editable {
            get {return _editable;}
            set {_editable = value;}
        }
        
    }
    
    public abstract class EXListViewSubItemAB : ListViewItem.ListViewSubItem {
        
        private string _value = "";
        
        public EXListViewSubItemAB() {
            
        }
        
        public EXListViewSubItemAB(string text) {
            this.Text = text;
        }
        
        public string MyValue {
            get {return _value;}
            set {_value = value;}
        }
        
        //return the new x coordinate
        public abstract int DoDraw(DrawListViewSubItemEventArgs e, int x, EXControls.EXColumnHeader ch);

    }
    
    public class EXListViewSubItem : EXListViewSubItemAB {
        
        public EXListViewSubItem() {
            
        }
        
        public EXListViewSubItem(string text) {
            this.Text = text;
        }
        
        public override int DoDraw(DrawListViewSubItemEventArgs e, int x, EXControls.EXColumnHeader ch) {
            return x;
        }

    }
    
    public class EXControlListViewSubItem : EXListViewSubItemAB {
        
        private Control _control;
            
        public EXControlListViewSubItem() {
            
        }
        
        public Control MyControl {
            get {return _control;}
            set {_control = value;}
        }
        
        public override int DoDraw(DrawListViewSubItemEventArgs e, int x, EXColumnHeader ch) {
            return x;
        }
        
    }
    
    public class EXImageListViewSubItem : EXListViewSubItemAB {
        
        private Image _image;
            
        public EXImageListViewSubItem() {
            
        }
        
        public EXImageListViewSubItem(string text) {
            this.Text = text;
        }
            
        public EXImageListViewSubItem(Image image) {
            _image = image;
        }
	
        public EXImageListViewSubItem(Image image, string value) {
            _image = image;
            this.MyValue = value;
        }
	
        public EXImageListViewSubItem(string text, Image image, string value) {
            this.Text = text;
            _image = image;
            this.MyValue = value;
        }
        
        public Image MyImage {
            get {return _image;}
            set {_image = value;}
        }
        
        public override int DoDraw(DrawListViewSubItemEventArgs e, int x, EXControls.EXColumnHeader ch) {
            if (this.MyImage != null) {
                Image img = this.MyImage;
                int imgy = e.Bounds.Y   ((int) (e.Bounds.Height / 2)) - ((int) (img.Height / 2));
                e.Graphics.DrawImage(img, x, imgy, img.Width, img.Height);
                x  = img.Width   2;
            }
            return x;
        }
        
    }
    
    public class EXMultipleImagesListViewSubItem : EXListViewSubItemAB {
        
        private ArrayList _images;
            
        public EXMultipleImagesListViewSubItem() {
            
        }
        
        public EXMultipleImagesListViewSubItem(string text) {
            this.Text = text;
        }
            
        public EXMultipleImagesListViewSubItem(ArrayList images) {
            _images = images;
        }
	
        public EXMultipleImagesListViewSubItem(ArrayList images, string value) {
            _images = images;
            this.MyValue = value;
        }
        
        public EXMultipleImagesListViewSubItem(string text, ArrayList images, string value) {
            this.Text = text;
            _images = images;
            this.MyValue = value;
        }
        
        public ArrayList MyImages {
            get {return _images;}
            set {_images = value;}
        }
        
        public override int DoDraw(DrawListViewSubItemEventArgs e, int x, EXColumnHeader ch) {    
            if (this.MyImages != null && this.MyImages.Count > 0) {
                for (int i = 0; i < this.MyImages.Count; i  ) {
                    Image img = (Image) this.MyImages[i];
                    int imgy = e.Bounds.Y   ((int) (e.Bounds.Height / 2)) - ((int) (img.Height / 2));
                    e.Graphics.DrawImage(img, x, imgy, img.Width, img.Height);
                    x  = img.Width   2;
                }
            }
            return x;
        }
        
    }
    
    public class EXBoolListViewSubItem : EXListViewSubItemAB {
        
        private bool _value;
            
        public EXBoolListViewSubItem() {
            
        }
        
        public EXBoolListViewSubItem(bool val) {
            _value = val;
	        this.MyValue = val.ToString();
        }
        
        public bool BoolValue {
            get {return _value;}
            set {
		        _value = value;
		        this.MyValue = value.ToString();
	        }
        }
        
        public override int DoDraw(DrawListViewSubItemEventArgs e, int x, EXColumnHeader ch) {    
            EXBoolColumnHeader boolcol = (EXBoolColumnHeader) ch;
            Image boolimg;
            if (this.BoolValue == true) {
                boolimg = boolcol.TrueImage;
            } else {
                boolimg = boolcol.FalseImage;
            }
            int imgy = e.Bounds.Y   ((int) (e.Bounds.Height / 2)) - ((int) (boolimg.Height / 2));
            e.Graphics.DrawImage(boolimg, x, imgy, boolimg.Width, boolimg.Height);
            x  = boolimg.Width   2;
            return x;
        }
        
    }
    
    public class EXListViewItem : ListViewItem {
	
	    private string _value;
        
        public EXListViewItem() {
            
        }
        
        public EXListViewItem(string text) {
            this.Text = text;
        }
	
        public string MyValue {
            get {return _value;}
            set {_value = value;}
        }
        
    }
    
    public class EXImageListViewItem : EXListViewItem {
        
        private Image _image;
            
        public EXImageListViewItem() {
            
        }
        
        public EXImageListViewItem(string text) {
            this.Text = text;
        }
        
        public EXImageListViewItem(Image image) {
            _image = image;
        }
	
        public EXImageListViewItem(string text, Image image) {
            _image = image;
            this.Text = text;
        }
	
	    public EXImageListViewItem(string text, Image image, string value) {
            this.Text = text;
            _image = image;
	        this.MyValue = value;
        }
        
        public Image MyImage {
            get {return _image;}
            set {_image = value;}
        }
        
    }
    
    public class EXMultipleImagesListViewItem : EXListViewItem {
        
        private ArrayList _images;
            
        public EXMultipleImagesListViewItem() {
            
        }
        
        public EXMultipleImagesListViewItem(string text) {
            this.Text = text;
        }
            
        public EXMultipleImagesListViewItem(ArrayList images) {
            _images = images;
        }
	
	    public EXMultipleImagesListViewItem(string text, ArrayList images) {
            this.Text = text;
            _images = images;
        }
	
	    public EXMultipleImagesListViewItem(string text, ArrayList images, string value) {
            this.Text = text;
            _images = images;
	        this.MyValue = value;
        }
        
        public ArrayList MyImages {
            get {return _images;}
            set {_images = value;}
        }
        
    }    

}

标签: C#

实例下载地址

C# listview 扩展 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 luyongmao 发表于: 2025-03-08 15:42 01
没有源代码

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警