Combobox listindex problem

Combobox listindex problem

Post by Ivan Debon » Tue, 07 Dec 2004 17:13:46


Hi all,

I have a combobox with dropdowncombo style so the user can type in text.
Initially the list is empty and as the user types in, records from a
preloaded recordset are filtered and listed in the list. All works fine. On
the Validate event I check if the user's entry exists in the list and if
yes, set the right listindex. In debug mode this works fine, but in runtime,
after the listindex is set to value, it changes back to -1. Here is the code
in the relevant events:

Private Sub cboPreparation_Change()
Dim lIdx As Long
Dim lSel As Long
Dim sText As String

If mbDelKey Then Exit Sub

'Populate according to user input
With cboPreparation
If (.Text <> vbNullString) And (Len(.Text) = 1) Then
sText = .Text

'Filter list by user input
[snippet] filter recordset

'Dropdown the list
If bDropDown Then
SendMessage cboPreparation.hWnd, CB_SHOWDROPDOWN, True,
ByVal 0&
End If

'Select the first entry
If .ListCount > 0 Then
.ListIndex = 0
End If

'Select the edit text
.SelStart = 1
.SelLength = Len(.Text)
End If
End With

'Find in the list
With cboPreparation
LockWindowUpdate .hWnd

lIdx = SendMessageString(.hWnd, CB_FINDSTRING, 0, .Text)
lSel = .SelStart
If lIdx >= 0 Then
.ListIndex = lIdx
.SelStart = lSel
.SelLength = Len(.Text)
End If

LockWindowUpdate False
End With
End Sub

Private Sub cboPreparation_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack Then
mbDelKey = True

ElseIf KeyAscii = vbKeyReturn Then
KeyAscii = 0
If cmdOK.Enabled Then
cmdOK_Click
End If
Else
mbDelKey = False
End If
End Sub

Private Sub cboPreparation_Validate(Cancel As Boolean)
Dim lIdx As Long
Dim sTmp As String

With cboPreparation
sTmp = Trim$(.Text)
lIdx = .ListIndex
End With

If lIdx = -1 And sTmp <> vbNullString Then
lIdx = GetIndex(cboPreparation, sTmp) 'Calls CB_FINDSTRING or
CB_FINDSTRINGEXACT

If lIdx = -1 Then
[code snippet]Add new record to the database, to the list, and
select it
End If
End If

'Set the index
SendMessage cboPreparation.hWnd, CB_SETCURSEL, ByVal lIdx, ByVal 0&
End Sub

Anybody can help??

Thanks,
Ivan
 
 
 

Combobox listindex problem

Post by Randy Birc » Wed, 08 Dec 2004 11:50:48

That is normal behavior for a combo. Don't attempt to change the list index
from within a combo property ... at the end of the property it is restored
to that which existed before. You can change it by firing a timer ... the
key is to allow the event to end before affecting the change.

--


Randy Birch
MS MVP Visual Basic
http://www.yqcomputer.com/




: Hi all,
:
: I have a combobox with dropdowncombo style so the user can type in text.
: Initially the list is empty and as the user types in, records from a
: preloaded recordset are filtered and listed in the list. All works fine.
On
: the Validate event I check if the user's entry exists in the list and if
: yes, set the right listindex. In debug mode this works fine, but in
runtime,
: after the listindex is set to value, it changes back to -1. Here is the
code
: in the relevant events:
:
: Private Sub cboPreparation_Change()
: Dim lIdx As Long
: Dim lSel As Long
: Dim sText As String
:
: If mbDelKey Then Exit Sub
:
: 'Populate according to user input
: With cboPreparation
: If (.Text <> vbNullString) And (Len(.Text) = 1) Then
: sText = .Text
:
: 'Filter list by user input
: [snippet] filter recordset
:
: 'Dropdown the list
: If bDropDown Then
: SendMessage cboPreparation.hWnd, CB_SHOWDROPDOWN, True,
: ByVal 0&
: End If
:
: 'Select the first entry
: If .ListCount > 0 Then
: .ListIndex = 0
: End If
:
: 'Select the edit text
: .SelStart = 1
: .SelLength = Len(.Text)
: End If
: End With
:
: 'Find in the list
: With cboPreparation
: LockWindowUpdate .hWnd
:
: lIdx = SendMessageString(.hWnd, CB_FINDSTRING, 0, .Text)
: lSel = .SelStart
: If lIdx >= 0 Then
: .ListIndex = lIdx
: .SelStart = lSel
: .SelLength = Len(.Text)
: End If
:
: LockWindowUpdate False
: End With
: End Sub
:
: Private Sub cboPreparation_KeyPress(KeyAscii As Integer)
: If KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack Then
: mbDelKey = True
:
: ElseIf KeyAscii = vbKeyReturn Then
: KeyAscii = 0
: If cmdOK.Enabled Then
: cmdOK_Click
: End If
: Else
: mbDelKey = False
: End If
: End Sub
:
: Private Sub cboPreparation_Validate(Cancel As Boolean)
: Dim lIdx As Long
: Dim sTmp As String
:
: With cboPreparation
: sTmp = Trim$(.Text)
: lIdx = .ListIndex
: End With
:
: If lIdx = -1 And sTmp <> vbNullString Then
: lIdx = GetIndex(cboPreparation, sTmp) 'Calls CB_FINDSTRING or
: CB_FINDSTRINGEXACT
:
: If lIdx = -1 Then
: [code snippet]Add new record to the database, to the list, and
: select it
: End If
: End If
:
: 'Set the index
: SendMessage cboPreparation.hWnd, CB_SETCURSEL, ByVal lIdx, ByVal 0&
: End Sub
:
: Anybody can help??
:
: Thanks,
: Ivan
:
:

 
 
 

