Home All Groups Group Topic Archive Search About
Author
26 Feb 2009 3:48 AM
Edward
What would be the best way to color different lines of a ListBox based on
values?

I've tried:
If RS!Process = "value1" Then
   lstUtility.ForeColor = vbBlue
    ElseIf  RS!Process = "value2" Then
   lstUtility.ForeColor = vbRed
    Else: lstUtility.ForeColor = vbCyan ' 15 other types
End If
lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success & Chr$(9) &
RS!LogDate
RS.MoveNext
Loop
RS.Close

But the List Box ends up with all lines the same color.

Author
26 Feb 2009 5:33 AM
mayayana
You don't seem to be using VB. ! is not a "legal"
character in a variable. If you're actually somehow
using VB then the answer is that you need to
subclass the listbox. If you're using VB.Net then you've
wandered into the wrong group. For VB.Net you want
a group with "dotnet" in the name. Try these:

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb


Show quoteHide quote
> What would be the best way to color different lines of a ListBox based on
> values?
>
> I've tried:
>  If RS!Process = "value1" Then
>    lstUtility.ForeColor = vbBlue
>     ElseIf  RS!Process = "value2" Then
>    lstUtility.ForeColor = vbRed
>     Else: lstUtility.ForeColor = vbCyan ' 15 other types
>  End If
> lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success & Chr$(9) &
> RS!LogDate
> RS.MoveNext
> Loop
> RS.Close
>
> But the List Box ends up with all lines the same color.
>
>
Author
26 Feb 2009 10:37 AM
Bill McCarthy
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:%230sUAQ9lJHA.2460@TK2MSFTNGP06.phx.gbl...
>
>   You don't seem to be using VB. ! is not a "legal"
> character in a variable.

Wrong.  The ! is an indexer identifier.
Author
26 Feb 2009 2:40 PM
Jeff Johnson
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:%230sUAQ9lJHA.2460@TK2MSFTNGP06.phx.gbl...

>   You don't seem to be using VB. ! is not a "legal"
> character in a variable.

Correct, but

>>  If RS!Process = "value1" Then

RS!Process is not a variable; it's the equivalent of RS("Process").Value.
Did your brain just misfire or are you actually not familiar with the
(ancient) bang syntax?
Author
26 Feb 2009 3:35 PM
mayayana
> RS!Process is not a variable; it's the equivalent of RS("Process").Value.
> Did your brain just misfire or are you actually not familiar with the
> (ancient) bang syntax?
>

   I guess that's from before my time. I've never
run across it. And I don't find it in either "VB and VBA
in a Nutshell" or in MSDN. Is it some kind of DOS-era
thing that's carried over? Is there a list of those
somewhere?
Author
26 Feb 2009 3:55 PM
Vinchenzo vinç
Show quote Hide quote
"mayayana" <mayayaX***@rcXXn.com> escribió en el mensaje de noticias
news:ux6FKgCmJHA.2064@TK2MSFTNGP05.phx.gbl...
>
>> RS!Process is not a variable; it's the equivalent of RS("Process").Value.
>> Did your brain just misfire or are you actually not familiar with the
>> (ancient) bang syntax?
>>
>
>   I guess that's from before my time. I've never
> run across it. And I don't find it in either "VB and VBA
> in a Nutshell" or in MSDN. Is it some kind of DOS-era
> thing that's carried over? Is there a list of those
> somewhere?
>

    http://support.microsoft.com/kb/129287/en-us



--
    Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Author
26 Feb 2009 4:30 PM
Jeff Johnson
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:ux6FKgCmJHA.2064@TK2MSFTNGP05.phx.gbl...

>> RS!Process is not a variable; it's the equivalent of RS("Process").Value.
>> Did your brain just misfire or are you actually not familiar with the
>> (ancient) bang syntax?
>>
>
>   I guess that's from before my time. I've never
> run across it.

You're not missing anything. I despise the syntax myself. I learned it in my
Access days.
Author
26 Feb 2009 3:12 PM
Vinchenzo vinç
"mayayana" <mayayaX***@rcXXn.com> escribió en el mensaje de noticias
news:%230sUAQ9lJHA.2460@TK2MSFTNGP06.phx.gbl...
>
>   You don't seem to be using VB. ! is not a "legal"
> character in a variable.

    Actually it is a valid syntax to access a Collection!Member by its key
name.

    For instance:

'******************
Dim col As Collection: Set col = New Collection

col.Add "One", "1"
col.Add 2, "Second"
col.Add Me, "oneForm"
col.Add col, "itself"

