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

ListView - clicking column header to resort?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Feb 09, 2007 6:05 pm    Post subject: Reply with quote

The ColumnClick event is just a first step. I'm still pondering how to build in automatic sorting into the listview itself. Stay-tuned on that.
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: Fri Feb 09, 2007 9:20 pm    Post subject: Reply with quote

Ok here is an example using a Bubble Sort:
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

$Form = $System.Form()
$Form.StartPosition = 1  ;FormStartPosition_CenterScreen
$Form.Size = $System.Size(600,500) ;(Width,Height)
$Form.Text = "Form"

$ListView = $System.ListView()
$ListView.ColumnClick = "ListView_ColumnClick(0)"
$ListView.Dock = 5  ;Fill
$ListView.GridLines = -1  ;True
$ListView.View = $System.View_Details
$nul = $Form.Controls.Add($ListView)

$ListViewColumn0 = $ListView.Columns.Add($System.ColumnHeader("Column 0",100,$System.HorizontalAlignment_Left))
$ListViewColumn1 = $ListView.Columns.Add($System.ColumnHeader("Column 1",100,$System.HorizontalAlignment_Center))
$ListViewColumn2 = $ListView.Columns.Add($System.ColumnHeader("Column 2",100,$System.HorizontalAlignment_Right))

$Col0 = Split("26 17 7 1 4 13 21 22 14 3 2 20 23 10 24 19 11 18 8 9 12 5 6 16 15 25") ; 1-26
$Col1 = Split("Z Q G A D M U V N C B T W J X S K R H I L E F P O Y")                  ; A-Z
$Col2 = Split("1 10 20 26 23 14 6 5 13 24 25 7 4 17 3 8 16 9 19 18 15 22 21 11 12 2") ; 26-1

For $Index = 0 to 25
   $Item = $ListView.Items.Add($System.ListViewItem($Col0[$Index]))
   $Item = $ListView.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Col1[$Index]))
   $Item = $ListView.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Col2[$Index]))
Next

$ButtonPanel = $System.Panel()
$ButtonPanel.BorderStyle = 1  ;FixedSingle
$ButtonPanel.Dock = 1  ;Top
$ButtonPanel.Height = 40
$nul = $Form.Controls.Add($ButtonPanel)

$Button = $System.Button()
$Button.Click = "SortOrder()"
$Button.Left = 10
$Button.Text = "Ascending"
$Button.Top = 10
$nul = $ButtonPanel.Controls.Add($Button)

$Form.Show  ;Displays the Form

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

Function SortOrder()
   If $Order = 0
      $Order = 1
      $Button.Text = "Descending"
      $ListView.ColumnClick = "ListView_ColumnClick(1)"
   Else
      $Order = 0
      $Button.Text = "Ascending"
      $ListView.ColumnClick = "ListView_ColumnClick(0)"
   EndIf
EndFunction

Function ListView_ColumnClick($Order)
   Dim $ItemCount,$ColumnIndex,$Row,$ItemText,$NextRow,$NextItemText,$Item,$nul
   $ListView.BeginUpdate
   $ItemCount = $ListView.Items.Count - 1
   $ColumnIndex = $System.EventArgs.Column
   For $Row = 0 to $ItemCount
      For $NextRow = $Row + 1 to $ItemCount
         $ItemText = $ListView.Items.Item($Row).SubItems.Item($ColumnIndex).Text
         $NextItemText = $ListView.Items.Item($NextRow).SubItems.Item($ColumnIndex).Text
         If ($NextItemText < $ItemText And Not $Order) Or ($NextItemText > $ItemText And $Order)
            $Item = $ListView.Items.Item($NextRow)
            $nul = $ListView.Items.Item($NextRow).Remove
            $nul = $ListView.Items.Insert($Row,$Item)
         EndIf
      Next
   Next
   $ListView.EndUpdate
EndFunction

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)


Last edited by benny69 on Fri Feb 09, 2007 9:36 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
benny69
KiXforms Advocate
KiXforms Advocate


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

PostPosted: Fri Feb 09, 2007 9:35 pm    Post subject: Reply with quote

Ok here is an example using a Human Sort:
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

$Form = $System.Form()
$Form.StartPosition = 1  ;FormStartPosition_CenterScreen
$Form.Size = $System.Size(600,500) ;(Width,Height)
$Form.Text = "Form"

$ListView = $System.ListView()
$ListView.ColumnClick = "ListView_ColumnClick(0)"
$ListView.Dock = 5  ;Fill
$ListView.GridLines = -1  ;True
$ListView.View = $System.View_Details
$nul = $Form.Controls.Add($ListView)

$ListViewColumn0 = $ListView.Columns.Add($System.ColumnHeader("Column 0",100,$System.HorizontalAlignment_Left))
$ListViewColumn1 = $ListView.Columns.Add($System.ColumnHeader("Column 1",100,$System.HorizontalAlignment_Center))
$ListViewColumn2 = $ListView.Columns.Add($System.ColumnHeader("Column 2",100,$System.HorizontalAlignment_Right))

$Col0 = Split("26 17 7 1 4 13 21 22 14 3 2 20 23 10 24 19 11 18 8 9 12 5 6 16 15 25") ; 1-26
$Col1 = Split("Z Q G A D M U V N C B T W J X S K R H I L E F P O Y")                  ; A-Z
$Col2 = Split("1 10 20 26 23 14 6 5 13 24 25 7 4 17 3 8 16 9 19 18 15 22 21 11 12 2") ; 26-1

