Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Reusable ListView in C# with textbox and combobox

0.00/5 (No votes)
22 Mar 2007 1  
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