现在的位置: 首页 > 综合 > 正文

可编辑的ListView控件

2012年11月06日 ⁄ 综合 ⁄ 共 3785字 ⁄ 字号 评论关闭

      ListView控件是一个在Windows应用程序中使用频率比较高的一个,通常使用它来显示数据报表。很多情况下,我们不仅仅使用ListView来显示数据,还要求编辑其中的数据。但是.NET提供的ListView控件的编辑功能十分有限,只能编辑首列,编辑格式只能为文本框,等等,使用起来甚为不便。因此本人新写了一个类,扩充了其功能,客户程序员可以设置任何列的格式(只读,编辑状态下文本框,编辑状态下组合框),界面如图:
源代码在此下载:/Files/hcfalan/EditableListView.rar
摘录部分源代码如下:
  1/// <summary>
  2/// 列描述
  3/// </summary>

  4public class ALAN_ColumnHeader : ColumnHeader
  5{
  6    private ALAN_ListViewColumnStyle cs; //本列的风格
  7
  8    public ALAN_ColumnHeader() : base()
  9    {
 10        cs = ALAN_ListViewColumnStyle.ReadOnly;
 11    }

 12
 13    public ALAN_ColumnHeader(ALAN_ListViewColumnStyle _cs)
 14    {
 15        cs = _cs;
 16    }

 17
 18    public ALAN_ListViewColumnStyle ColumnStyle
 19    {
 20        get return cs; }
 21        set { cs = value;}
 22    }

 23}
;
 24
 25/// <summary>
 26/// 可编辑的ListView控件
 27/// </summary>

 28public class ALAN_EditListView : ListView 
 29{
 30
 31    private ListViewItem m_currentLVItem;     
 32
 33    private int m_nX=0;
 34
 35    private int m_nY=0;
 36
 37    private string m_strSubItemText ;
 38
 39    private int m_nSubItemSelected = 0 ; 
 40
 41    private ComboBox[] m_arrComboBoxes = new ComboBox[20];
 42
 43    private System.Windows.Forms.TextBox  editBox;
 44
 45    private Font m_fontComboBox;
 46
 47    private Font m_fontEdit;
 48
 49    private Color m_bgcolorComboBox;
 50
 51    private Color m_bgcolorEdit;
 52
 53
 54
 55    public ALAN_EditListView()
 56    {
 57
 58        editBox = new System.Windows.Forms.TextBox();
 59
 60        this.ComboBoxFont = this.Font;
 61
 62        this.EditFont = this.Font;
 63
 64    
 65
 66        this.EditBgColor = Color.LightBlue;
 67
 68        this.m_bgcolorComboBox = Color.LightBlue;
 69
 70        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SMKMouseDown);
 71
 72        this.DoubleClick += new System.EventHandler(this.SMKDoubleClick);
 73
 74        this.GridLines = true ;
 75
 76    
 77
 78        editBox.Size  = new System.Drawing.Size(0,0);
 79
 80        editBox.Location = new System.Drawing.Point(0,0);
 81
 82        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.editBox});
 83
 84        editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
 85
 86        editBox.LostFocus += new System.EventHandler(this.FocusOver);
 87
 88        editBox.AutoSize = true;
 89
 90        editBox.Font = this.EditFont;
 91
 92        editBox.BackColor = this.EditBgColor;
 93
 94        editBox.BorderStyle = BorderStyle.FixedSingle;
 95
 96        editBox.Hide();
 97
 98        editBox.Text = "";
 99
100    }

101
102
103
104    public Font ComboBoxFont
105    {
106
107        get return this.m_fontComboBox; }
108
109        set this.m_fontComboBox = value;}
110
111    }

112
113
114
115    public Color ComboBoxBgColor
116    {
117
118        get return this.m_bgcolorComboBox; }
119
120        set 
121
122        {
123
124            this.m_bgcolorComboBox = value;
125
126            for(int i=0; i<this.m_arrComboBoxes.Length; i++)
127
128            {
129
130                if (m_arrComboBoxes[i] != null)
131
132                    m_arrComboBoxes[i].BackColor = this.m_bgcolorComboBox;
133
134            }

135
136        }
      
137
138    }

139
140
141
142    public Font EditFont
143    {
144
145        get return this.m_fontEdit; }
146
147        set
148
149        {
150
151            this.m_fontEdit = value;
152
153            this.editBox.Font = this.m_fontEdit;
154
155        }

156
157    }

158
159
160
161    public Color EditBgColor
162    {
163
164        get return this.m_bgcolorEdit; }
165
166        set
167
168        {
169
170            this.m_bgcolorEdit = value;
171
172            this.editBox.BackColor = this.m_bgcolorEdit;
173
174        }

175
176    }

177
178
179
180    public void SetColumn(int columnIndex, ALAN_ListViewColumnStyle cs)
181    {
182
183        if (columnIndex<0 || columnIndex>this.Columns.Count)

抱歉!评论已关闭.