Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#
Article

Reusable ListView in C# with textbox and combobox

Rate me:
Please Sign up or sign in to vote.
3.67/5 (24 votes)
22 Mar 20073 min read 253.8K   15.9K   67   33
A Reusable ListView in C# with textbox and combobox
Screenshot - image001.jpg

Introduction

Please don't ask me why I am only posting articles related to List View Controls! Even I wonder about this. In my first article, I had posted some code to create an MFC List Control with Edit Boxes and Combo Boxes. Here I am appearing again, with the same old wine but in C#.NET bottle!

In VC++, the problem was quite easy to solve as there was an API GetSubItemRect. Unfortunately, in C#.NET, there is no such method to get the sub item bounds. But as the Win32 API is still there in the operating system, we can access it through our C# code, and get the sub item coordinates. Once you get it correctly, you can create and display any controls within your ListView.

So, as usual, we have a class derived from ListView, and I gave a sweet, professional name too: ListViewEx.

Features

  1. Editable cells: We can add text boxes in individual cells, individual rows or individual columns.
  2. Combo box in cells: We can add combo boxes in individual cells, individual rows or individual columns.
  3. If subitems are not added for an item, it will be added automatically, while clicking on the item.

1

3

About the Code

  • This code contains a reusable class derived from ListView.
  • This class is developed using Microsoft .NET framework 1.1. But it will execute in .NET 2.0 too.
  • Inside the class, a single hash table is used to store the information of the custom cell. By custom cell, I mean a cell that contains a textbox or combo box. The key of the HashTable is the row-column information of the cell. The value is a string collection if the cell is for combo box or null if the cell is for textbox. After getting the clicked item and sub item indices, we will check this hash table for deciding what to display.
  • This code will load user32.dll and invoke the method SendMessage, in order to get the sub item rectangle.

Pre Conditions

  1. ListView's View property should be Detail
  2. ListView's FullRowSelect property should be true

Using the Code

Placing a textbox

To place a text box, use the method AddEditableCell.

C#
void AddEditableCell(int row, int col) 

We have to pass the 0 based index of the item and sub item to this method. For making individual row/column editable, pass -1 as index.

Examples

  1. This will place a text box in the cell : 2 X 3
    C#
    this.listViewMain.AddEditableCell(1, 2); 
  2. This will place a text box in all the cells of 4th row
    C#
    this.listViewMain.AddEditableCell(3, -1); 
  3. This will place a text box in all the cells of 4th column
    C#
    this.listViewMain.AddEditableCell(-1, 3); 
  4. This will place a text box in all the cells of the ListView
    C#
    this.listViewMain.AddEditableCell(-1, -1); 

Placing a combobox

To place a combo box, use the method AddComboBoxCell.

C#
void AddComboBoxCell( int row, int col, StringCollection data )
void AddComboBoxCell( int row, int col, string[] data )

We have to pass the 0 based indices as well as the data to be displayed inside the combo box.

Examples

  1. Create data for combobox
    C#
    StringCollection grades = new StringCollection();
    grades.AddRange(new string[]{ "A", "B", "C", "D", "E" });

    This will place a combo box in the cell: 2 X 3
    C#
    this.listViewMain.AddComboBoxCell(1, 2, grades); 

  2. This will place a combo box in all the cells of 4th row
    C#
    this.listViewMain.AddComboBoxCell (3, -1, grades); 
  3. This will place a combo box in all the cells of 4th column
    C#
    this.listViewMain.AddComboBoxCell (-1, 3, grades); 
  4. This will place a combo box in all the cells of the ListView
    C#
    this.listViewMain.AddComboBoxCell (-1, -1, grades); 

Configuring ListView to add subitems automatically

If an item is not having many subitems as per the number of columns, this class will add subitems automatically, upon clicking on the item. We can configure this using the property AddSubItem.

Example

If subitem is not added, add it automatically on clicking an item.

