As a Visual C++ user for 10 years I can say that Microsoft deserves praise for their new ListView class. The MFC ListView class was, well, unpleasant to use. C# makes life a bit easier with a richer property and method set for ListViews. Also, you can now, set the ListView to select an entire row in report mode, something that in Visual C++ you had to write a whole custom ListView control to do. Note also the nice grid lines.
Below are the files included for the Visual.NET Project. Form1.cs contains all the code in this article:
The above mini application shows you how to create a ListView in report mode and make it persistent with streams. The ListView's columns are initialized with the following code:
public
The routine uses the font sizes to determine the width of the column and the alignment flags to determine where to put the header text. The property for making the ListView a ReportView is called View and is set in the property window. Setting the FullRowSelect property allows the user to select the entire row of a ListView.
In this example, we use the edit box fields to populate the rows in our listview. Below is the routine for inserting rows into our contact list:
protected
Another function of this ListView Example is the ability to delete rows. In this example we trap the KeyDown event and look for the delete key. If the delete key is pressed, the program will remove the selected rows:
Several articles on this site already cover streams, so I'll only talk briefly about them here.
The FileStream allows you to read or write to a file. You can use the StreamWriter class to manipulate this stream for writing. In this example we use the SaveFileDialog component to prompt the user for the file we want to save too. The programmer can set this component for the extensions, defaults, Title and other specifics for the appearance of this dialog:
The code for writing the ListView out to a tab-delimited text file is shown below:
nStart = nPos1 + 1;int nPos2 = strLine.IndexOf("\t",nStart);string str2 = strLine.Substring(nStart, nPos2 - nStart); // get second column stringnStart = nPos2 + 1;string str3 = strLine.Substring(nStart); // get last column stringlistView1.InsertItem(count, str1, 0, new string[]{str2, str3}); // Add the row to the ListViewcount++; // increment rownStart = 0; // resetstrLine = m_streamReader.ReadLine(); // get next line from the stream}// Close the streamm_streamReader.Close();} }catch(Exception em){System.Console.WriteLine(em.Message.ToString());}}
This application can probably aid as a starting point for doing many spreadsheet-like applications. For example, the application could be used to create a program that goes through all the people on the list and emails them a letter addressing them with their name. (See the SMTP article on this site Sending mail in .NET using C# by Mahesh Chand). It could also be the starting point for entering contacts into a database using ADO+(see the article on this site Write data to an access database using SQL Query and ADO by Mahesh Chand.) I think you'll find that creating any such application in C# should prove quicker and easier than it was with Visual Studio 6.0.
Pro WPF: Windows Presentation Foundation in .NET 3.0