Click here to Skip to main content
Click here to Skip to main content

Reusable ListView in C# with textbox and combobox

, 22 Mar 2007
Rate this:
Please Sign up or sign in to vote.
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.

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
    this.listViewMain.AddEditableCell(1, 2); 
  2. This will place a text box in all the cells of 4th row
    this.listViewMain.AddEditableCell(3, -1); 
  3. This will place a text box in all the cells of 4th column
    this.listViewMain.AddEditableCell(-1, 3); 
  4. This will place a text box in all the cells of the ListView
    this.listViewMain.AddEditableCell(-1, -1); 

Placing a combobox

To place a combo box, use the method AddComboBoxCell.

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
    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
    this.listViewMain.AddComboBoxCell(1, 2, grades); 

  2. This will place a combo box in all the cells of 4th row
    this.listViewMain.AddComboBoxCell (3, -1, grades); 
  3. This will place a combo box in all the cells of 4th column
    this.listViewMain.AddComboBoxCell (-1, 3, grades); 
  4. This will place a combo box in all the cells of the ListView
    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.

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

Share

About the Author

Shine Kumar
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

 
General很好用库!非常感谢! Pinmemberjatq20102-Aug-14 6:09 
QuestionWhat is the event for changed or modified items ? PinmemberMember 874505219-Nov-13 1:50 
QuestionVery nice but... Pinmemberpierumberto29-Sep-12 5:58 
QuestionHow to set the state of a column/cell after change the value to not editable textbox ? PinmemberMember 874505228-Aug-12 0:31 
GeneralIt's really nice! Pinmemberqimingxingwen16-May-12 18:15 
GeneralReading text from combo or textbox Pinmembernordsluttning26-Oct-10 13:06 
GeneralComboBox moving PinmemberOmar.Pessoa17-Feb-10 6:59 
QuestionTagging Images in Windows Application C# Pinmembernace2k217-Jun-09 5:24 
GeneralCropping Images Pinmembernace2k216-Jun-09 22:42 
Generaldelete combobox Pinmemberkumi_aberer@hotmail.com17-Mar-09 22:33 
GeneralRe: delete combobox PinmemberMember 188752623-Feb-10 20:13 
GeneralMy vote of 2 Pinmemberme_pollack2-Feb-09 18:46 
QuestionHow to add textbox PinmemberGhelai ALonzo17-Nov-08 23:43 
AnswerRe: How to add textbox PinmemberGhelai ALonzo19-Nov-08 17:43 
GeneralVery good article PinmemberGolllli8-Nov-08 23:01 
Generalhi Pinmembersujeeba26-Oct-08 11:13 
QuestionHow can I clear the Hashtable Pinmembermykey08153-Jul-08 23:30 
Generali want to know the detail of GetSubItemRect, GetKey Pinmemberfatema7-May-08 22:23 
GeneralExit from cell - little update from me PinmemberJosip8410-Apr-08 4:59 
GeneralAfterLabelEdit PinmemberDekkie8-Apr-08 23:10 
GeneralRe: AfterLabelEdit Pinmemberniczobxor7-Feb-12 3:27 
QuestionHow to improve this control ? Pinmembercomebaker20-Aug-07 15:54 
AnswerRe: How to improve this control ? Pinmembercomebaker23-Aug-07 20:24 
GeneralRe: How to improve this control ? PinmemberShine Kumar27-Aug-07 14:39 
GeneralRe: How to improve this control ? Pinmembercomebaker29-Aug-07 17:00 
GeneralQuestion: Pinmemberjdelito7-Aug-07 21:42 
GeneralRe: Question: Pinmembercomebaker20-Aug-07 15:56 
QuestionNice but missing something crucial in an article PinmemberDEGT23-May-07 2:38 
AnswerRe: Nice but missing something crucial in an article PinmemberDarren Martz20-Aug-08 23:33 
QuestionHow can I use your control in a VB proyect? PinmemberXTV28-Mar-07 1:54 
AnswerRe: How can I use your control in a VB proyect? PinmemberShine Kumar8-Apr-07 15:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Mar 2007
Article Copyright 2007 by Shine Kumar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid