Two text colours in one ListBox cell?

I’ve no problem with colouring the text in individual cells with CellTextPaint, but I need to able to do something along the lines of

“Hello World” with “Hello” as black and “World” as red all in the one cell.

(I can do the string stuff to split them btw - it’s just how to get the two colours that I’m stuck on)

I’m sure this must have been mentioned in another thread but I can’t find it.

g.forecolor=&cff0000
g.drawstring "TE",0,g.textascent
g.forecolor=&c0000ff
g.drawstring "ST",g.stringwidth("TE"),g.textascent

just to illustrate the “how”… up to you to make it pretty :slight_smile:

fyi… prints “TEST” with first two in RED, last two in BLUE

Thanks very much for the pointer Dave. I’ll have a play with that.

Well I’ve got this very close to what I want but have reached another impasse.

To explain the code below the text in column 1 (ie. the 2nd column) has a name with an ‘account type’ in brackets afterwards. So it might be “Richard Brown (Newbie)” or “Dave S (Genius)” or “Richard Summers (Crackpot)” :wink: - that kind of thing - and I want the bracketed text in another colour.

So… so far in the CellTextPaint event of my ListBox I have:

  IF column = 1 then
    
    dim i as integer
    dim n as integer
    dim celltext as string
    dim startstring as string
    dim endstring as string
    
    for i=0 to me.ListCount-1
      
      celltext=me.Cell(i,1)
      
      n = InStr(celltext, "(")
      
      startstring = Left(celltext,n-1)
      
      endstring = Right(celltext, Len(celltext)-n + 1)
      
      g.forecolor=&c000000
      g.drawstring (startstring,0,g.textascent)
      g.forecolor=&c888888
      g.drawstring (endstring,g.stringwidth(startstring),g.textascent)
      
      Return True
      
    next i
    
  end if

Which works beautifully except it puts the value from the first row into every row!

Any ideas where I’m going wrong?

[quote=159758:@Richard Brown]…

for i=0 to me.ListCount-1 celltext=me.Cell(i,1)[/quote]

CellTextPaint is called individually for each cell but that loop starts out processing row 0 each time, then hits the return true before any more. Instead remove the loop and use the Row parameter to get the current cells text…

celltext=me.Cell(Row, 1)
IF column = 1 then
   
    dim n as integer
    dim celltext as string
    dim startstring as string
    dim endstring as string
      celltext=me.Cell(row,column) // change the "1", just for portability sake :)
      
      n = InStr(celltext, "(")
      
      startstring = Left(celltext,n-1)
      
      endstring = Right(celltext, Len(celltext)-n + 1)
      
      g.forecolor=&c000000
      g.drawstring (startstring,0,g.textascent)
      g.forecolor=&c888888
      g.drawstring (endstring,g.stringwidth(startstring),g.textascent)
      
      Return True
  
  end if

also… the code ASSUMES there will always be a “(”…

Thanks very much for your help Dave.

I get it now and have it working as I need.