Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

Focus on Building the Console - Part 2 of 2

Written by Brent Pinkley  [author's bio]  [read 40297 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3  Page 4 

Determining whether a list item is checked is a little more involved. I want to credit Phillip Koebbe for providing the code necessary for determining the state of a list item's checkbox in a forum post. The Console application uses this process to evaluate which items are checked for the purposes of removing only the checked items from the list view. Due to the size of the code listing, you will want to check it out in the downloadable code that accompanies this article, since it is too much to list here.

To toggle an option on or off, you can do something similar to the following, which toggles the gridlines for the list view:

' Additional module declaration
Public Const LVS_EX_GRIDLINES = &H1

' In form
Private Sub ToggleGridlines _
(ByVal bolState As Boolean)

lvwMessages.SetFocus
' Get handle to ListView
hWnd = GetFocus()

If bolState Then
' Show gridlines
SendMessage hWnd, _
LVM_SETEXTENDEDLISTVIEWSTYLE, _
LVS_EX_GRIDLINES, LVS_EX_GRIDLINES
Else
' Hide gridlines
SendMessage hWnd, _
LVM_SETEXTENDEDLISTVIEWSTYLE, _
LVS_EX_GRIDLINES, 0
End If
End Sub

In a similar way, you can auto-size the column widths to either the header caption or the actual text in the line items:

' Additional module declaration
Public Const LVSCW_AUTOSIZE = -1
Public Const LVSCW_AUTOSIZE_USEHEADER = -2

' In form
Private Sub ColumnAutoSize(ByVal lngIndex As Long, _
ByVal bolAutoSize As Boolean)

If bolAutoSize Then
' Autosize to column data
SendMessage hWnd, LVM_SETCOLUMNWIDTH, _
lngIndex, LVSCW_AUTOSIZE
Else
' Autosize to header
SendMessage hWnd, LVM_SETCOLUMNWIDTH, _
lngIndex, LVSCW_AUTOSIZE_USEHEADER
End If
End Sub

You are now well on your way to creating a fully functional list view control. As an added benefit, you might also allow the user to click on a column header and sort that column alternately in an ascending or descending way. You can actually use a built in event of the list view to do this one. Imagine that!

Private Sub lvwMessages_ColumnClick _
(ByVal Index As Long)
' If list already sorted by this column,
' reverse order
If lvwMessages.SortKey = Index - 1 Then
lvwMessages.SortOrder = _
lvwMessages.SortOrder Xor 1
Else
lvwMessages.SortOrder = lvwAscending
End If

lvwMessages.SortKey = Index - 1
End Sub

As a final bonus for the user, you might also want to add a pop-up menu for allowing quick options related to the selected list item. The Console uses a basic version of a pop-up menu to allow a message to be read, forwarded or deleted. It operates on the ItemClick event of the list view. A more sophisticated approach would be to monitor the windows events that are triggered when a user clicks and holds on an item, and pop up a menu after a certain time has elapsed. Unfortunately, this is approach is fairly involved, since there is not a standard MouseDown/MouseUp set of events associated with the list view. You could even go a step further and position the menu based on the X, Y coordinates, but this is extra work as well. To understand the simplified approach, you can add the following to your application:

' Additional Module declarations
Public Const MF_ENABLED = &H0&
Public Const MF_STRING = &H0&
Public Const MF_SEPARATOR = &H800&
Public Const TPM_LEFTALIGN = &H0&
Public Const TPM_TOPALIGN = &H0&
Public Const TPM_RETURNCMD = &H100&

Public Declare Function CreatePopupMenu Lib _
"Coredll" () As Long
Public Declare Function AppendMenu Lib "Coredll" _
Alias "AppendMenuW" (ByVal hMenu As Long, _
ByVal wFlags As Long, _
ByVal wIDNewItem As Long, _
ByVal lpNewItem As String) As Long
Public Declare Function TrackPopupMenuEx Lib _
"Coredll" (ByVal hMenu As Long, _
ByVal un As Long, ByVal n1 As Long, _
ByVal n2 As Long, ByVal hWnd As Long, _
lpTPMParams As Long) As Long

' In form
Private Function ShowPopupMenu _
(ByVal intPosLeft As Integer, _
ByVal intPosTop As Integer) As Integer

' Pop-up Menu Handle
hMenu = CreatePopupMenu

' Construct menu
AppendMenu hMenu, MF_ENABLED Or MF_STRING, _
1, "Read"
AppendMenu hMenu, MF_ENABLED Or MF_STRING, _
2, "Forward"
AppendMenu hMenu, MF_SEPARATOR, 0, ""
AppendMenu hMenu, MF_ENABLED Or _
MF_STRING, 3, "Delete"

' Return result
ShowPopupMenu = TrackPopupMenuEx(hMenu, _
TPM_LEFTALIGN Or TPM_TOPALIGN Or _
TPM_RETURNCMD, intPosLeft, intPosTop, _
Me.hWnd, 0)
End Function

Private Sub lvwMessages_ItemClick _
(ByVal Index As Long)

' Capture list item clicked
lngCurItem = Index - 1

' Determine menu position
intPosLeft = 100
intPosTop = 200

' Capture menu item selected
intMenuOption = ShowPopupMenu(intPosLeft, _
intPosTop)

Select Case intMenuOption
Case 1 ' Read
' Code for read option
Case 2 ' Forward
' Code for forward option
Case 3 ' Delete
' Code for delete option
End Select
End Sub

Previous Page  Next Page