PDA

View Full Version : ComboBox .ItemData(.NewIndex)



MacroShadow
06-11-2012, 02:31 AM
In VB the ItemData is used to get the data based on the
index. In VBA the ComboBox doesn't have the ItemData property. What needs to be done to use the following code in VBA?

With cboCharset
.AddItem "ANSI_CHARSET"
.ItemData(.NewIndex) = ANSI_CHARSET
.ListIndex = .NewIndex
.AddItem "DEFAULT_CHARSET"
.ItemData(.NewIndex) = DEFAULT_CHARSET
.AddItem "SYMBOL_CHARSET"
.ItemData(.NewIndex) = SYMBOL_CHARSET
.AddItem "SHIFTJIS_CHARSET"
.ItemData(.NewIndex) = SHIFTJIS_CHARSET
.AddItem "HANGEUL_CHARSET"
.ItemData(.NewIndex) = HANGEUL_CHARSET
.AddItem "HANGUL_CHARSET"
.ItemData(.NewIndex) = HANGUL_CHARSET
.AddItem "GB2312_CHARSET"
.ItemData(.NewIndex) = GB2312_CHARSET
.AddItem "CHINESEBIG5_CHARSET"
.ItemData(.NewIndex) = CHINESEBIG5_CHARSET
.AddItem "OEM_CHARSET"
.ItemData(.NewIndex) = OEM_CHARSET
.AddItem "JOHAB_CHARSET"
.ItemData(.NewIndex) = JOHAB_CHARSET
.AddItem "HEBREW_CHARSET"
.ItemData(.NewIndex) = HEBREW_CHARSET
.AddItem "ARABIC_CHARSET"
.ItemData(.NewIndex) = ARABIC_CHARSET
.AddItem "GREEK_CHARSET"
.ItemData(.NewIndex) = GREEK_CHARSET
.AddItem "TURKISH_CHARSET"
.ItemData(.NewIndex) = TURKISH_CHARSET
.AddItem "VIETNAMESE_CHARSET"
.ItemData(.NewIndex) = VIETNAMESE_CHARSET
.AddItem "THAI_CHARSET"
.ItemData(.NewIndex) = THAI_CHARSET
.AddItem "EASTEUROPE_CHARSET"
.ItemData(.NewIndex) = EASTEUROPE_CHARSET
.AddItem "RUSSIAN_CHARSET"
.ItemData(.NewIndex) = RUSSIAN_CHARSET
.AddItem "MAC_CHARSET"
.ItemData(.NewIndex) = MAC_CHARSET
.AddItem "BALTIC_CHARSET"
.ItemData(.NewIndex) = BALTIC_CHARSET
End With

gmaxey
06-11-2012, 04:09 AM
I'm not completely sure that I understand what your are after. Is it this:

Option Explicit
Private Sub UserForm_Initialize()
With cb1
.AddItem "ANSI_CHARSET"
.AddItem "DEFAULT_CHARSET"
.AddItem "SYMBOL_CHARSET"
.AddItem "SHIFTJIS_CHARSET"
.AddItem "HANGEUL_CHARSET"
.AddItem "HANGUL_CHARSET"
.AddItem "GB2312_CHARSET"
End With
End Sub

Private Sub cb1_Change()
MsgBox cb1.List(cb1.ListIndex)
End Sub

MacroShadow
06-11-2012, 04:26 AM
I'm trying to filter results in a listbox based on the value of the combobox.

I found a VB project that enumerates and displays installed fonts in a listbox, there is an option to filter based on charset, hidden and/or vertical fonts. I managed to adapt it to vba, but I got stuck on (probably the easiest part) filtering the listbox based on the charset selected in the combobox.

This is the code used to add fonts to the listbox:
Private Sub AddFontToList(ByVal FaceName As String, lst As Control, Optional ByVal CharSet As Long = -1)
Dim Skip As Boolean
' Add the font to the list, if it meets criteria
' held in module-level flags.
If FilterHidden Then
If FontHidden(FaceName) Then Skip = True
End If
If FilterVertical Then
If InStr(FaceName, "@") = 1 Then Skip = True
End If
If Not Skip Then
If CharSet >= 0 Then
FaceName = FaceName & " (" & CharSet & ")"
End If
lst.AddItem FaceName
End If
End Sub

Tinbendr
06-11-2012, 08:17 AM
What are you trying to accomplish?

Frosty
06-11-2012, 10:27 AM
MacroShadow,

When I am trying to "filter" a list box based on the values of a combo box, I find it useful to use arrays.

Without a better data set, I can only give you a general idea. But I would approach it thusly:
1. Build my array of all available fonts, something that looks like
myArray(0,0) = "Value1"
myArray(0,1) = "filter criteria A"
myArray(1,0) = "Value 2"
myArray(1,1) = "filter criteria A"
myArray(2,0) = "Value 3"
myArray(2,1) = "filter criteria B"

And then from there, I would construct multiple arrays, based on the filter criteria... so that you got
myArrayA(0) = "Value1"
myArrayA(1) = "Value2"

and
myArrayB(0) = "Value3"

And so on...

And then as you switch your combo box, simply set the .List property of the List control to whichever array is appropriate.

This will, I think, ultimately be easier to manage, rather than using the .Add and .Clear methods on the listbox, which in large lists becomes tricky to wade through.

I can give a more concrete example if you post more code... but otherwise I'm in the same boat as Greg and Tinbendr... it's tough to be specific. How do you get the "type" of font based on the Font Name?

MacroShadow
06-11-2012, 12:36 PM
Ok, attached is the document I'm dealing with.

MacroShadow
06-11-2012, 02:19 PM
The source of the original VB project is from: http://vb.mvps.org/samples/FontFilter/

Frosty
06-11-2012, 02:22 PM
Hmm... that user form is confusing to me. Why do I click checkboxes called "Filter" and then get *more* items in my returns?

I think what you're looking for is something along the lines of
1. change the commented out line in cmdEnum2_Click to...
lf.lfCharSet = cboCharset.List(cboCharset.ListIndex, 1)

2. In the UserForm_Initialize routine, use
.List(.ListCount - 1, 1) = ANSI_CHARSET
(or whatever is appropriate).

But that combo box only applies to the list box on the right...it doesn't appear to be in place as a filter for the items on the left. And you will need to error trap for a .ListIndex of -1.

I'd definitely clean up that user form to make it make a little more intuitive sense.

I forget-- you aren't getting help for homework assignments, are you? Because I'm not sure why you're reverse engineering a product to put in VBA when it already works elsewhere...

MacroShadow
06-11-2012, 10:55 PM
No, it's not for homework, I'm out of school for more than a decade.
I was trying to find a way to detect the language of a specific font in VBA, and was given the VB project by its author Karl E. Peterson, as a starting point.

Your suggestion worked.

And again you're right the combobox only filters the list box on the right.

Frosty
06-12-2012, 06:41 AM
You sure have interesting problems ;)