KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Line colouring in ListView

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
Mart
KiXforms Regular
KiXforms Regular


Joined: 03 Oct 2005
Posts: 57
Location: Rotterdam - Netherlands

PostPosted: Sun Feb 03, 2008 11:14 pm    Post subject: Line colouring in ListView Reply with quote

Is there a nice way to do some line colouring in a ListView?
I want to give each second line a different colour to improve readability. I have a solution but imho it's kinda funky and quick and dirty and certainly not best practice Shocked . Does anyone know a way to do this cleaner then I'm doing it right now?

I'm keeping an index of each line added to the listview, divide it by two (does not return decimals), multiply it by two and compare it with the index. If they are different use one colour of not use an other colour. There must be a nicer and cleaner way of doing this imho.

Code snippet.
Code:

$halfline = $index / 2
$2xhalfline = $halfline * 2
If $2xhalfline <> $index
   $ListView1Item$index0.BackColor = $System.Color.FromName("White")
Else
   $ListView1Item$index0.BackColor = $System.Color.FromName("WhiteSmoke")   
EndIf

_________________
Mart

- He's chained forever to a world that's departed.....It's not enough, it's not enough - Sorrow by Pink Floyd.
Back to top
View user's profile Send private message
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Mon Feb 04, 2008 1:35 pm    Post subject: Reply with quote

Hey Mart, How you do'n?

Is this what you are looking for?

Code:

Break On
$System = CreateObject("Kixforms.System")
If Not $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf
$nul = $System.Application.EnableVisualStyles

$Form1 = $System.Form()
$Form1.Left = 0
$Form1.StartPosition = 0  ;FormStartPosition_Manual
$Form1.Size = $System.Size(400,300) ;(Width,Height)
$Form1.Text = "Form1"
$Form1.Top = 0

$ListView1 = $System.ListView()
$ListView1.Dock = 5  ;Fill
$ListView1.FullRowSelect = -1  ;True
$ListView1.View = $System.View_Details
$nul = $Form1.Controls.Add($ListView1)

$Item = $ListView1.Columns.Add($System.ColumnHeader("Item Column",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Column A",100,$System.HorizontalAlignment_Center))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Column B",100,$System.HorizontalAlignment_Right))

For $Index = 0 to 9
  $Items = "Item "+$Index,"Item "+$Index+"A","Item "+$Index+"B"
  $Item = $ListView1.Items.Add($System.ListViewItem($Items[0]))
  $Item = $ListView1.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Items[1]))
  $Item = $ListView1.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Items[2]))
Next

$Index = 1
For Each $Item in $ListView1.Items
  If $Index mod 2
    $Item.BackColor = $System.Color.FromName("WhiteSmoke")
  EndIf
  $Index = $Index + 1
Next

$Form1.Show  ;Displays the Form


While $Form1.Visible
   $Nul = Execute($Form1.DoEvents())
Loop
Exit 0

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Mart
KiXforms Regular
KiXforms Regular


Joined: 03 Oct 2005
Posts: 57
Location: Rotterdam - Netherlands

PostPosted: Mon Feb 04, 2008 7:13 pm    Post subject: Reply with quote

Sweet Cool You saved my day. Works and looks great Stunned

I'm using this to display files that had some kind of error while parsing it silently in a kf.net app we just started testing in our production environment. Line colouring improvises the readability a lot and could be used to highlight the line(s) that had an error with let's say a red(ish) background.

Thanks mate.

_________________
Mart

- He's chained forever to a world that's departed.....It's not enough, it's not enough - Sorrow by Pink Floyd.
Back to top
View user's profile Send private message
Mart
KiXforms Regular
KiXforms Regular


Joined: 03 Oct 2005
Posts: 57
Location: Rotterdam - Netherlands

PostPosted: Mon Feb 04, 2008 9:43 pm    Post subject: Reply with quote

Allrighte. This is the end result.
It creates a form with a listview with 20 columns. It then adds x amount of lines to the listview depending on the amount of lines in the source file. It checks to see if each line in the source file is 18 elements long. If not it adds the line number to an array and later colours all lines that had errors Salmon (light red). All lines that are ok are coloured white or whitesmoke.

