HcFalan [original] Loaded from 9cbs http://dev.9cbs.net/develop/Article/21/21801.shtm
The ListView control is a relatively high frequency of frequencies in a Windows application, usually using it to display data reports. In many cases, we don't just use listview to display data, but also request editing of them. However, the editing function of the ListView control provided by .NET is very limited. You can only edit the first column. Editing the format can only be text boxes, etc., it is very inconvenient to use. Therefore, I have newly written a class, expand its function, and customer programmers can set up any column format (read-only, editing, text box, editing, combination box), code as follows:
1. Alan_listViewColumnStyle Column Style Enumeration
///
/// Column style enumeration
/// summary>
Public Enum Alan_listViewColumnStyle
{
Readonly, // Read only
The text box is displayed in editbox, // editing
Composite box shown in ComboBox // Edit state
}
2. Alan_ColumnHeader columns with custom styles
///
/// column description
/// summary>
Public class alan_columnheader: ColumnHeader
{
Private alan_listviewcolumnstyle cs; // This list
Public alan_columnheader (): base ()
{
Cs = alan_listviewcolumnstyle.readonly;
}
Public alan_columnhead (alan_listviewcolumnstyle _cs)
{
CS = _CS;
}
Public alan_listviewColumnStyle ColumnStyle
{
Get {return cs;}
SET {CS = Value;
}
}
3. Alan_EditListView list control
///
/// Editable ListView control
/// summary>
Public class alan_editlistview: ListView
{
Private ListViewItem M_Currentlvitem;
Private INT m_nx = 0;
Private INT m_NY = 0;
PRIVATE STRING M_STRSUBITEMTEXT;
Private int m_nsubitemselected = 0;
Private ComboBox [] M_ArrcomboBoxes = New ComboBox [20];
Private system.windows.Forms.TextBox Editbox;
PRIVATE FONT M_FONTCOMBOBOX;
Private font m_fontedit;
Private color m_bgcolorcombobox;
Private color m_bgcoloredit;
Public alan_editlistview ()
{
Editbox = new system.windows.Forms.TextBox ();
THIS.comBOBOXFONT = this.font;
THIS.EDITFONT = this.font;
THIS.EDITBGCOLOR = color.lightblue; this.m_bgcolorcomboBOX = color.lightblue;
This.MouseDown = new system.windows.Forms.MouseEventHandler (this.smkmousedown);
THIS.DOUBLECLICK = New System.EventHandler (this.smkdoubleClick);
THIS.GRIDLINES = True;
EditBox.size = new system.drawing.size (0,0);
Editbox.location = new system.drawing.point (0,0);
This.Controls.addrange (new system.windows.forms.control [] {this.editbox});
EditBox.KeyPress = new system.windows.Forms.KeyPressEventHandler (this.EDitover);
EditBox.lostfocus = New System.EventHandler (this.focusover);
Editbox.autosize = true;
Editbox.font = this.editfont;
Editbox.backcolor = this.EDitbgcolor;
Editbox.Borderstyle = BorderStyle.fixedsingle;
Editbox.hide ();
EditBox.Text = "";
}
Public Font ComboBoxFont
{
Get {return this.m_fontcomboBox;}
Set {this.m_fontcomboBox = value;
}
Public Color ComboBoxBgcolor
{
Get {return this.m_bgcolorcomboBOX;
set
{
THIS.M_BGCOLORCOMBOX = VALUE;
For (int i = 0; i { IF (m_arrcomboBoxes [i]! = null) M_ARRCOMBOXES [I] .BackColor = this.m_bgcolorcomboBOX; } } } Public Font EditFont { Get {return this.m_fontedit;} set { THIS.M_FONTEDIT = VALUE; This.EDitbox.font = this.m_fontedit; } } Public Color Editbgcolor { Get {return this.m_bgcoloredit; set { THIS.M_BGCOLOREDIT = VALUE; THIS.EDITBOX.BACKCOLOR = this.m_bgcoloredit; } } Public void setColumn (int ColumnIndex, Alan_ListViewColumnStyle CS) { IF (ColumnIndex <0 || ColumnIndex> this.columns.count) Throw New Exception ("Column Index Is Out of Ran"); ((Alan_columnheader) columns [columnindex]). ColumnStyle = cs;} Public void BoundlistTocolumn (int columnIndex, string [] items) { IF (ColumnIndex <0 || ColumnIndex> this.columns.count) Throw New Exception ("Column Index Is Out of Ran"); IF ((Alan_ColumnHeader) Column [ColumnIndex]). ColumnStyle! = Alan_ListViewColumnStyle.comBobox) Throw New Exception ("Column Should Be ComboBox Style); ComboBox newbox = new comboBox (); For (int i = 0; i Newbox.Items.Add (items [i]); Newbox.size = new system.drawing.size (0,0); Newbox.location = new system.drawing.point (0,0); This.Controls.addrange (new system.windows.forms.control [] {newbox}); Newbox.selectedIndexchanged = new system.eventhandler (this.cmbselected); Newbox.lostfocus = new system.EventHandler (this.cmbfocusover); NewBox.KeyPress = new system.windows.Forms.KeyPressEventHandler (this.cmbkeypress); Newbox.font = this.comboBoxFont; NEWBOX.BACKCOLOR = this.comboBoxBgcolor; Newbox.dropdownStyle = ComboBoxStyle.dropdownList; Newbox.hide (); THIS.M_ARRCOMBOXES [ColumnIndex] = Newbox; } Private void CMBKeyPress (Object Sender, System.Windows.Forms.KeypressEventArgs E) { ComboBox CMBbox = (ComboBox) Sender; IF (e.keychar == 13 || E.keychar == 27) // Cr or esc PRESS { CMBBOX.HIDE (); } } Private Void CMBSelected (Object Sender, System.EventArgs E) { ComboBox CMBbox = (ComboBox) Sender; INT SEL = CMBbox.selectedIndex; IF (SEL> = 0) { String itemsel = cmbox.items [sel] .tostring (); m_currentlvitem.subitems [m_nsubitemselected] .text = itemsel; } } Private Void CMBFocusover (Object Sender, System.EventArgs E) { ComboBox CMBbox = (ComboBox) Sender; CMBBOX.HIDE (); } Private vid Editover (Object Sender, System.Windows.Forms.KeyPressEventArgs E) { IF (e.keychar == 13) { M_currentlvitem.subitems [m_nsubitemselected] .Text = EditBox.Text; Editbox.hide (); } IF (E.Keychar == 27) Editbox.hide (); } Private Void Focusover (Object Sender, System.EventArgs E) { M_currentlvitem.subitems [m_nsubitemselected] .Text = EditBox.Text; Editbox.hide (); } Public Void SmkdoubleClick (Object Sender, System.EventArgs E) { // Check the Subitem Clicked. INT NSTART = m_nx; // Current Mouse Down X position INT spos = 0; Int epos = this.columns [0] .width; For (int i = 0; i { IF (NStart> Spos && Nstart { m_nsubitemselected = i; Break; } SPOS = EPOS; EPOS = this.columns [i] .width; } m_strsubitemtext = m_currentlvitem.subitems [m_nsubitemselected] .TEXT; Alan_columnheader column = (alan_columnhead) Columns [m_nsubitemselected]; IF (Column.columnStyle == Alan_listViewColumnStyle.comBobox) { ComboBox CMBbox = this.m_arrcomboBoxes [m_nsubitemselected]; IF (cmbbox == null) Throw New Exception ("The ComboxBox Control Bind To Current Column Is Null"; Rectangle R = New Rectangle (spos, m_currentlvitem.bounds.y, epos, m_currentlvitem.bounds.bottom); CMBBOX.SIZE = New System.drawing.size (epos - spos, m_currentlvitem.bounds.bottom-m_currentlvitem.bounds.top); CMBBOX.LOCATION = New System.drawing.point (spos, m_currentlvitem.bounds.y); CMBBOX.SHOW (); CMBBOX.TEXT = m_strsubitemtext; CMBBOX.SELECTALL (); cMbbox.focus (); } IF (Column.columnStyle == alan_listviewcolumnstyle.editbox) { Rectangle R = New Rectangle (spos, m_currentlvitem.bounds.y, epos, m_currentlvitem.bounds.bottom); Editbox.size = new system.drawing.size (epos - spos, m_currentlvitem.bounds.height); Editbox.location = new system.drawing.point (spos, m_currentlvitem.bounds.y); Editbox.show (); Editbox.text = m_strsubitemtext; Editbox.selectall (); Editbox.focus (); } } Public void smkmousedown (Object Sender, System.Windows.Forms.MouseEventArgs E) { m_currentlvitem = this.getitemat (E.x, E.Y); m_nx = E.x; m_ny = e.y; } } } The method is as follows: This.listview1 = new alan_editlistview (); /// /// add the columns Alan_columnheader header1 = new alan_columnhead (alan_listviewcolumnstyle.readonly); Header1.width = 120; Header1.text = "Name"; This.listview1.columns.add (header1); Alan_columnheader header2 = new alan_columnhead (alan_listviewcolumnstyle.editbox); Header2.width = 120; Header2.text = "gender"; this.listview1.columns.add (header2); Alan_columnheader header3 = new alan_columnhead (alan_listviewcolumnstyle.editbox); Header3.width = 120; Header3.Text = "Age"; This.listview1.columns.add (header3); Alan_columnheader header4 = new alan_columnhead (alan_listviewcolumnstyle.comboBox); Header4.width = 120; Header4.Text = "Professional"; This.listview1.columns.add (header4); Alan_columnheader header5 = new alan_columnhead (alan_listviewcolumnstyle.combobox); Header5.width = 120; Header5.text = "Reward"; This.listview1.columns.add (header5); /// /// Bound the Combox This.listview1.boundlisttocolumn (3, new string "," lotus "," sql server "," .NET "," vb "}); this.listview1.boundlisttocolumn (4, new string [] {" sun ", "MON", "TUE", "WED", "THU", "fri", "sat"}); ///// /// set Edit State Font and BackColor //this.listview1.comboBoxbgcolor = color.lightyellow; THISTVIEW1.COMBOBOXFONT = New System.drawing.Font ("Microsoft Sans Serif", 9F, System.drawing.FontStyle.Regular, System.drawing.graphchicsUnit.Point, ((System.byte) (0)))); THISTVIEW1.EDITFONT = New System.drawing.Font ("Microsoft Sans Serif", 9F, System.drawing.FontStyle.Regular, System.drawing.graphicsUnit.point, ((System.byte) (0))); /// /// add rows String [] s = new string [5]; s [0] = "Zhang San"; s [1] = "Men"; s [2] = "24"; s [3] = ".NET"; s [4] = "sun"; This.ListView1.Item.Add (New ListViewItem (s)); S [0] = "Li Si"; s [1] = "Men"; S [2] = "25"; s [3] = "SQL Server"; s [4] = "MON"; This.ListView1.Item.Add (New ListViewItem (s)); S [0] = "Wang Wu"; s [1] = "Men"; s [2] = "23"; s [3] = "VB"; s [4] = "TUE"; This.ListView1.Item.Add (New ListViewItem (s)); s [0] = "Zhao Liu"; s [1] = "Women"; s [2] = "22"; s [3] = "Lotus"; s [4] = "WED"; This.ListView1.Item.Add (New ListViewItem (s)); /// /// other commit attributs This.ListView1.dock = system.windows.forms.dockstyle.fill; THISTVIEW1.FONT = New System.drawing.Font ("Microsoft Sans Serif", 9.75F, System.drawing.FontStyle.Regular, System.drawing.graphicsUnit.Point ((System.byte) (0)))); THISTVIEW1.FULLROWSELECT = TRUE; this.listview1.name = "listview1"; This.listview1.size = new system.drawing.size (448, 273); this.listview1.tabindex = 0; This.listview1.view = system.windows.forms.view.details; this.controls.addrange (new system.windows.forms.control] { THISTVIEW1}); Double-click the unit you need to edit, if the column is Alan_ListViewColumnStyle.Readonly, there is no change; if the column is Alan_ListViewColumnStyle.Editbox, text box appears; if the column is Alan_ListViewColumnTyle.comBobox, the combo box appears.