Click here to Skip to main content
Licence 
First Posted 22 Mar 2007
Views 113,487
Downloads 4,391
Bookmarked 54 times

Reusable ListView in C# with textbox and combobox

By | 22 Mar 2007 | Article
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

About the Author

Shine Kumar

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIt's really nice! Pinmemberqimingxingwen17:15 16 May '12  
GeneralReading text from combo or textbox Pinmembernordsluttning12:06 26 Oct '10  
GeneralComboBox moving PinmemberOmar.Pessoa5:59 17 Feb '10  
QuestionTagging Images in Windows Application C# Pinmembernace2k24:24 17 Jun '09  
GeneralCropping Images Pinmembernace2k221:42 16 Jun '09  
Generaldelete combobox Pinmemberkumi_aberer@hotmail.com21:33 17 Mar '09  
GeneralRe: delete combobox PinmemberMember 188752619:13 23 Feb '10  
GeneralMy vote of 2 Pinmemberme_pollack17:46 2 Feb '09  
QuestionHow to add textbox PinmemberGhelai ALonzo22:43 17 Nov '08  
AnswerRe: How to add textbox PinmemberGhelai ALonzo16:43 19 Nov '08  
GeneralVery good article PinmemberGolllli22:01 8 Nov '08  
Generalhi Pinmembersujeeba10:13 26 Oct '08  
QuestionHow can I clear the Hashtable Pinmembermykey081522:30 3 Jul '08  
Generali want to know the detail of GetSubItemRect, GetKey Pinmemberfatema21:23 7 May '08  
GeneralExit from cell - little update from me PinmemberJosip843:59 10 Apr '08  
GeneralAfterLabelEdit PinmemberDekkie22:10 8 Apr '08  
GeneralRe: AfterLabelEdit Pinmemberniczobxor2:27 7 Feb '12  
QuestionHow to improve this control ? Pinmembercomebaker14:54 20 Aug '07  
AnswerRe: How to improve this control ? Pinmembercomebaker19:24 23 Aug '07  
GeneralRe: How to improve this control ? PinmemberShine Kumar13:39 27 Aug '07  
GeneralRe: How to improve this control ? Pinmembercomebaker16:00 29 Aug '07  
GeneralQuestion: Pinmemberjdelito20:42 7 Aug '07  
GeneralRe: Question: Pinmembercomebaker14:56 20 Aug '07  
QuestionNice but missing something crucial in an article PinmemberDEGT1:38 23 May '07  
AnswerRe: Nice but missing something crucial in an article PinmemberDarren Martz22:33 20 Aug '08  

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.

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