C#
this.listViewMain.AddSubItem = true; 

By default, this class will not add subitems automatically. So please set the value to true to make this feature enabled.

Conclusion

That's it! Have a look at the code, use it, and customize as per your wish. I will come again with List View Controls. Let me study some new programming languages.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Hi,this is Shine from India, working as a software engineer. I am working mainly with C++ and C#. I like to create reusable stuffs.

Comments and Discussions

 
QuestionFails to hide the textbox, dropdown box when scrolled Pin
Bhushan A3-Jul-17 5:34
Bhushan A3-Jul-17 5:34 
AnswerRe: Fails to hide the textbox, dropdown box when scrolled Pin
jlp_1_28-Nov-17 10:54
jlp_1_28-Nov-17 10:54 
General很好用库!非常感谢! Pin
jatq20102-Aug-14 5:09
jatq20102-Aug-14 5:09 
QuestionWhat is the event for changed or modified items ? Pin
atisah19-Nov-13 0:50
atisah19-Nov-13 0:50 
QuestionVery nice but... Pin
pierumberto29-Sep-12 4:58
pierumberto29-Sep-12 4:58 
QuestionHow to set the state of a column/cell after change the value to not editable textbox ? Pin
atisah27-Aug-12 23:31
atisah27-Aug-12 23:31 
GeneralIt's really nice! Pin
qimingxingwen16-May-12 17:15
qimingxingwen16-May-12 17:15 
GeneralReading text from combo or textbox Pin
nordsluttning26-Oct-10 12:06
nordsluttning26-Oct-10 12:06 
GeneralComboBox moving Pin
Omar.Pessoa17-Feb-10 5:59
Omar.Pessoa17-Feb-10 5:59 
QuestionTagging Images in Windows Application C# Pin
nace2k217-Jun-09 4:24
nace2k217-Jun-09 4:24 
GeneralCropping Images Pin
nace2k216-Jun-09 21:42
nace2k216-Jun-09 21:42 
Generaldelete combobox Pin
kumi_aberer@hotmail.com17-Mar-09 21:33
kumi_aberer@hotmail.com17-Mar-09 21:33 
GeneralRe: delete combobox Pin
neitcouq23-Feb-10 19:13
neitcouq23-Feb-10 19:13 
GeneralMy vote of 2 Pin
me_pollack2-Feb-09 17:46
me_pollack2-Feb-09 17:46 
QuestionHow to add textbox Pin
Ghelai ALonzo17-Nov-08 22:43
Ghelai ALonzo17-Nov-08 22:43 
AnswerRe: How to add textbox Pin
Ghelai ALonzo19-Nov-08 16:43
Ghelai ALonzo19-Nov-08 16:43 
GeneralVery good article Pin
Golllli8-Nov-08 22:01
Golllli8-Nov-08 22:01 
Generalhi Pin
sujeeba26-Oct-08 10:13
sujeeba26-Oct-08 10:13 
QuestionHow can I clear the Hashtable Pin
mykey08153-Jul-08 22:30
mykey08153-Jul-08 22:30 
Generali want to know the detail of GetSubItemRect, GetKey Pin
fatema7-May-08 21:23
fatema7-May-08 21:23 
I cant figure out what is these two methods are doing .......need help Frown | :(
GeneralExit from cell - little update from me Pin
Josip8410-Apr-08 3:59
Josip8410-Apr-08 3:59 
GeneralAfterLabelEdit Pin
Dekkie8-Apr-08 22:10
Dekkie8-Apr-08 22:10 
GeneralRe: AfterLabelEdit Pin
niczobxor7-Feb-12 2:27
niczobxor7-Feb-12 2:27 
QuestionHow to improve this control ? Pin
comebaker20-Aug-07 14:54
comebaker20-Aug-07 14:54 
AnswerRe: How to improve this control ? Pin
comebaker23-Aug-07 19:24
comebaker23-Aug-07 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.