Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listbox Question

80 views
Skip to first unread message

David Pope

unread,
Jun 25, 2003, 5:18:26 PM6/25/03
to
Here is my problem...

I have 2 listbox's. Checkbox is enabled on both.

Listbox1
Listbox2

Both listboxes contain the same exact items. When I scroll in listbox1, I
would like listbox2 to scroll in sync with listbox1.

Does anyone have any code examples?

Thanks,

David


Tom Esh

unread,
Jun 25, 2003, 5:47:55 PM6/25/03
to
On Wed, 25 Jun 2003 16:18:26 -0500, "David Pope" <dp...@satx.rr.com>
wrote:

Use the listbox Scroll event to sync the TopIndex properties. Ideally
you should use a flag variable to prevent setting the TopIndex prop in
one from firing the Scroll event in the other. (Saves a few CPU
cycles.)

Dim m_NoScroll As Boolean 'module-level flag var

Private Sub List1_Scroll()
If Not m_NoScroll Then
m_NoScroll = True
List2.TopIndex = List1.TopIndex
m_NoScroll = False
End If
End Sub

Private Sub List2_Scroll()
If Not m_NoScroll Then
m_NoScroll = True
List1.TopIndex = List2.TopIndex
m_NoScroll = False
End If
End Sub

==============
Alternatively if you used an ~array~ of listboxes, you could use a
local Static var for the flag:

Private Sub List1_Scroll(Index As Integer)
Static NoScroll As Boolean

If Not NoScroll Then
NoScroll = True
If Index = 0 Then
List1(1).TopIndex = List1(0).TopIndex
Else
List1(0).TopIndex = List1(1).TopIndex
End If
NoScroll = False
End If
End Sub


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Jim Carlock

unread,
Jun 25, 2003, 7:06:14 PM6/25/03
to
Question in line with the previous ListBox question...

I'm converting some code from VB3 to VB6 and there's an API call being
performed that's supposed to identify if an item is in a listbox...

'Declare Function SendMessageByString& _
' Lib "User" Alias "SendMessage" ( _
' ByVal hwnd%, ByVal wMsg%, _
' ByVal wParam%, ByVal lParam$)
Declare Function SendMessageByString Lib "user32.dll" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Pos& = SendMessageByString(lstPlts.hwnd, _
LB_FINDSTRINGEXACT, -1, strPlant)

The SendMessageByString is an API call to SendMessageA (was originally a
16-bit SendMessage).

Is there a better method to finding information in the listbox? The
transformation from 16-bit SendMessage to 32-bit SendMessageA broke the API
call... and the old parameter of LB_FINDSTRINGEXACT was/is set to:

Global Const WM_USER = &H400
Global Const CB_FINDSTRING = (WM_USER + 12)
Global Const CB_FINDSTRINGEXACT = (WM_USER + 24)

That might be wrong, I'm looking for references to the SendMessageA to see
if I can modify it to make it work and if worst comes to worst I'll just
For/Next it.

--
Jim Carlock
http://www.microcosmotalk.com
Feel free to post back to the newsgroup for all to witness!

Jim Carlock

unread,
Jun 25, 2003, 7:38:55 PM6/25/03
to
Oops.. missed some constants declared in the beginning of the procedure...

Const WM_USER = &H400
Const LB_FINDSTRINGEXACT = (WM_USER + 35)

That comes out to &H423.

I found something at http://www.mvps.org/vbnet

Going to try the following:

Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2

--
Jim Carlock
http://www.microcosmotalk.com
Feel free to post back to the newsgroup for all to witness!


"Jim Carlock" <nos...@127.0.0.1> wrote in message
news:%2352Tj52...@tk2msftngp13.phx.gbl...

Jim Carlock

unread,
Jun 25, 2003, 7:49:54 PM6/25/03
to
The Constants being fixed fixed the problem.

If anyone has another suggestion as to how to fill a listbox with unique
values...

What's happening, is 600+ records are being read and two listboxes are being
filled. Only unique values are being placed into the listboxes. I'm thinking
about breaking it up into two different queries... a query to find unique
values in a database will work fine as well... It all happens so fast that
there's no noticeable difference. However if it's done on a 386... oh well,
no use thinking about that.

Open to any other suggestions... I have two methods in mind, the one I just
fixed is working fine until the API changes again.

Jim Carlock

unread,
Jun 25, 2003, 10:05:48 PM6/25/03
to
Just in case anyone wants the API code - it is used to determine if an exact
string is within any given listbox:

Dim Pos as Long, hw as Long

Const LB_FINDSTRINGEXACT = &H1A2

hw = List1.hwnd

Pos& = SendMessageByString(hw, _
LB_FINDSTRINGEXACT, -1, ByVal (strStuff))

If Pos& = -1 Then
' string wasn't found, add to list
List1.AddItem strStuff
End If

You'll need to make sure you don't send a Null string "" into the API call.

Thanks to Randy Birch'es website, http://www.mvps.org/vbnet for the
clarification on the proper constant to use!

Phill. W

unread,
Jun 26, 2003, 9:16:35 AM6/26/03
to
"David Pope" <dp...@satx.rr.com> wrote in message
news:eN0IsC2O...@TK2MSFTNGP10.phx.gbl...
. . .

> I have 2 listbox's. Checkbox is enabled on both.
. . .

> Both listboxes contain the same exact items. When I scroll in listbox1, I
> would like listbox2 to scroll in sync with listbox1.

If you're using VB6, look into the Scroll Event:

Sub List1_Scroll( ...
List1.TopIndex = List1.TopIndex
End Sub

HTH,
Phill W.

0 new messages