TSplitter: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TSplitter
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A [[TSplitter]]  is a component that can be placed on a panel or form as vertical or horizontal bar to separate sub-panels functionally. | |
| 2 | + | ||
| 3 | + | The control can be used as a visual separator between two halves of your form and allows the user of your application to move it either vertically or horizontally. It can be found on the [[Additional tab]] of the [[Component Palette]]. | |
| 4 | + | ||
| 5 | + | TSplitter can work basically in two different modes: via [[Autosize / Layout|Align]] (the [[Delphi]] way) or via [[Autosize / Layout|AnchorSides]] (not supported by Delphi). | |
| 6 | + | ||
| 7 | + | ## Splitter and Align | |
| 8 | + | ||
| 9 | + | Align can be used for many simple layouts like two controls or a row of controls. For example when you need some freely resizable controls like a memo and a listbox. | |
| 10 | + | ||
| 11 | + | The following example demonstrates this. | |
| 12 | + | ||
| 13 | + | ### Design time | |
| 14 | + | ||
| 15 | + | 1. create a new [[TForm|form]] | |
| 16 | + | 2. drop a [[TMemo]] on a form (left click on the TMemo icon in component palette to select, then left click on the form) | |
| 17 | + | 3. set in [[IDE Window: Object Inspector|Object Inspector]] Align of Memo1 to `alLeft`. | |
| 18 | + | 4. drop a TSplitter on a form | |
| 19 | + | 5. the default Align is already `alLeft` | |
| 20 | + | 6. drop another TMemo on the form. | |
| 21 | + | 7. set in Object Inspector Align of Memo2 to `alClient`. | |
| 22 | + | ||
| 23 | + | ### [[runtime|Run time]] | |
| 24 | + | ||
| 25 | + | You can also perform the same actions as above in code instead of in the designer/Object Inspector: | |
| 26 | + | ||
| 27 | + | ``` Pascal | |
| 28 | + | procedure TMainForm.FormCreate(Sender: TObject); | |
| 29 | + | var | |
| 30 | + | Memo1: TMemo; | |
| 31 | + | Splitter1: TSplitter; | |
| 32 | + | Memo2: TMemo; | |
| 33 | + | begin | |
| 34 | + | Memo1:=TMemo.Create(Self); | |
| 35 | + | with Memo1 do begin | |
| 36 | + | Name:='Memo1'; | |
| 37 | + | Parent:=Self; | |
| 38 | + | Align:=alLeft; | |
| 39 | + | end; | |
| 40 | + | Splitter1:=TSplitter.Create(Self); | |
| 41 | + | with Splitter1 do begin | |
| 42 | + | Name:='Splitter1'; | |
| 43 | + | Parent:=Self; | |
| 44 | + | Left:=1; // position it right of Memo1 | |
| 45 | + | Align:=alLeft; | |
| 46 | + | end; | |
| 47 | + | Memo2:=TMemo.Create(Self); | |
| 48 | + | with Memo2 do begin | |
| 49 | + | Name:='Memo2'; | |
| 50 | + | Parent:=Self; | |
| 51 | + | Align:=alClient; | |
| 52 | + | end; | |
| 53 | + | end; | |
| 54 | + | ``` | |
| 55 | + | ||
| 56 | + | ## Splitter with AnchorSides | |
| 57 | + | ||
| 58 | + | Anchor sides allows more fine tuned layouts. Align fills all the space. AnchorSides allow to anchor controls to any other sibling control. | |
| 59 | + | ||
| 60 | + | ### Design time | |
| 61 | + | ||
| 62 | + | 1. create a new [[TForm|form]] | |
| 63 | + | 2. drop a [[TMemo]] on a form (left click on the TMemo icon in component paletter to select, then left click on the form) | |
| 64 | + | 3. set in Object Inspector Align of Memo1 to `alLeft` | |
| 65 | + | 4. drop a TSplitter on a form | |
| 66 | + | 5. set the Align property of the Splitter1 to `alNone` | |
| 67 | + | 6. select the Memo1 | |
| 68 | + | 7. View -\> Anchor Editor | |
| 69 | + | 8. anchor the right side of Memo1 to the Splitter1 | |
| 70 | + | 9. drop another TMemo on the form. | |
| 71 | + | 10. set the Align property of Memo2 to `alRight`. | |
| 72 | + | 11. anchor the left side of Memo2 to Splitter1. Make sure to anchor to the right side of Splitter1 (the button on the Anchor editor below the combobox). | |
| 73 | + | ||
| 74 | + | ### Run time | |
| 75 | + | ||
| 76 | + | You can also perform the same actions as above in code instead of in the designer/Object Inspector: | |
| 77 | + | ||
| 78 | + | ``` Pascal | |
| 79 | + | procedure TMainForm.FormCreate(Sender: TObject); | |
| 80 | + | var | |
| 81 | + | Memo1: TMemo; | |
| 82 | + | Splitter1: TSplitter; | |
| 83 | + | Memo2: TMemo; | |
| 84 | + | begin | |
| 85 | + | Memo1:=TMemo.Create(Self); | |
| 86 | + | with Memo1 do begin | |
| 87 | + | Name:='Memo1'; | |
| 88 | + | Parent:=Self; | |
| 89 | + | Align:=alLeft; | |
| 90 | + | end; | |
| 91 | + | Splitter1:=TSplitter.Create(Self); | |
| 92 | + | with Splitter1 do begin | |
| 93 | + | Name:='Splitter1'; | |
| 94 | + | Parent:=Self; | |
| 95 | + | Align:=alNone; | |
| 96 | + | Left:=100; // some value | |
| 97 | + | AnchorParallel(akBottom,0,Parent); | |
| 98 | + | end; | |
| 99 | + | Memo1.AnchorToNeighbour(akRight,0,Splitter1); | |
| 100 | + | Memo2:=TMemo.Create(Self); | |
| 101 | + | with Memo2 do begin | |
| 102 | + | Name:='Memo2'; | |
| 103 | + | Parent:=Self; | |
| 104 | + | Align:=alRight; | |
| 105 | + | AnchorToNeighbour(akLeft,0,Splitter1); | |
| 106 | + | end; | |
| 107 | + | end; | |
| 108 | + | ``` | |
| 109 | + | ||
| 110 | + | ## Color of splitter | |
| 111 | + | ||
| 112 | + | By default, TSplitter has themed painting. So assigning to Color property won't help to change the color. But if you assign dummy OnPaint event, themed painting will be disabled. | |
| 113 | + | ||
| 114 | + | ``` pascal | |
| 115 | + | Splitter1.OnPaint:= @SplitterOnPaintDummy; | |
| 116 | + | Splitter1.Color:= clGreen; | |
| 117 | + | .... | |
| 118 | + | ||
| 119 | + | procedure TfmMain.SplitterOnPaintDummy(Sender: TObject); | |
| 120 | + | begin | |
| 121 | + | //empty, to disable themed paint | |
| 122 | + | end; | |
| 123 | + | ``` | |
| 124 | + | ||
| 125 | + | ## See also | |
| 126 | + | ||
| 127 | + | - [[doc:lcl/extctrls/tsplitter.html|TSplitter doc]] | |
| 128 | + | - [[TPairSplitter]] | |
| 129 | + | ||
| 130 | + | --- | |
| 131 | + | *From the Free Pascal wiki, page "TSplitter", as it stood on 2023-02-13 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
