Listbox varios colores

hola amigos.

Se que debe ser algo que se me pasa, pero estoy intentando colorear un listbox dependiendo de si lo introducido es un fichero o una carpeta.

mi cdigo es el siguiente
metodo llenalb(dire as string, lb as listbox)
Dim i as Integer
Dim fold as Folderitem

fold = new FolderItem( dire, FolderItem.PathTypeNative )

if fold<>nil then
lb.DeleteAllRows
for i=1 to fold.count
if left(fold.item(i).name,1)<>"." then
colorete=rgb(255,0,0)
if fold.item(i).Directory=true then

      colorete=rgb(200,200,200)
    else
      colorete=rgb(0,0,0)
    end if
    
    lb.AddRow fold.item(i).name
   
  end if
  
next
folderstr=fold.ShellPath

end if

envento celltextpaint
g.ForeColor=colorete

el caso es que siempre tengo el listbox con color grisceo claro.

que es lo que se me pasa

debera de tener los directorios en color gris claro, y los ficheros en negro.

una ayuda???

Gracias

Hola,

El problema del color me imagino que ser que el evento celltextpaint no se dispara cada vez que aades una linea al listbox.

Adems, el forecolor en el celltextpaint se refiere al color del texto, no al fondo (creo, no estoy en mi ordenador ahora para comprobarlo), que entiendo que es lo que quieres cambiar. Para el fondo tienes que usar el evento cellpaint/cellbackgriundpaint/… o como se llame, si no me equivoco.

Julen

Intenta eso:

[code]Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) As Boolean
If row Mod 2=0 And Not Me.selected(row) Then
g.foreColor=&cECF3FE
g.fillrect 0,0,g.width,g.height

If column = 6 Then
  g.foreColor=&cFFFF0000
  g.fillrect 0,0,g.width,g.height
End If

If column = 7 Then
  g.foreColor=&cE6E6E600
  g.fillrect 0,0,g.width,g.height
End If

Elseif row Mod 2<>0 And Not Me.selected(row) Then
g.foreColor=&cFFFFFF
g.fillrect 0,0,g.width,g.height

If column = 6 Then
  g.foreColor=&cFFFF0000
  g.fillrect 0,0,g.width,g.height
End If

If column = 7 Then
  g.foreColor=&cE6E6E600
  g.fillrect 0,0,g.width,g.height
End If

Elseif Me.selected(row) Then

If column<6 Then
  ' im.gradient(g,Me)
  g.foreColor=&cFFFFFF00
  g.fillrect 0,0,g.width,g.height
  
  g.foreColor=&c00000000
  g.DrawLine 0,0,g.width,0
  g.DrawLine 0,g.Height-1,g.width,g.Height-1
Else
  
  If column = 6 Then
    g.foreColor=&cFFFF0000
    g.fillrect 0,0,g.width,g.height
  End If
  
  If column=7 Then
    g.foreColor=&cE6E6E600
    g.fillrect 0,0,g.width,g.height
  End If
  
End If

End If

Return True
End Function
[/code]

El cdigo de Oliver es un ejemplo de lo que puedes hacer.

Ahora tienes que pensar en la manera de conseguir que cada vez que se redibuje el listbox los colores adecuados de cada fila se vuelvan a dibujar. Podras guardar el color de cada fila en su propiedad rowtag cuando aades elementos al listbox y luego leer una a una esas rowtags y colorear adecuadamente la fila correspondiente en cellbackgroundpaint.

Julen

Julen[/quote]

[quote=84160:@Julen Ibarretxe Uriguen]El código de Oliver es un ejemplo de lo que puedes hacer.

Ahora tienes que pensar en la manera de conseguir que cada vez que se redibuje el listbox los colores adecuados de cada fila se vuelvan a dibujar. Podrías guardar el color de cada fila en su propiedad rowtag cuando añades elementos al listbox y luego leer una a una esas rowtags y colorear adecuadamente la fila correspondiente en cellbackgroundpaint.

Julen[/quote]

Muy buena idea.

En efecto, el problema es que no sabia que celltextpaint no se aplicaba cada vez que se añade un item.

intentare lo que me dices de rowtags…

os mantendre informados.

Pues no me funciona.

Necesito colorear solo el texto, no el fondo.

El texto debera de cambiar, ya sea el item, un fichero, o una carpeta.

El caso es que le aado este dato a otra columna del listbox pero no consigo que me los coloree correctamente.

For the benefit of all others on this forum… Please post in English… or in the appropriate langugage sub-topic area.

It’s more than cleat that Gabriel doesn’t speak English. Not everybody magically learns English!

Yes, this should be posted in the Spanish Forum.

By the way, we are much more permissive when we see someone writing in English in the Spanish forum, and we don’t force a user to write in Spanish!

sorry for posting in the wrong place.

Spanish users are very few in comparison with the international community.

but if I only posting there, even a question that might interest more people, so I will.

Sorry once again.

lo siento por postear en lugar equivocado.

los usuarios en espaol somos muy pocos en comparacion con la comunidad internacional.

pero si unicamente debo de postear alli, aunque sea una pregunta que pueda interesar a mas gente, asi lo hare.

Lo siento una vez mas.

[quote=84159:@Oliver Osswald]Intenta eso:

