-
Sub Classing a ListBox
I'm writing an application which has a listbox full of items. The Style property in the ListBox is set to 1 - CheckBox. I need to stop the user unchecking the first item in the listbox. I've written the code to subclass the ListBox to detect the WM_LBUTTONDOWN message, but I don't know how to find out whether the check box of the first item in the list was clicked.
How do I check if the check box of the first item in the list was clicked?
Thanks in advance
:)
-
Code:
Option Explicit
Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal _
lParam As String)
Private Const LB_GETSEL = &H187
Public Function IsSelected(hwnd As Long, iIndex As Integer) As Boolean
Dim lRet&
lRet = SendMessage(hwnd, LB_GETSEL, iIndex, 0)
If lRet Then
IsSelected = True
Exit Function
Else
IsSelected = False
End If
End Function
Private Sub cmdIsSelected_Click()
'put the window handle of your listbox here, and the index of the item required....
If IsSelected(hwnd, iCount) Then MsgBox "SELECTED"
End Sub
;)
-
1 Attachment(s)
heres a project for ease of use....
-
1 Attachment(s)
Thanks for attaching your code, but it wasn't quite what I was after.
What I'm after is a way of stopping a user unchecking a checked item in a list box.
I've changed the code slightly, so that Hello 1 (the first item in the list) is checked during the Form_Load event.
How do I make sure the user can't uncheck the first Item?
:)
-
1 Attachment(s)
-
Betcha want the source now dontcha?
-
-
seeing as i'm such a nice guy here you go.....it was a bit of a pain this one....but it works sorry bout the formatting of the code - didn't have time to worry about it.....if you need any info about whats going on then just holler ;)
-
1 Attachment(s)
blatently trying to increment the post count ;)
-
-
And I won't tell that you are blatently trying to increment the post count.
Now I'm doing it.
D'oh
;)
-
I dont understand why you are fooling with all this subclassing ect...
If i understand correctly you just want it so the user can not unselect/uncheck the first item in your listbox right?
Well just put this code in your in Lists Mousedown event.
For i = 0 To 0
List1.Selected(i) = True
Next
-
why are you using a For Loop??
you could just do it like this...
Code:
lst1.selected(0) = True
and FWIW....It was a demonstration of a different way to do it, and it worked......
-
I've just tried it and I can achieve the same effect my adding your code to the MouseDown event.
I think I over complicated matters by going down the Sub Class road.
Nice one Arc, and thanks again Crispin
:)