• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Removing highlight color of Items in listbox and how to add mousover color on items text.

I have a LisBox in visual Basic. I would like to know how to remove that highlight when a I click on the items in the listbox. That highlight is a royal blue color sort of.
Is it possible? Is there a code or API function to remove it?
Thanks.
By removing that highlight, Is there a way I can make mouseover color over the items text in the listbox? and how is that possible?
0
Zeyadss
Asked:
Zeyadss
  • 6
  • 4
  • 4
1 Solution
 
ZeyadssAuthor Commented:
By removing that highlight, Is there a way I can make mouseover color over the items text in the listbox? and how is that possible?
0
 
jpaulinoCommented:
If it is in VB.NET you can change the selectionmode = None
0
 
ZeyadssAuthor Commented:
Is it possible in Visual Basic 6.0?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jpaulinoCommented:
I have to check for a soluction (if it is possible!) in my other computer. It will take some minutes.
0
 
jpaulinoCommented:
After some test a few research I didnt found anything!

The color is a system color and with the different windows color scene it changes.

But why do you want to do that ?
0
 
ZeyadssAuthor Commented:
The reason I am wanting to do this is because I want the listbox to match the look of my program. Unless there are other controls where I can add text and fit that controls colors to match my program.
0
 
ArkCommented:
Replace ListBox with ListView (view=report, HoverSelection=true)

Private Sub Form_Load()
   With ListView1
      .View = lvwReport
      .ColumnHeaders.Add , , , .Width
      .HideColumnHeaders = True
      For i = 1 To 30
         .ListItems.Add , , "lvItem_" & i
      Next i
      .HotTracking = True
      .HoverSelection = True
      .MultiSelect = False
   End With
End Sub
0
 
ZeyadssAuthor Commented:
I have tried ListView before, but when you click on the items in the listview, it still shows the high-lighted royal blue color over the items when they are clicked. Is there not a way to change this system color? or is it rather impossible?
0
 
jpaulinoCommented:
Nop,

Only with custom control like this http://www.vbaccelerator.com/home/VB/Code/Controls/index.asp
0
 
ArkCommented:
Sorry, probably I misunderstood you - you want REMOVE highlight selection or CHANGE highlight selection color?
Try TreeView instead

Private Sub Form_Load()
   With TreeView1
      For i = 1 To 30
         .Nodes.Add , , , "tvItem_" & i
      Next i
      .HotTracking = True
      .HideSelection = True
      .FullRowSelect = True
   End With
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
   Static nd As Node
   If Not nd Is Nothing Then
      nd.BackColor = vbWindowBackground
   End If
   Set nd = Node
   nd.BackColor = vbYellow
   Command1.SetFocus
End Sub
0
 
ArkCommented:
Private Sub Form_Load()
   With TreeView1
      For i = 1 To 30
         .Nodes.Add , , , "tvItem_" & i
      Next i
      .HideSelection = True
      .FullRowSelect = True
   End With
End Sub

Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
   Static ndPrevious As Node
   Dim nd As Node
   Set nd = TreeView1.HitTest(120, y)
   If Not nd Is Nothing Then
      If Not ndPrevious Is Nothing Then
         ndPrevious.BackColor = vbWindowBackground
      End If
      nd.BackColor = vbYellow
      Set ndPrevious = nd
   End If
End Sub

Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
   Dim nd As Node
   Set nd = TreeView1.HitTest(120, y)
   If Not nd Is Nothing Then
      nd.BackColor = vbYellow
   End If
   Command1.SetFocus
End Sub
0
 
ArkCommented:
And, finally, you can use WebBrowser control to customize 'listbox' as you want:
'Add webbrowser control (Microsoft Internet controls) on form
Option Explicit

Private Sub Form_Load()
   InitWBList WebBrowser1, "Navy", "AliceBlue", "Silver", True, "default"
   Dim i As Long
   For i = 1 To 100
       wbListAddItem WebBrowser1, "Sample item No." & i, "key_" & i
   Next i
End Sub

Private Sub InitWBList(wb As WebBrowser, _
                       Optional ByVal foreColor As String = "Black", _
                       Optional ByVal backColor As String = "White", _
                       Optional ByVal hoverColor As String = "Gold", _
                       Optional ByVal hoverUnderline As Boolean = False, _
                       Optional ByVal cursorType As String = "hand")
   
   Dim strNormal As String, strHover As String
   strNormal = "{text-decoration: none;"
   strNormal = strNormal & "color: " & foreColor & ";"
   strNormal = strNormal & "background-color: " & backColor & ";"
   strNormal = strNormal & "cursor: " & cursorType & "; width: 100%;}"
   
   strHover = "{text-decoration: " & IIf(hoverUnderline, "underline;", "none;")
   strHover = strHover & "color: " & foreColor & ";"
   strHover = strHover & "background-color: " & hoverColor & ";"
   strHover = strHover & "cursor: " & cursorType & "; width: 100%;}"
   
   wb.Navigate "about:<body><table></table></body>"
   While wb.ReadyState < 4
      DoEvents
   Wend
   
   Dim css
   Set css = wb.Document.createStyleSheet
   css.addRule ".listItem a:link ", strNormal
   css.addRule ".listItem a:visited ", strNormal
   css.addRule ".listItem a:hover ", strHover
   
   wb.Document.body.Style.margin = 0
   wb.Document.getElementsByTagName("TABLE")(0).Width = "100%"
   wb.Document.getElementsByTagName("TABLE")(0).cellPadding = 0
   wb.Document.getElementsByTagName("TABLE")(0).cellSpacing = 0
   wb.Document.getElementsByTagName("TABLE")(0).tBodies(0).id = "listTable"
End Sub

Private Sub wbListAddItem(wb As WebBrowser, ByVal strText As String, ByVal strKey As String)
   Dim doc, tbody, row
   Dim cell As MSHTML.HTMLTableCell
   Set tbody = wb.Document.getElementById("listTable")
   Set doc = tbody.Document
   Set row = tbody.insertRow
   tbody.appendChild row
   row.className = "listItem"
   Set cell = doc.createElement("TD")
   row.appendChild cell
   cell.innerHTML = "<a href=""idx=" & strKey & """>" & strText & "</a>"
   cell.id = strKey
End Sub

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
   Dim arrURL() As String
   Dim id As String
   If Flags > 0 Then
      Cancel = True
      arrURL = Split(URL, "=")
      id = arrURL(UBound(arrURL))
      MsgBox "You click on " & WebBrowser1.Document.getElementById(id).innerText
   End If
End Sub

0
 
ArkCommented:
Oops, change
Dim cell As MSHTML.HTMLTableCell
to
Dim cell
to avoid addin MSHTML reference
0
 
ArkCommented:
Thanks for points, glad I could help you, but IMHO in your case WebBrowser approach is much more flexible - you can make really customized 'ListBox', including background/foreground images, different fonts, back/fore colors, showing grid etc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now