Combobox listindex problem

Post by Ivan Debon » Thu, 09 Dec 2004 01:09:03

eez... All these hours spent for normal behavior!!! I'll look into it :)

Ivan

"Randy Birch" < XXXX@XXXXX.COM > schrieb im Newsbeitrag
news: XXXX@XXXXX.COM ...
index
and


 
 
 

Combobox listindex problem

Post by Randy Birc » Thu, 09 Dec 2004 03:17:30

ame thing with other actions ... try to unload a form -- any form -- from a
combo list click event. Or try unloading control array members. No can do.

--


Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/


"Ivan Debono" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...
: Jeez... All these hours spent for normal behavior!!! I'll look into it :)
:
: Ivan
:
: "Randy Birch" < XXXX@XXXXX.COM > schrieb im Newsbeitrag
: news: XXXX@XXXXX.COM ...
: > That is normal behavior for a combo. Don't attempt to change the list
: index
: > from within a combo property ... at the end of the property it is
restored
: > to that which existed before. You can change it by firing a timer ...
the
: > key is to allow the event to end before affecting the change.
: >
: > --
: >
: >
: > Randy Birch
: > MS MVP Visual Basic
: > http://vbnet.mvps.org/
: >
: >
: > "Ivan Debono" < XXXX@XXXXX.COM > wrote in message
: > news:% XXXX@XXXXX.COM ...
: > : Hi all,
: > :
: > : I have a combobox with dropdowncombo style so the user can type in
text.
: > : Initially the list is empty and as the user types in, records from a
: > : preloaded recordset are filtered and listed in the list. All works
fine.
: > On
: > : the Validate event I check if the user's entry exists in the list and
if
: > : yes, set the right listindex. In debug mode this works fine, but in
: > runtime,
: > : after the listindex is set to value, it changes back to -1. Here is
the
: > code
: > : in the relevant events:
: > :
: > : Private Sub cboPreparation_Change()
: > : Dim lIdx As Long
: > : Dim lSel As Long
: > : Dim sText As String
: > :
: > : If mbDelKey Then Exit Sub
: > :
: > : 'Populate according to user input
: > : With cboPreparation
: > : If (.Text <> vbNullString) And (Len(.Text) = 1) Then
: > : sText = .Text
: > :
: > : 'Filter list by user input
: > : [snippet] filter recordset
: > :
: > : 'Dropdown the list
: > : If bDropDown Then
: > : SendMessage cboPreparation.hWnd, CB_SHOWDROPDOWN, True,
: > : ByVal 0&
: > : End If
: > :
: > : 'Select the first entry
: > : If .ListCount > 0 Then
: > : .ListIndex = 0
: > : End If
: > :
: > : 'Select the edit text
: > : .SelStart = 1
: > : .SelLength = Len(.Text)
: > : End If
: > : End With
: > :
: > : 'Find in the list
: > : With cboPreparation
: > : LockWindowUpdate .hWnd
: > :
: > : lIdx = SendMessageString(.hWnd, CB_FINDSTRING, 0, .Text)
: > : lSel = .SelStart
: > : If lIdx >= 0 Then
: > : .ListIndex = lIdx
: > : .SelStart = lSel
: > : .SelLength = Len(.Text)
: > : End If
: > :
: > : LockWindowUpdate False
: > : End With
: > : End Sub
: > :
: > : Private Sub cboPreparation_KeyPress(KeyAscii As Integer)
: > : If KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack Then
: > : mbDelKey = True
: > :
: > : ElseIf KeyAscii = vbKeyReturn Then
: > : KeyAscii = 0
: &