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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bold some text in a listbox but not all

Status
Not open for further replies.

MBorofsky

Programmer
Nov 24, 2004
47
US
I have a list box as follows (VBA/Access 2003)

Label Above: Items proceeded by * are labels and may not be selected.

The box contains something like:

*Category 1
Item 1
Item 2
*Category 2
Item 1
Item 2
*Category 3
*Subheading 1
Item 1
Item 2
Item 3
Item 4
*Category 4
Item 1
*Subheading 2
Item 2
Item 3
Item 4

.....

I manually made this using the .AddItem procedure. If the user selects a category or subheading (anything begining in *), a message box tells them they need to select something more specific and unselects the item. Only one selection is permitted at a time. I would like the make it so that the *'ed items are bold instead of being *'ed. It would be much more user-friendly. The items (anything without a * in front) would need to be normal font.

The rest is superflous (i think) but may help:

This is generated from a set of tables. The primary table lists all items. Each item has a category (which it will appear under). This is an ID to another table that has the category text. Some items have subheading's. If there is a subheading it displays it and increases the justifications (just by using spaces).
 
I don't have Access 2003 yet, but the Microsoft ListView activeX control is very flexible and it makes a great "standard" looking listbox with custom formatting capabilities. Heres some sample code for loading it with custom formatting. Notice that the "ItemClick" event delivers the ListItem so you don't even have to muck with trying to find its index or selected property:
Code:
Private Sub Form_Load()
 Dim lvw As ListView
 Dim li As ListItem
 
 Set lvw = Me.lvwFormatted.Object  [green]'<-- important[/green]
 
 With lvw
  .View = lvwReport
  
  .ColumnHeaders.Add 1, "colMain", "Items:", Me.lvwFormatted.Width * 0.95
  .ColumnHeaders(1).Alignment = lvwColumnLeft
  
  .ListItems.Add 1, "Cat1", "Category 1"
  .ListItems(1).Bold = True
  .ListItems(1).ForeColor = RGB(0, 0, 200)
  
  .ListItems.Add 2, "Item1.1", "  Item 1.1"
  .ListItems.Add 3, "Item1.2", "  Item 1.2"
  
    .ListItems.Add 4, "Cat2", "Category 2"
  .ListItems(4).Bold = True
  .ListItems(4).ForeColor = RGB(0, 0, 200)
  
  .ListItems.Add 5, "Item2.1", "  Item 2.1"
  .ListItems.Add 6, "Item2.2", "  Item 2.2"
  
    .ListItems.Add 7, "Cat3", "Category 3"
  .ListItems(7).Bold = True
  .ListItems(7).ForeColor = RGB(0, 0, 200)
  
  .ListItems.Add 8, "Item3.1", "  Item 1.1"
  .ListItems.Add 9, "Item3.2", "  Item 1.2"
  
 End With
 
 For Each li In lvw.ListItems
  Debug.Print li.Bold, li.ForeColor, li.Text
 Next
 
End Sub

Private Sub lvwFormatted_ItemClick(ByVal Item As Object)
  If Left(Item.Key, 3) = "Cat" Then
    MsgBox "Invalid Selection, please select a subcategory..."
    Item.Selected = False
  Else
    MsgBox "Executing code for " & Trim(Item.Text)
  End If
End Sub

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top