The script below requires the ReadFile() UDF.
UDF Library @ KORG ReadFile() - Read a file into an array

Code:

Break On

$file = ReadFile("D:\somefile.tab")

$System = CreateObject("Kixforms.System")
If NOT $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf

$nul = $System.Application.EnableVisualStyles

$Form1 = $System.Form()
$Form1.FormBorderStyle = 1
$Form1.MaximizeBox = 0
$Form1.StartPosition = 1
$Form1.Size = $System.Size(920,700) ;(Width,Height)
$Form1.Text = "Error report..."

$ListView1 = $System.ListView()
$ListView1.Dock = 5  ;Fill
$ListView1.FullRowSelect = -1  ;True
$ListView1.GridLines = -1
$ListView1.HeaderStyle = 1  ;Nonclickable
$ListView1.Height = 550
$ListView1.Left = 5
$ListView1.Top = 10
$ListView1.View = $System.View_Details
$ListView1.Width = 800
$nul = $Form1.Controls.Add($ListView1)

;Create 20 column's.
$Item = $ListView1.Columns.Add($System.ColumnHeader("Line",50,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Debtor nr.",70,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Name",150,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Street",150,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Postalcode",80,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("City",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Client",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Discount",60,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Payment",60,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Accountmanager",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Account name",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Account number",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Account city",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Category",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("MM price",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("MM amount",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Client ref.",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("VAT code",70,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Bookingaccount",100,$System.HorizontalAlignment_Left))
$Item = $ListView1.Columns.Add($System.ColumnHeader("Week nr.",60,$System.HorizontalAlignment_Left))

;Insert data into column's line by line.
For $index = 0 to Ubound($file)
   $linenumber = $index + 1
   If $file[$index] <> ""
      $items = Split($file[$index], "   ")
      ;Check array lenght (must be 18).
      If Ubound($items) <> "18"
         ;Array is short or long. Add linenumber to errorline and write data.
         $errorline = $errorline + "   " + $linenumber
         $Item = $ListView1.Items.Add($System.ListViewItem($linenumber))
         For $itemindex = 0 to Ubound($items)
            $Item = $ListView1.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Items[$itemindex]))
         Next
      Else
         ;Array lenght is ok. Write data.
         $Item = $ListView1.Items.Add($System.ListViewItem($linenumber))
         For $itemindex = 0 to Ubound($items)
            $Item = $ListView1.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Items[$itemindex]))
         Next
      EndIf
   EndIf
Next

;Apply line colouring.
;Ok lines are white or whitesmoke other lines are salmon (light red).
$errorline = Split($errorline, "   ")
$index = 1
For Each $item in $ListView1.Items
   If $index mod 2
      If ASCAN($errorline, $index) = "-1"
         $item.BackColor = $System.Color.FromName("WhiteSmoke")
      Else
         $item.BackColor = $System.Color.FromName("Salmon")
      EndIf
   Else
      If ASCAN($errorline, $index) = "-1"
         $item.BackColor = $System.Color.FromName("White")
      Else
         $item.BackColor = $System.Color.FromName("Salmon")
      EndIf
   EndIf
  $index = $index + 1
Next

$Form1.Show  ;Displays the Form


While $Form1.Visible
   $Nul = Execute($Form1.DoEvents())
Loop
Exit 0

_________________
Mart

- He's chained forever to a world that's departed.....It's not enough, it's not enough - Sorrow by Pink Floyd.
Back to top
View user's profile Send private message
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Tue Feb 05, 2008 2:19 am    Post subject: Reply with quote

Glad I could help pally, well done it looks great.
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
pearly
KiXforms Aficionado
KiXforms Aficionado


Joined: 27 Jan 2004
Posts: 332

PostPosted: Tue Feb 05, 2008 6:50 pm    Post subject: Reply with quote

That's what I do benny, except you use the slick mod command. Cool

This is the color I use to alternate listview rows.

Code:

$System.Color.FromRgb(239,239,247)


Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group