The built-in Checkbox functionality for ListViews will put a checkbox at the beginning of each row. I only needed to put the check box at the beginning of certain rows. The following posting show a simply way to achieve this requirement. I did not use this method exactly as it is shown, but it was definitely a good start:
This blog is a depository for things I have found regarding VB.NET and C#.NET development (and other things I may need to remember). This blog is primarily for my own reference, but if the information I found is useful to others, then that is great. If others want to contribute with comments or other information, then that is great too.
Showing posts with label listview. Show all posts
Showing posts with label listview. Show all posts
Wednesday, May 26, 2010
Thursday, June 12, 2008
Getting Selection and Arrow keys to behave right with a ListBox in the.NET Compact Framework
I had a form in a Window Mobile application that contains a Label and a ListBox. I wanted the ListBox to behave according to the Windows Mobile UI guidelines where you click on an item with the stylus and it is automatically selected. I used the SelectedIndexChanged event to implement this behavior and it worked fine when an item was selected with the stylus. However, with all of the different kinds of devices, many of them have partial or even full keyboards and at the very least they have a joypad or equivalent, my users were wanting to use the arrow keys to navigate to a specific item in the list and then press the enter button (never using the stylus). Unfortunately, the SelectedIndexChanged event is fired anytime the ListBox’s selected item is change (by stylus or keys) and since using the arrow keys usually requires scrolling through several items before getting to the item you want, the next item in the list is always selected (be it the one you want or not). I could not find a way in the .NET Compact Framework to get the ListBox to work like I wanted.
My solution was to replace the ListBox with a ListView. Change the View property to Details and change the Activation property to OneClick. Then use the ItemActivate event provided by the ListView instead of the SelectedIndexChanged event. You also have to add at least one Column to the ListView, and you can optionally set HeaderStyle to None to get rid of the column header and make the ListView look just like a ListBox.
My solution was to replace the ListBox with a ListView. Change the View property to Details and change the Activation property to OneClick. Then use the ItemActivate event provided by the ListView instead of the SelectedIndexChanged event. You also have to add at least one Column to the ListView, and you can optionally set HeaderStyle to None to get rid of the column header and make the ListView look just like a ListBox.
Labels:
arrow keys,
itemactivate,
listbox,
listview,
selectedindexchanged,
selection,
stylus
Sunday, March 23, 2008
Exporting ListView to CSV File in VB.NET
I have posted an article on copying a ListView to the Windows Clipboard, but I have also found it useful to export a ListView directly to a .CSV file (comma separated value file). The process to go from a ListView to the clipboard or to a CSV file is similar.
There are number of rules for creating a CSV file. Here is a good place to look if you want to see them all. I have simplified the process to a few rules that will work for most cases. I put quotes around all values and column names. Technically, this is not required, but by putting them around all cases eliminates the need to determine if you need them or not. I also replace any quote in the data with a double quote.
You may want to get the file name from the user and automatically open the file in Excel too. Here is an example of how to do that:
There are number of rules for creating a CSV file. Here is a good place to look if you want to see them all. I have simplified the process to a few rules that will work for most cases. I put quotes around all values and column names. Technically, this is not required, but by putting them around all cases eliminates the need to determine if you need them or not. I also replace any quote in the data with a double quote.
Public Function ExportListViewToCSV(ByVal filename As String, ByVal lv As ListView) As Boolean
Try
' Open output file
Dim os As New StreamWriter(filename)
' Write Headers
For i As Integer = 0 To lv.Columns.Count - 1
' replace quotes with double quotes if necessary
os.Write("""" & lv.Columns(i).Text.Replace("""", """""") & """,")
Next
os.WriteLine()
' Write records
For i As Integer = 0 To lv.Items.Count - 1
For j As Integer = 0 To lv.Columns.Count - 1
os.Write("""" & lv.Items(i).SubItems(j).Text.Replace("""", """""")+ """,")
Next
os.WriteLine()
Next
os.Close()
Catch ex As Exception
' catch any errors
Return False
End Try
Return True
End Function
You may want to get the file name from the user and automatically open the file in Excel too. Here is an example of how to do that:
Public Sub TestExportToCSV()
Dim dlg As New SaveFileDialog
dlg.Filter = "CSV files (*.CSV)|*.csv"
dlg.FilterIndex = 1
dlg.RestoreDirectory = True
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
If ExportListViewToCSV(dlg.FileName, ListView1) Then
Process.Start(dlg.FileName)
End If
End If
End Sub
Sunday, June 17, 2007
Copy ListView to Clipboard in VB.NET, C#, and VB6
It is often useful to be able to copy the contents of a ListView to the Windows Clipboard. I have written a function for VB.NET, C#, and VB6 called CopyListViewToClipboard that will copy the contents of a ListView (in Details mode) to the Windows Clipboard. Additional functions are also available for a ListBox control here. The column headers will be the first row and the items in the listview will be the remaining rows. To make the copied data more compatible with applications like Excel, a tab character is placed between items in a row and a carriage return/linefeed is placed at the end of the row.
Here is an example ListView with some data in it:

Here is what is copied to the clipboard and then to Excel:

VB.NET:
C#
VB6
Here is an example ListView with some data in it:

Here is what is copied to the clipboard and then to Excel:

VB.NET:
Imports System.text
...
Public Sub CopyListViewToClipboard(ByVal lv As ListView)
Dim buffer As New StringBuilder
For i As Integer = 0 To lv.Columns.Count - 1
buffer.Append(lv.Columns(i).Text)
buffer.Append(vbTab)
Next
buffer.Append(vbCrLf)
For i As Integer = 0 To lv.Items.Count - 1
For j As Integer = 0 To lv.Columns.Count - 1
buffer.Append(lv.Items(i).SubItems(j).Text)
buffer.Append(vbTab)
Next
buffer.Append(vbCrLf)
Next
My.Computer.Clipboard.SetText(buffer.ToString)
End Sub
C#
using System.Text;
...
public void CopyListViewToClipboard(ListView lv)
{
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < lv.Columns.Count; i++)
{
buffer.Append(lv.Columns[i].Text);
buffer.Append("\t");
}
buffer.Append("\n");
for (int i = 0; i < lv.Items.Count; i++)
{
for (int j = 0; j < lv.Columns.Count; j++)
{
buffer.Append(lv.Items[i].SubItems[j].Text);
buffer.Append("\t");
}
buffer.Append("\n");
}
Clipboard.SetText(buffer.ToString());
}
VB6
Public Sub CopyListViewToClipboard(lv As ListView)
Dim buf As String
Dim i As Integer
Dim j As Integer
For i = 1 To lv.ColumnHeaders.count
buf = buf + lv.ColumnHeaders.Item(i).Text + vbTab
Next
buf = buf + vbCrLf
For i = 1 To lv.ListItems.count
buf = buf + lv.ListItems(i).Text
For j = 1 To lv.ColumnHeaders.count - 1
buf = buf + vbTab + lv.ListItems(i).SubItems(j)
Next
buf = buf + vbCrLf
Next
buf = buf + vbCrLf
Clipboard.Clear
Clipboard.SetText buf
End Sub
How to Fit all columns in a ListView in C#, VB.NET, and VB6
Code is shown below to Autofit the data in a listview for C#, VB.NET, and VB6:
C#
VB.NET
VB6
Unfortunately, the VB6 solution is a bit more complicated:
You can change the LVSCW_AUTOSIZE_USEHEADER to LVSCW_AUTOSIZE if you do not want to consider the Header when adjusting the size.
More examples of the VB6 approach can be found at:
http://vbnet.mvps.org/index.html?code/comctl/lvcolumnautosize.htm
C#
public void AutoFitListView(ListView lv)
{
for (int i = 0; i <= lv.Columns.Count - 1; i++) { lv.Columns[i].Width = -2; // longest col head and item // -1 for just longest item } }
VB.NET
Public Sub AutoFitListView(ByRef lv As ListView)
For i As Integer = 0 To lv.Columns.Count - 1
lv.Columns(i).Width = -2
Next
End Sub
VB6
Unfortunately, the VB6 solution is a bit more complicated:
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long
Public Sub AutoFitListView(lv As ListView)
Dim i As Long
For i = 0 To lv.ColumnHeaders.count - 1
Call SendMessage(lv.hwnd, LVM_SETCOLUMNWIDTH, i, ByVal LVSCW_AUTOSIZE_USEHEADER)
Next
End Sub
You can change the LVSCW_AUTOSIZE_USEHEADER to LVSCW_AUTOSIZE if you do not want to consider the Header when adjusting the size.
More examples of the VB6 approach can be found at:
http://vbnet.mvps.org/index.html?code/comctl/lvcolumnautosize.htm
Subscribe to:
Posts (Atom)