Jump to content

[SOLVED] Write to listbox in different colors


Recommended Posts

Hi all,

I use this List as a Status window:

$logview = GUICtrlCreateList("", 8, 344, 449, 273,  BitOR($LBS_SORT, $LBS_NOSEL, $LBS_STANDARD, $WS_VSCROLL, $WS_BORDER))

Func _Status($i,$sTemp)
    _GUICtrlListBox_AddString($logview, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & StringStripCR($sTemp))
        if $i = "0" then
           GUICtrlSetColor(-1, 0xff0000) 
        else
           GUICtrlSetColor(-1, 0xffeeee)
        endif 

    _GUICtrlListBox_SetTopIndex($logview, _GUICtrlListBox_GetCount($logview) - 1)
EndFunc

and when I need to write something inside, i call it in this way:

_status(0,"Mesage written to list box)
        _status(1,"Mesage written to list box)

But in the listbox I can't see the lines in different colors, I always get the 1st one.

How is possible to write them in different colors (i.e. black for normal messages, red for error messages,...)

Thanks,

M.

Edited by marko001
Link to comment
Share on other sites

  • Moderators

marko001,

ListBox controls can only have a single text colour - if you want more you need a RichText control like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiRichEdit.au3>

$hGui = GUICreate("RichEdit Test", 320, 350)

$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

GUISetState()

Sleep(1000)

; Increase by 12 pts, set to "bold" and colour red
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am the BIG Heading!" & @CRLF, +12, "+bo", 0xFF0000)

Sleep(2000)

; Decrease by 6 pts, take away "bold" and colour Green
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am the smaller Subheading!" & @CRLF, -6, "-bo", 0x00FF00)

Sleep(2000)

; Reduce by the other 6 pts, leave the attibutes alone and colour black
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am normal text!" & @CRLF, -6, "", 0x000000)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSwitch

WEnd

Func _GUICtrlRichEdit_WriteLine($hWnd, $sText, $iIncrement = 0, $sAttrib = "", $iColor = -1)

    ; Count the @CRLFs
    StringReplace(_GUICtrlRichEdit_GetText($hWnd, True), @CRLF, "")
    Local $iLines = @extended
    ; Adjust the text char count to account for the @CRLFs
    Local $iEndPoint = _GUICtrlRichEdit_GetTextLength($hWnd, True, True) - $iLines
    ; Add new text
    _GUICtrlRichEdit_AppendText($hWnd, $sText & @CRLF)
    ; Select text between old and new end points
    _GuiCtrlRichEdit_SetSel($hWnd, $iEndPoint, -1)
    ; Convert colour from RGB to BGR
    $iColor = Hex($iColor, 6)
    $iColor = '0x' & StringMid($iColor, 5, 2) & StringMid($iColor, 3, 2) & StringMid($iColor, 1, 2)
    ; Set colour
    If $iColor <> -1 Then _GuiCtrlRichEdit_SetCharColor($hWnd, $iColor)
    ; Set size
    If $iIncrement <> 0 Then _GUICtrlRichEdit_ChangeFontSize($hWnd, $iIncrement)
    ; Set weight
    If $sAttrib <> "" Then _GUICtrlRichEdit_SetCharAttributes($hWnd, $sAttrib)
    ; Clear selection
    _GUICtrlRichEdit_Deselect($hWnd)

EndFunc

Or you can use a series of labels like this (you will need the GuiScrollBars_Ex UDF in my sig):

#include <GUIConstantsEx.au3>

#Include <GuiScrollBars_Ex.au3>

Global $iItems = 50        ; Number of items in list
Global $iDisplayItems = 20 ; Number of items in GUI
Global $aLabelID[$iItems]  ; ControlIDs of labels
Global $aData[$iItems][2]  ; Data and colour state of labels

; Fill data array
For $i = 0 To UBound($aData) - 1
    $aData[$i][0] = $i & " - " & $i & " - " & $i & " - " & $i ; Data
    $aData[$i][1] = 0                                         ; State
Next

; Create GUI
$hGui = GUICreate("Scroll Colour Line List", 420, 20 + $iDisplayItems * 20)

; Create "list"
For $i = 0 To $iItems - 1
    $aLabelID[$i] = GUICtrlCreateLabel($aData[$i][0], 10, 10 + ($i * 20), 380, 20)
Next

GUISetState()

; Generate scrollbars
_GUIScrollbars_Generate($hGui, 0, 4 + ($iItems * 20)) ; Just this one line is all you need to get scrollbars !!!!!!!!!!!!!!!!!!!!!!!!!!

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        ; Click on label so change colour
        Case $aLabelID[0] To $aLabelID[19]
            $iIndex = $iMsg - $aLabelID[0]
            ; Toggle colour data
            $aData[$iIndex][1] = Not $aData[$iIndex][1]
            ; Change line colour
            Switch $aData[$iIndex][1]
                Case 0
                    GUICtrlSetColor($aLabelID[$iIndex], 0x000000)
                Case 1
                    GUICtrlSetColor($aLabelID[$iIndex], 0xFF0000)
            EndSwitch
    EndSwitch

WEnd

Any help? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Func _Status($i,$sTemp)
    if $i = 0 Then _GUICtrlRichEdit_WriteLine($logview, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $sTemp, 0, "", 0x000000)
    if $i = 1 Then _GUICtrlRichEdit_WriteLine($logview, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $sTemp, 0, "", 0xFF0000)
    if $i = 2 Then _GUICtrlRichEdit_WriteLine($logview, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $sTemp, 0, "", 0x0000ff)
EndFunc   ;==>_Status

and works fine!

Just let me know if there is a way to unable typing inside (@gui_disable doesn't work)

M.

[Edit] .. SOLVED! $ES_READONLY ..

Thanks again for your help!

Marco

Edited by marko001
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...