For $Index = 0 to 25
   $Item = $ListView.Items.Add($System.ListViewItem($Col0[$Index]))
   $Item = $ListView.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Col1[$Index]))
   $Item = $ListView.Items.Item($Index).SubItems.Add($System.ListViewSubItem($Col2[$Index]))
Next

$ButtonPanel = $System.Panel()
$ButtonPanel.BorderStyle = 1  ;FixedSingle
$ButtonPanel.Dock = 1  ;Top
$ButtonPanel.Height = 40
$nul = $Form.Controls.Add($ButtonPanel)

$Button = $System.Button()
$Button.Click = "SortOrder()"
$Button.Left = 10
$Button.Text = "Ascending"
$Button.Top = 10
$nul = $ButtonPanel.Controls.Add($Button)

$Form.Show  ;Displays the Form

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

Function SortOrder()
   If $Order = 0
      $Order = 1
      $Button.Text = "Descending"
      $ListView.ColumnClick = "ListView_ColumnClick(1)"
   Else
      $Order = 0
      $Button.Text = "Ascending"
      $ListView.ColumnClick = "ListView_ColumnClick(0)"
   EndIf
EndFunction

Function ListView_ColumnClick($Order)
   Dim $ItemCount,$ColumnIndex,$TempLen,$Length,$Row,$ItemText,$NextRow,$NextItemText,$Item,$nul
   $ListView.BeginUpdate
   $ItemCount = $ListView.Items.Count - 1
   $ColumnIndex = $System.EventArgs.Column
   For $Row = 0 to $ItemCount
      $TempLen = Len($ListView.Items.Item($Row).SubItems.Item($ColumnIndex).Text)
      If $TempLen > $Length
         $Length = $TempLen
      EndIf
   Next
   For $Row = 0 to $ItemCount
      For $NextRow = $Row + 1 to $ItemCount
         $ItemText = $ListView.Items.Item($Row).SubItems.Item($ColumnIndex).Text
         $NextItemText = $ListView.Items.Item($NextRow).SubItems.Item($ColumnIndex).Text
         If (PadNumbers($NextItemText,$Length) < PadNumbers($ItemText,$Length) And Not $Order) Or (PadNumbers($NextItemText,$Length) > PadNumbers($ItemText,$Length) And $Order)
            $Item = $ListView.Items.Item($NextRow)
            $nul = $ListView.Items.Item($NextRow).Remove
            $nul = $ListView.Items.Insert($Row,$Item)
         EndIf
      Next
   Next
   $ListView.EndUpdate
EndFunction

Function PadNumbers($,$i)
   Dim $g,$h
   For $h = Len($) to $i
      For $g = 0 to 9
         $=Join(Split($,$g),#+$g)
      Next
   Next
   $PadNumbers=$
EndFunction

_________________
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
benny69
KiXforms Advocate
KiXforms Advocate


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

PostPosted: Fri Feb 09, 2007 9:36 pm    Post subject: Reply with quote

I think the Human Sort is my pref.
_________________
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
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Feb 09, 2007 10:36 pm    Post subject: Reply with quote

wow impressive. i would not have guessed that sorting things in the script would have worked-out so well. playing...
Back to top
View user's profile Send private message
Gargoyle
KiXforms Aficionado
KiXforms Aficionado


Joined: 30 Dec 2003
Posts: 366
Location: Arizona

PostPosted: Fri Feb 09, 2007 11:53 pm    Post subject: Reply with quote

Shawn / Benny
Would it have been any easier if the .Net Sort functions were implemented. I did alot of research on it back during the bubble sort golf round.

I don't know how much it would be for Shawn to implement such a change but just another one of those thoughts running through my mind.

_________________
Parents were invented to make children happy by giving them something to ignore.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Sat Feb 10, 2007 2:13 am    Post subject: Reply with quote

Yeah I agree. The "simpliest" thing to my mind would be to have a ListView.Sort method, would work like this:

$ListView.Sort(column,order,type)

Where order would be ascending or decending, and type would be like string, integer,date, etc ...
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Sat Feb 10, 2007 2:22 am    Post subject: Reply with quote

Well, maybe the "easiest-easiest" way to do this would be to just let the listview sort the columns automatically by default. Might need something like this:

$NameColumn.DataType = <string,number,date>

So that the listview would know how to sort it. And maybe something like this:

$ListView.AutoSort = true/false

To turn this whole auto sort thing on. While this idea isn't pure .net - it sure beats the alternative ways of using callbacks etc.
Back to top
View user's profile Send private message
masken
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 14 Mar 2003
Posts: 202
Location: Gothenburg, Sweden

PostPosted: Sat Feb 10, 2007 4:04 pm    Post subject: Reply with quote

hmm... nice, but what about this:

A property like "$ListView.Sortable" controls wether the listview itself accepts re-sorting, and how, like:
0 = off, 1 = per-row, and 2 = per-column (ie, "breaking row-objects order")

AutoSort on/off could be another useful property Smile

...and of course stuff to handle re-sorting like suggested with the $ListView.Sort(column,order,type) thing, so that if the $ListView.Sortable is set to 1 for example, sorting the 2nd column resorts the whole table according to the 2nd column (ie, not breaking rows).

btw Shawn, I ran into a limitation in the length of a an item in column Surprised Seems like there's a limit of 260 chars?
Back to top
View user's profile Send private message MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Sat Feb 10, 2007 5:10 pm    Post subject: Reply with quote

Quote:

Seems like there's a limit of 260 chars?


Dont have time to research this right now, but suspect this is a .net thing, not a kf.net thing. If a .net thing - not much can be done about. Will look into later today unless someone googles an answer sooner.
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
Goto page Previous  1, 2
Page 2 of 2

 
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