Debug.Print col![1]
Debug.Print col!OneForm.Controls!Text1.Text ' "Controls collection" as well
col.Remove col!Second ' Removes the index 2 (Variant/Integer member)
Debug.Print col!Itself.Count
'******************



--
    Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Author
26 Feb 2009 4:17 PM
mayayana
Thanks. I'd never heard of that. Nor have I ever seen
the square brackets. It's rather confusing.
All of these work:

Debug.Print col![1]
Debug.Print col.Item(1)
Debug.Print col.Item("1")

Yet this doesn't:

Debug.Print col!(1)


Show quoteHide quote
>     Actually it is a valid syntax to access a Collection!Member by its key
> name.
>
>     For instance:
>
> '******************
> Dim col As Collection: Set col = New Collection
>
> col.Add "One", "1"
> col.Add 2, "Second"
> col.Add Me, "oneForm"
> col.Add col, "itself"
>
> Debug.Print col![1]
> Debug.Print col!OneForm.Controls!Text1.Text ' "Controls collection" as
well
> col.Remove col!Second ' Removes the index 2 (Variant/Integer member)
> Debug.Print col!Itself.Count
> '******************
>
>
>
> --
>     Regards
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> ( ! )
http://groups.google.com/group/microsoft.public.vb.general.discussion
Show quoteHide quote
> ( i ) http://www.microsoft.com/communities/conduct/default.mspx
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>
Author
26 Feb 2009 5:23 PM
Jeff Johnson
Show quote Hide quote
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:ef2HD4CmJHA.504@TK2MSFTNGP06.phx.gbl...

> Thanks. I'd never heard of that. Nor have I ever seen
> the square brackets. It's rather confusing.
> All of these work:
>
> Debug.Print col![1]
> Debug.Print col.Item(1)
> Debug.Print col.Item("1")
>
> Yet this doesn't:
>
> Debug.Print col!(1)

The brackets are mainly for field names that have spaces, so you could do

col![Some Date]

and apparently they work as an indexer as well. I, too, never knew that.
Author
26 Feb 2009 5:40 PM
Bob Butler
Show quote Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message
news:Okf00bDmJHA.1288@TK2MSFTNGP02.phx.gbl...
> "mayayana" <mayayaX***@rcXXn.com> wrote in message
> news:ef2HD4CmJHA.504@TK2MSFTNGP06.phx.gbl...
>
>> Thanks. I'd never heard of that. Nor have I ever seen
>> the square brackets. It's rather confusing.
>> All of these work:
>>
>> Debug.Print col![1]
>> Debug.Print col.Item(1)
>> Debug.Print col.Item("1")
>>
>> Yet this doesn't:
>>
>> Debug.Print col!(1)
>
> The brackets are mainly for field names that have spaces, so you could do
>
> col![Some Date]
>
> and apparently they work as an indexer as well. I, too, never knew that.

The best use is to create hidden enum members...

public enum myvalues
  [_mintype]=1
  type1=1
  type2=2
  type3=3
  [_maxtype]=3
end enum

The underscore makes the enum hidden and the brackets make VB accept the
underscore.  Now you can code limit tests and just update the enum when
necessary:

if thevalue>=[_mintype] and thevalue<=[_maxtype] then
Author
26 Feb 2009 6:40 PM
Jeff Johnson
Show quote Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:ORSUPmDmJHA.1172@TK2MSFTNGP04.phx.gbl...

>>> Thanks. I'd never heard of that. Nor have I ever seen
>>> the square brackets. It's rather confusing.
>>> All of these work:
>>>
>>> Debug.Print col![1]
>>> Debug.Print col.Item(1)
>>> Debug.Print col.Item("1")
>>>
>>> Yet this doesn't:
>>>
>>> Debug.Print col!(1)
>>
>> The brackets are mainly for field names that have spaces, so you could do
>>
>> col![Some Date]
>>
>> and apparently they work as an indexer as well. I, too, never knew that.
>
> The best use is to create hidden enum members...

For reference, I was only addressing their use with the bang syntax.
Author
26 Feb 2009 7:07 PM
Ulrich Korndoerfer
Hi,

mayayana schrieb:

> Thanks. I'd never heard of that. Nor have I ever seen
> the square brackets. It's rather confusing.
> All of these work:
>
> Debug.Print col![1]
> Debug.Print col.Item(1)
> Debug.Print col.Item("1")
>

It worked because you had the luck that the entry of value "One" was
added as first entry (at index 1) into the collection.

Col.Item("1") and Col.Item[1] are equivalent and adress under all
circumstances the same item, Col.Item(1) is not equivalent because it
adresses the item having the numerical index 1.

