Mega Code Archive

 
Categories / Delphi / VCL
 

A TMeo that has more than one column and can be sorted

Title: A TMeo that has more than one column and can be sorted Question: How can I get a TMemo that has more than one colun and in which I can sort the rown using a THeaderControl Answer: Just see the project. I know the use of a TListView would ease it up a lot but to me, the tmemo is an underestimated control. With this, it will have some of the big advantages of a tlistview and still keep its own functionality.... Here the pas file: unit Unit1; { Combine TMemo with THeaderControl to achieve a TMemo that: -- can have more than one Column -- sort the lines in the TMemo by each of its columns by clicking on each section of the THeaderControl Programmed by: Omer Yasar Can - omercan@home.nl Here, also to learn: How to create your own class with its own variables and its own procedures/functions and use it} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) HeaderControl1: THeaderControl; StatusBar1: TStatusBar; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure HeaderControl1SectionClick(HeaderControl: THeaderControl; Section: THeaderSection); private { Private declarations } public { Public declarations } end; type TNamesTowns = class Names, Towns: string; procedure Init(PNames,PTowns: string); end; var Form1: TForm1; NamesTowns: array [0..2] of TNamesTowns; NumberOfItems: integer; implementation {$R *.dfm} procedure TNamesTowns.Init(PNames,PTowns: string); begin Names := PNames; Towns := PTowns; end; {------------------------------------------------------------------------------} procedure TForm1.FormCreate(Sender: TObject); Var tabs: Array [0..1] of Integer; //for making memo with multible columns //i: integer; begin NumberOfItems := 3; tabs[0] := 18 * 4; //to get more than one column (here just 2) Memo1.Clear; // {test-phase} Memo1.Lines.Add('A'#9'S'); Memo1.Perform( EM_SETTABSTOPS, 2, LongInt(@tabs)); Memo1.Refresh; NamesTowns[0] := TNamesTowns.Create; NamesTowns[0].Init('John Melkenow','New York'); Memo1.Lines.Add(NamesTowns[0].Names+#9+NamesTowns[0].Towns); NamesTowns[1] := TNamesTowns.Create; NamesTowns[1].Init('Mark Feelix','Amsterdam'); Memo1.Lines.Add(NamesTowns[1].Names+#9+NamesTowns[1].Towns); NamesTowns[2] := TNamesTowns.Create; NamesTowns[2].Init('Ferda Saracoglu','Istanbul'); Memo1.Lines.Add(NamesTowns[2].Names+#9+NamesTowns[2].Towns); end; procedure TForm1.HeaderControl1SectionClick(HeaderControl: THeaderControl; Section: THeaderSection); var //btw: I use here the bubblesort (fast enough for this!!! done: boolean; i, n: integer; Dummy,Dummy2: string; begin Memo1.Lines.Clear; n := NumberOfItems; case Section.Index of 0: repeat done := true; for i := 0 to n - 2 do if NamesTowns[i].Names NamesTowns[i + 1].Names then begin Dummy := NamesTowns[i].Names; Dummy2 := NamesTowns[i].Towns; NamesTowns[i].Names := NamesTowns[i + 1].Names; NamesTowns[i].Towns := NamesTowns[i + 1].Towns; NamesTowns[i + 1].Names := Dummy; NamesTowns[i + 1].Towns := Dummy2; done := false; end; until done; 1: repeat done := true; for i := 0 to n - 2 do if NamesTowns[i].Towns NamesTowns[i + 1].Towns then begin Dummy := NamesTowns[i].Names; Dummy2 := NamesTowns[i].Towns; NamesTowns[i].Names := NamesTowns[i + 1].Names; NamesTowns[i].Towns := NamesTowns[i + 1].Towns; NamesTowns[i + 1].Names := Dummy; NamesTowns[i + 1].Towns := Dummy2; done := false; end; until done; end; //case Section.Index of for i:=0 to 2 do Memo1.Lines.Add(NamesTowns[i].Names + #9 + NamesTowns[i].Towns); end; end. here the dpr: program TMemo_THeaderControl; uses Forms, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. and the dfm:unit1.dfm object Form1: TForm1 Left = 416 Top = 378 Width = 229 Height = 143 Caption = 'Examp.fuo.TMemo' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object HeaderControl1: THeaderControl Left = 0 Top = 0 Width = 221 Height = 17 DragReorder = False Sections = item ImageIndex = -1 Text = 'Name' Width = 110 end item ImageIndex = -1 Text = 'Town' Width = 110 end OnSectionClick = HeaderControl1SectionClick end object StatusBar1: TStatusBar Left = 0 Top = 97 Width = 221 Height = 19 Panels = item Text = 'Dir:\TMemo_THeaderControl' Width = 50 end SimplePanel = False end object Memo1: TMemo Left = 0 Top = 17 Width = 221 Height = 80 Hint = 'Combine TMemo with THeaderControl'#13#10'to achieve a TMemo that:'#13#10'-- ' + 'can have more than one Column'#13#10'-- sort the lines in the TMemo by' + ' each of its'#13#10' columns by clicking on each section of the '#13#10' ' + ' THeaderControl' Align = alClient Lines.Strings = ( 'Memo1') ParentShowHint = False ShowHint = True TabOrder = 2 end end