Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highliting a value in a listbox withou clicking on it.

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
I have a listbox on a form for the user to select a value, and of course the picked value remains hyghlited.
But when the program starts it starts with a default value of the listbox. Since the listbox value denoting the default value is not highlited, question: how can the default value be highlited in the listbox to show what value has been picked by default ?
Thank you for any suggestions.
 
Assuming that you know what the default value is, you could select it from the list like this:

Code:
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" _
     (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As _
     Integer, ByVal lParam As Any) As Long

Private Const LB_FINDSTRINGEXACT = &H1A2

  ' This is where you'd highlight the default value in the list:
  Default$ = "Item #6"
  ListBox1.ListIndex = SendMessage(ListBox1.hWnd, LB_FINDSTRINGEXACT, -1, Default$)

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Another way to do that:

Code:
Private Sub Command1_Click()
    
    ListView1.View = lvwReport
    ListView1.ListItems.Clear
    ListView1.HideSelection = False
    ListView1.FullRowSelect = True
    

    ListView1.ListItems.Add , , "First Row"
    ListView1.ListItems.Add , , "Second Row"
    ListView1.ListItems.Add , , "Third Row"
    
    ListView1.ListItems(1).ListSubItems.Add , , "First row sub item"
    ListView1.ListItems(2).ListSubItems.Add , , "Second row sub item"
    ListView1.ListItems(3).ListSubItems.Add , , "Third row sub item"

    For Each iItem In ListView1.ListItems
        If iItem.SubItems(1) = "Second row sub item" Then
            iItem.Selected = True
            iItem.EnsureVisible
            ListView1.SetFocus
            Exit For
        End If
    Next

    
End Sub
 
So another way to highlight an item in a ListBox is to use a ListView?
 
Why not just use [tt]ListIndex[/tt] of the listbox?

VB6, on the Form: a List1 (ListBox), Text1 (TextBox), and Command1 (CommandButton)

Code:
Option Explicit

Private Sub Command1_Click()

List1.ListIndex = Val(Text1.Text)

End Sub

Private Sub Form_Load()
Dim i As Integer

Command1.Caption = "Select"
Text1.Text = ""

With List1
    .Clear
    For i = 0 To 25
        .AddItem "Item " & i
    Next i
End With

End Sub

You get the list of Items in the list box from 'Item 0' to 'Item 25'
If you type 7 in a text box and click command button, 'Item 7' will be selected.

Have fun.

---- Andy
 
So another way to highlight an item in a ListBox is to use a ListView?"

Misread it...just trying to be helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top