Items in a collection are adressed either by a key string or by a
numerical index. Item is a property of the Collection class with one
variant parameter named "IndexOrKey" that adresses the item. If the
variant parameter's type is of type String, the parameters value is used
to adress by key, if it is a numerical type it is used to adress by index.

After

Set col = New Collection
col.Add "First", "2"
col.Add "Second", "1"
col.Add "Third", "KeyOfThird"

you have a collection with three items:

the item at index 1 has the value "First" and the key "2",
the item at index 2 has the value "Second" and the key "1",
the item at index 3 has the value "Third" and the key "KeyOfThird".

So col.Item("1") delivers "Second", col.item(1) delivers "First".
col.item("2") delivers "First", col.item(2) delivers "Second".
col.item("KeyOfThird") and col.item(3) both deliver "Third".

When using the shortcut col!<Key> for keys, wether you have to use [] or
not depends on the key characters. If the key has characters that are
illegal if the key string would be used as an identifier name (eg as
variable name), the [] have to be used.

So because 1 is not a legal variable name, one has to use col![1].
However KeyOfThird is a legal identifier name and col.KeyOfThird is ok.

Show quoteHide quote
> Yet this doesn't:
>
> Debug.Print col!(1)
>
>
>>     Actually it is a valid syntax to access a Collection!Member by its key
>> name.
>>
>>     For instance:
>>
>> '******************
>> Dim col As Collection: Set col = New Collection
>>
>> col.Add "One", "1"
>> col.Add 2, "Second"
>> col.Add Me, "oneForm"
>> col.Add col, "itself"
>>
>> Debug.Print col![1]
>> Debug.Print col!OneForm.Controls!Text1.Text ' "Controls collection" as
> well
>> col.Remove col!Second ' Removes the index 2 (Variant/Integer member)
>> Debug.Print col!Itself.Count
>> '******************
>>
>>
>>
>> --
>>     Regards
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> ( ! )
> http://groups.google.com/group/microsoft.public.vb.general.discussion
>> ( i ) http://www.microsoft.com/communities/conduct/default.mspx
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>>
>>
>
>

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.proSource.de/Downloads/
Author
26 Feb 2009 4:41 PM
Edward
Show quote Hide quote
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:%230sUAQ9lJHA.2460@TK2MSFTNGP06.phx.gbl...
>
>   You don't seem to be using VB. ! is not a "legal"
> character in a variable. If you're actually somehow
> using VB then the answer is that you need to
> subclass the listbox. If you're using VB.Net then you've
> wandered into the wrong group. For VB.Net you want
> a group with "dotnet" in the name. Try these:
>
> microsoft.public.dotnet.general
> microsoft.public.dotnet.languages.vb
>
>
>> What would be the best way to color different lines of a ListBox based on
>> values?
>>
>> I've tried:
>>  If RS!Process = "value1" Then
>>    lstUtility.ForeColor = vbBlue
>>     ElseIf  RS!Process = "value2" Then
>>    lstUtility.ForeColor = vbRed
>>     Else: lstUtility.ForeColor = vbCyan ' 15 other types
>>  End If
>> lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success & Chr$(9)
>> &
>> RS!LogDate
>> RS.MoveNext
>> Loop
>> RS.Close
>>
>> But the List Box ends up with all lines the same color.
>>
Mayaya

If I don't use the "!" symbol when referring to an item of a recordset, I
receive a "Method or data member not found" error in VB6.
Author
26 Feb 2009 5:37 PM
Bob Butler
"Edward" <nospam@mail.com> wrote in message
news:ECzpl.2441$l71.873@newsfe23.iad...
> If I don't use the "!" symbol when referring to an item of a recordset, I
> receive a "Method or data member not found" error in VB6.

Post the syntax you are trying...
Author
26 Feb 2009 6:58 PM
Henning
Show quote Hide quote
"Edward" <nospam@mail.com> skrev i meddelandet
news:ECzpl.2441$l71.873@newsfe23.iad...
>
> "mayayana" <mayayaX***@rcXXn.com> wrote in message
> news:%230sUAQ9lJHA.2460@TK2MSFTNGP06.phx.gbl...
>>
>>   You don't seem to be using VB. ! is not a "legal"
>> character in a variable. If you're actually somehow
>> using VB then the answer is that you need to
>> subclass the listbox. If you're using VB.Net then you've
>> wandered into the wrong group. For VB.Net you want
>> a group with "dotnet" in the name. Try these:
>>
>> microsoft.public.dotnet.general
>> microsoft.public.dotnet.languages.vb
>>
>>
>>> What would be the best way to color different lines of a ListBox based
>>> on
>>> values?
>>>
>>> I've tried:
>>>  If RS!Process = "value1" Then
>>>    lstUtility.ForeColor = vbBlue
>>>     ElseIf  RS!Process = "value2" Then
>>>    lstUtility.ForeColor = vbRed
>>>     Else: lstUtility.ForeColor = vbCyan ' 15 other types
>>>  End If
>>> lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success & Chr$(9)
>>> &
>>> RS!LogDate
>>> RS.MoveNext
>>> Loop
>>> RS.Close
>>>
>>> But the List Box ends up with all lines the same color.
>>>
> Mayaya
>
> If I don't use the "!" symbol when referring to an item of a recordset, I
> receive a "Method or data member not found" error in VB6.
>
>

