Home All Groups Group Topic Archive Search About
Author
10 May 2005 8:59 PM
dm4714
Hello --

I'm populating a combo box from a database with a list of servername.  If
the a servername that is within a combo box matches the current machine
name, I want to select the servername from the combo and have it
highlighted.

My code works and selects the correct server, however it doesn't highlight.
What am I doing wrong?

Here is code that loops thru control:

For i = 0 to cboLocalServers.ListCount -1
   If cboLocalServers(i) = strComputerName Then
      cboLocalServers.ListIndex = i
      Call cboLocalServers_Click  ' all the click event to try and
highlight -- but it doesn't do it
      Exit For
   End If
   ' not found
  cboLocalServers.ListIndex = -1
Next

Author
10 May 2005 9:19 PM
Casey Provance
My favourite way to select items in a list/combp is via a little API.
Consider the following:

'General Decs
'Typesafe decs for SendMessage
Private Declare Function SendMessageByString Lib "user32" Alias
"SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String) As Long

Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA"
_
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Const LB_FINDSTRING             As Long = &H18F
Private Const LB_SETCURSEL              As Long = &H186

Private Const CB_FINDSTRING             As Long = &H14C
Private Const CB_SETCURSEL              As Long = &H14E


'The function
'Usage:  hWnd: The window handle to the Listbox/ComboBox
'            sItem: The string item of the item you are searching for in the
List/Combo
'            bIsCombo: True if using a ComboBox, False if using a ListBox
'
'            Return Value: 0 on success (string found and selected), -1 on
fail (string not found)
'
Public Function SetListItem(hWnd As Long, sItem As String, bIsCombo As
Boolean) As Long
    Dim lItem   As Long
    Dim lStrMsg As Long
    Dim lCurMsg As Long

    lStrMsg = LB_FINDSTRING
    lCurMsg = LB_SETCURSEL

    If bIsCombo Then
        lStrMsg = CB_FINDSTRING
        lCurMsg = CB_SETCURSEL
    End If

    lItem = SendMessageByString(hWnd, lStrMsg, -1, sItem)
    SendMessageByNum hWnd, lCurMsg, lItem, 0

    SetListItem = lItem
End Function
'==========================================

Hope this helps.  :)

- Kev


Show quote
"dm4714" <spam@spam.net> wrote in message
news:ObY$tMaVFHA.1152@tk2msftngp13.phx.gbl...
> Hello --
>
> I'm populating a combo box from a database with a list of servername.  If
> the a servername that is within a combo box matches the current machine
> name, I want to select the servername from the combo and have it
> highlighted.
>
> My code works and selects the correct server, however it doesn't
> highlight. What am I doing wrong?
>
> Here is code that loops thru control:
>
> For i = 0 to cboLocalServers.ListCount -1
>   If cboLocalServers(i) = strComputerName Then
>      cboLocalServers.ListIndex = i
>      Call cboLocalServers_Click  ' all the click event to try and
> highlight -- but it doesn't do it
>      Exit For
>   End If
>   ' not found
>  cboLocalServers.ListIndex = -1
> Next
>
>
>
Author
10 May 2005 9:24 PM
Jeff Johnson [MVP: VB]
"dm4714" <spam@spam.net> wrote in message
news:ObY$tMaVFHA.1152@tk2msftngp13.phx.gbl...

> My code works and selects the correct server, however it doesn't
> highlight. What am I doing wrong?

Can you define "highlight"? What style is your combo box?
Author
10 May 2005 9:26 PM
Ken Halter
Show quote
"dm4714" <spam@spam.net> wrote in message
news:ObY$tMaVFHA.1152@tk2msftngp13.phx.gbl...
> Hello --
>
> I'm populating a combo box from a database with a list of servername.  If
> the a servername that is within a combo box matches the current machine
> name, I want to select the servername from the combo and have it
> highlighted.
>
> My code works and selects the correct server, however it doesn't
> highlight. What am I doing wrong?
>
> Here is code that loops thru control:
>
> For i = 0 to cboLocalServers.ListCount -1
>   If cboLocalServers(i) = strComputerName Then
>      cboLocalServers.ListIndex = i
>      Call cboLocalServers_Click  ' all the click event to try and
> highlight -- but it doesn't do it
>      Exit For
>   End If
>   ' not found
>  cboLocalServers.ListIndex = -1
> Next

Setting ListIndex is all it should take... if you start a new project, drop
a listbox and command button on the form and run this, it *should* scroll
down to and highlight item #25 in the list... btw.. "Calling" an event
doesn't do anything to a control. All it does is run the code that's in the
event handler.
'==========
Option Explicit

Private Sub Command1_Click()
   List1.ListIndex = 24 'the 25th item
End Sub

