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

Changing font color in dropbox

Status
Not open for further replies.

talhamoin

IS-IT--Management
Mar 3, 2011
14
AE
I have a drop box with option of colors:

Code:
cboColorList.AddItem "Aqua"
cboColorList.AddItem "Blue"
cboColorList.AddItem "Bright Green"
cboColorList.AddItem "Dark Green"
cboColorList.AddItem "Indigo"
cboColorList.AddItem "Lavender"
cboColorList.AddItem "Orange"
cboColorList.AddItem "Red"

I want that every option should be in the color it describes. i.e. Aqua should appear in aqua color. Blue in blue color and so on.

Is it possible?
 
Can you use a list view?
IMG
 
Yes. That can also be used. How can I create this?
 
This code is an Access version pulling the text and the color codes from a database table. But you can either hard code the values or read the values from a range in Excel.

Private Sub Form_Load()
Dim lstItem As ListItem
Dim lvw As ListView
Dim rs As DAO.Recordset

'Access code to read from table
Set rs = CurrentDb.OpenRecordset("tblColors", dbReadOnly)
Set lvw = Me.lvwOne.Object
With lvw
'Set ListView style
.View = lvwReport
.GridLines = True
.FullRowSelect = True
.Appearance = cc3D
.TextBackground = lvwOpaque
'Clear Header and ListItems
.ListItems.Clear
.ColumnHeaders.Clear
End With

'Set up column headers
With lvw.ColumnHeaders
.Add , , "Color", 2000, lvwColumnLeft
End With

'You can just hard code the values instead of looping and reading from a 'database table
Do While Not rs.EOF
Set lstItem = lvw.ListItems.Add()
lstItem.Text = rs!Color
lstItem.ForeColor = rs!colorcode
rs.MoveNext
Loop
End Sub
 
Thanks MajP

I think using image control will be great. Do I need to add any reference for my VBA project to add this control; because I don't see this on the tool box.
 
The first example i showed used an "List View" it is the only control I know that allows you to use different fonts for each item in a list.

The Second example requires two controls: "Image Combo" a combo box with images. "ImageList" a hidden control that serves as a repository for images.

Here is the second example
Code:
Public imgLstObj As MSComctlLib.ImageList
Private Sub UserForm_Activate()
  Dim imgCmbo As ImageCombo
  Dim cmboItm As ComboItem
  Dim lstImg As ListImage
  Set imgCmbo = Me.imgCmboOne
  Call loadImgLst
  Set imgCmbo.ImageList = imgLstObj
  Set cmboItm = imgCmbo.ComboItems.Add()
  For Each lstImg In imgLstObj.ListImages
    Set cmboItm = imgCmbo.ComboItems.Add()
    With cmboItm
      .Text = lstImg.Key
      'This pulls the images out of the imagelist and puts them in the imagecombo
      .Image = lstImg.Index
    End With
  Next lstImg
End Sub

Public Sub loadImgLst()
  'This puts images into the image list
  Set imgLstObj = Me.imgLstOne
  imgLstObj.ListImages.Add 1, "Red", LoadPicture("C:\Red.jpg")
  imgLstObj.ListImages.Add 2, "Blue", LoadPicture("C:\Blue.jpg")
  '....
End Sub

You have to right click on your palette and choose "additional controls". The names all start with
Microsoft..
Once you click check them it will add the additional controls to your pallette. Normally you do not have to go to references. But sometimes you have to register the control.
 
Add checked controls are on the toolbox palette

IMG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top