Isn't that equal to If RS.Fields("Process").Value = "value1". It's just more
obscure what you mean using '!'.

/Henning
Author
26 Feb 2009 6:04 AM
Jason Keats
Edward wrote:
Show quoteHide quote
> What would be the best way to color different lines of a ListBox
> based on values?
>
> I've tried:
> If RS!Process = "value1" Then
>   lstUtility.ForeColor = vbBlue
>    ElseIf  RS!Process = "value2" Then
>   lstUtility.ForeColor = vbRed
>    Else: lstUtility.ForeColor = vbCyan ' 15 other types
> End If
> lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success &
> Chr$(9) & RS!LogDate
> RS.MoveNext
> Loop
> RS.Close
>
> But the List Box ends up with all lines the same color.

It's complicated.

Best to find an example of an "owner-drawn listbox" that someone's been kind
enough to provide.

Maybe you could get help modifying one of these examples:
http://www.thescarms.com/vbasic/OwnerDrawn.aspx
http://www.vbaccelerator.com/codelib/odcbolst/article.htm

HTH
Author
26 Feb 2009 3:36 PM
mayayana
For coloring a Listbox see here:

www.jsware.net/jsware/vbcode.php5#lbox

You can't do it without subclassing.
Author
26 Feb 2009 5:06 PM
Edward
"mayayana" <mayayaX***@rcXXn.com> wrote in message
news:uuRYIhCmJHA.3760@TK2MSFTNGP03.phx.gbl...
>  For coloring a Listbox see here:
>
> www.jsware.net/jsware/vbcode.php5#lbox
>
> You can't do it without subclassing.
>
Wonderful!

This example is exactly what I was searching for, different font colors for
different ListBox line items.
www.jsware.net/jsware/vbcode.php5#lbox

Thanks
Author
26 Feb 2009 4:05 PM
Vinchenzo vinç
Show quote Hide quote
"Edward" <nospam@mail.com> escribió en el mensaje de noticias
news:Whopl.16362$Si4.10962@newsfe22.iad...
> What would be the best way to color different lines of a ListBox based on
> values?
>
> I've tried:
> If RS!Process = "value1" Then
>   lstUtility.ForeColor = vbBlue
>    ElseIf  RS!Process = "value2" Then
>   lstUtility.ForeColor = vbRed
>    Else: lstUtility.ForeColor = vbCyan ' 15 other types
> End If
> lstUtility.AddItem RS!Process & Chr$(9) & "     " & RS!Success & Chr$(9) &
> RS!LogDate
> RS.MoveNext
> Loop
> RS.Close
>
> But the List Box ends up with all lines the same color.

    If you are using already a reference to MsComCtl.ocx in your project,
you can avoid the subclassing using a TreeView, for example:

'**************
Dim i As Long

With someTreeView
    .FullRowSelect = True
    .LabelEdit = tvwManual

    For i = 1 To 20
        With .Nodes.Add(, , , "TV Item " & CStr(i))
            .BackColor = QBColor(Int(Rnd * 8))
            .ForeColor = QBColor(Int(Rnd * 8))
        End With
    Next
End With
'**************


--
    Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Author
26 Feb 2009 4:31 PM
Jeff Johnson
"Vinchenzo vinç" <Vinç@newsgroup.nospam> wrote in message
news:OkB7VwCmJHA.5656@TK2MSFTNGP02.phx.gbl...
> "Edward" <nospam@mail.com> escribió en el mensaje de noticias
> news:Whopl.16362$Si4.10962@newsfe22.iad...

>> What would be the best way to color different lines of a ListBox based on
>> values?

>    If you are using already a reference to MsComCtl.ocx in your project,
> you can avoid the subclassing using a TreeView, for example:

List boxes and tree views are very different controls. I wouldn't
arbitrarily swap one for the other.