Private Sub Form_Load()
   Dim i As Integer
   For i = 1 To 50
      List1.AddItem "Item " & i
   Next
End Sub
'==========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
10 May 2005 10:11 PM
Ken Halter
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message

>   List1.ListIndex = 24 'the 25th item

hmmm... how'd I miss "Combo" instead of "List"... oh well. Time to go home.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
10 May 2005 10:11 PM
Ken Halter
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Show quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OQ$YubaVFHA.3540@TK2MSFTNGP15.phx.gbl...
> "dm4714" <spam@spam.net> wrote in message
> news:ObY$tMaVFHA.1152@tk2msftngp13.phx.gbl...
>> Hello --
>>
>> I'm populating a combo box from a database with a list of servername.  If
>> the a servername that is within a combo box matches the current machine
>> name, I want to select the servername from the combo and have it
>> highlighted.
>>
>> My code works and selects the correct server, however it doesn't
>> highlight. What am I doing wrong?
>>
>> Here is code that loops thru control:
>>
>> For i = 0 to cboLocalServers.ListCount -1
>>   If cboLocalServers(i) = strComputerName Then
>>      cboLocalServers.ListIndex = i
>>      Call cboLocalServers_Click  ' all the click event to try and
>> highlight -- but it doesn't do it
>>      Exit For
>>   End If
>>   ' not found
>>  cboLocalServers.ListIndex = -1
>> Next
>
> Setting ListIndex is all it should take... if you start a new project,
> drop a listbox and command button on the form and run this, it *should*
> scroll down to and highlight item #25 in the list... btw.. "Calling" an
> event doesn't do anything to a control. All it does is run the code that's
> in the event handler.
> '==========
> Option Explicit
>
> Private Sub Command1_Click()
>   List1.ListIndex = 24 'the 25th item
> End Sub
>
> Private Sub Form_Load()
>   Dim i As Integer
>   For i = 1 To 50
>      List1.AddItem "Item " & i
>   Next
> End Sub
> '==========
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Sign up now to help keep VB support alive - http://classicvb.org/petition
> Please keep all discussions in the groups..
>
Author
10 May 2005 10:23 PM
Ken Halter
Oh brother... battin 1000 here. See y'all tomorrow <g> I'm outta here!

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
10 May 2005 11:08 PM
MikeD
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:O6GWo7aVFHA.3808@TK2MSFTNGP14.phx.gbl...
> Oh brother... battin 1000 here. See y'all tomorrow <g> I'm outta here!

Would't that be batting 0.000? <g> I agree that you need to get some rest,
Ken. You're working too hard! <g>

--
Mike
Microsoft MVP Visual Basic
Author
11 May 2005 3:09 AM
dm4714
Thanks everyone --

The only other problem I'm having is that I want to make a command button
enabled when a particular combobox item is selected.  The correct listitem
appears on the form when it is loaded, but the command button is not
enabled.  If I simply select the listitem again from the combo, the button
enables.  It is like the OnChange event (where I enabled the button) isn't
being triggered.

How do I force the OnChange to run when I set the listindex manually?  I was
under the impression that setting a listitem would cause the OnChange
handler to fire???
Author
11 May 2005 6:40 AM
Michael Cole
dm4714 wrote:
> Thanks everyone --
>
> The only other problem I'm having is that I want to make a command
> button enabled when a particular combobox item is selected.  The
> correct listitem appears on the form when it is loaded, but the
> command button is not enabled.  If I simply select the listitem again
> from the combo, the button enables.  It is like the OnChange event
> (where I enabled the button) isn't being triggered.
>
> How do I force the OnChange to run when I set the listindex manually?
> I was under the impression that setting a listitem would cause the
> OnChange handler to fire???

Try the Click event

--
Regards,

Michael Cole
Author
11 May 2005 12:05 PM
MikeD
"dm4714" <spam@spam.net> wrote in message
news:OXYrrbdVFHA.1376@TK2MSFTNGP10.phx.gbl...
> The only other problem I'm having is that I want to make a command button
> enabled when a particular combobox item is selected.  The correct listitem
> appears on the form when it is loaded, but the command button is not
> enabled.  If I simply select the listitem again from the combo, the button
> enables.  It is like the OnChange event (where I enabled the button) isn't
> being triggered.
>
> How do I force the OnChange to run when I set the listindex manually?  I
> was under the impression that setting a listitem would cause the OnChange
> handler to fire???


OnChange event? VB's combobox doesn't have such an event.  Are you using the
combobox from FM20.DLL by any chance? If so, drop it and switch to VB's.
FM20.DLL is not intended for use in VB, it has many known problems if it is
used in VB, and it's not redistributable.

--
Mike
Microsoft MVP Visual Basic

AddThis Social Bookmark Button