[code]Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) As Boolean
If row Mod 2=0 And Not Me.selected(row) Then
g.foreColor=&cECF3FE
g.fillrect 0,0,g.width,g.height

If column = 6 Then
  g.foreColor=&cFFFF0000
  g.fillrect 0,0,g.width,g.height
End If

If column = 7 Then
  g.foreColor=&cE6E6E600
  g.fillrect 0,0,g.width,g.height
End If

Elseif row Mod 2<>0 And Not Me.selected(row) Then
g.foreColor=&cFFFFFF
g.fillrect 0,0,g.width,g.height

If column = 6 Then
  g.foreColor=&cFFFF0000
  g.fillrect 0,0,g.width,g.height
End If

If column = 7 Then
  g.foreColor=&cE6E6E600
  g.fillrect 0,0,g.width,g.height
End If

Elseif Me.selected(row) Then

If column<6 Then
  ' im.gradient(g,Me)
  g.foreColor=&cFFFFFF00
  g.fillrect 0,0,g.width,g.height
  
  g.foreColor=&c00000000
  g.DrawLine 0,0,g.width,0
  g.DrawLine 0,g.Height-1,g.width,g.Height-1
Else
  
  If column = 6 Then
    g.foreColor=&cFFFF0000
    g.fillrect 0,0,g.width,g.height
  End If
  
  If column=7 Then
    g.foreColor=&cE6E6E600
    g.fillrect 0,0,g.width,g.height
  End If
  
End If

End If

Return True
End Function
[/code][/quote]

Gracias por tu ejemplo, pero solo necesito cambiar el color del texto.

[quote=84209:@Gabriel Abellan]sorry for posting in the wrong place.

Spanish users are very few in comparison with the international community.

but if I only posting there, even a question that might interest more people, so I will.

Sorry once again.

lo siento por postear en lugar equivocado.

los usuarios en español somos muy pocos en comparacion con la comunidad internacional.

pero si unicamente debo de postear alli, aunque sea una pregunta que pueda interesar a mas gente, asi lo hare.

Lo siento una vez mas.[/quote]

While don’t you just create a new Topic in the Spanish subforum?

If you speak English, then please follow the Forum rules. I now agree with Dave!

Ya esta cambiado al foro en Espaol.

Pero de todas formas, te conviene escribir en el foro en ingls porque vas a recibir muchas ms respuestas.

Si slo quieres cambiar el color del texto, utiliza el rowtag con el color en el celltextpaint.

Tambin nos puedes mostrar el cdigo que tengas ahora para que intentemos encontrar el problema.

Julen

[quote=84220:@Julen Ibarretxe Uriguen]Pero de todas formas, te conviene escribir en el foro en inglés porque vas a recibir muchas más respuestas.

Si sólo quieres cambiar el color del texto, utiliza el rowtag con el color en el celltextpaint.

También nos puedes mostrar el código que tengas ahora para que intentemos encontrar el problema.

Julen[/quote]

El codigo es el siguiente :

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean

if me.rowtag(row) then

g.forecolor=rgb(125,125,125)

else
g.forecolor=rgb(0,0,0)
end if

End Function

Sub llenalb(dire as string, lb as listbox)
Dim i as Integer
Dim fold as Folderitem

fold = new FolderItem( dire, FolderItem.PathTypeNative )

if fold<>nil then
lb.DeleteAllRows
for i=1 to fold.count
if left(fold.item(i).name,1)<>"." then

    lb.AddRow fold.item(i).name,str(fold.Item(i).Directory)
    
    if fold.item(i).Directory=true  then 
      tipo=1
    else
      tipo=0
    end if
    lb.RowTag(lb.listindex+1)=tipo
  end if
  
next
folderstr=fold.ShellPath

end if
End Sub

con este código, tengo el primer item de color gris, que es un directorio (estaría correcto)
pero el segundo item que es otro directorio, ya esta en negro.

Esta lnea, compila sin dar errores?: if me.rowtag(row) then

Es posible que s, ya que el rowtag es variant (y por lo tanto puede ser boolean tambin), pero en todo caso la informacin que ests guardando es un integer, con lo que en la ejecucin me imagino que no consigues lo deseado en esa lnea (si es que no da errores).

Prueba: if me.rowtag(row)=1 then

No veo la declaracin de la variable “Tipo” por ninguna parte.

Julen

La variable tipo la he utilizado momentneamente.
el caso es que el primer item si lo tengo en gris, lo dems todo negro. (el texto se refiere)
he probado if me.rowtag(row)=1 then , con el mismo resultado.

Hola Gabriel,

He hecho un pequeo proyecto que muestra cmo colorear el texto usando rowtag. En una ventana he puesto un listbox y slo le he aadido el siguiente cdigo:

Sub Open() dim i as Integer for i=0 to 10 me.AddRow(str(i)) if i mod 2 = 0 then me.RowTag(i)=true else me.RowTag(i)=False end if End Sub
y

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean if me.rowtag(row) then g.ForeColor=&c000000 else g.ForeColor=&cFFFF00 end If End Function

Espero que con eso te sirva para hacer lo que buscas.

Julen

Muchas gracias, un ejemplo sencillo pero muy util.