| 1 | Unit CustomCheckListBox;
|
|---|
| 2 |
|
|---|
| 3 | Interface
|
|---|
| 4 |
|
|---|
| 5 | {$ifdef win32}
|
|---|
| 6 | Uses
|
|---|
| 7 | StdCtrls, ExtCtrls;
|
|---|
| 8 |
|
|---|
| 9 | Type
|
|---|
| 10 | TCheckListBoxType = TCheckListBox;
|
|---|
| 11 |
|
|---|
| 12 | {$else}
|
|---|
| 13 | Uses
|
|---|
| 14 | Classes,Forms,Graphics,StdCtrls, SysUtils;
|
|---|
| 15 |
|
|---|
| 16 | Type
|
|---|
| 17 | ECheckListException = class( Exception );
|
|---|
| 18 |
|
|---|
| 19 | TCheckListBoxData = class
|
|---|
| 20 | Data: TObject;
|
|---|
| 21 | Checked: boolean;
|
|---|
| 22 | end;
|
|---|
| 23 |
|
|---|
| 24 | TCustomCheckListBox=Class(TListBox)
|
|---|
| 25 | Protected
|
|---|
| 26 | FBitmapList: TBitmapList;
|
|---|
| 27 | FOnCheckboxChanged: TNotifyEvent;
|
|---|
| 28 | Protected
|
|---|
| 29 | Function GetChecked( Index: LongInt ): boolean;
|
|---|
| 30 | Procedure SetChecked( Index: LongInt; Value:boolean );
|
|---|
| 31 |
|
|---|
| 32 | Function GetString( Index: LongInt ): string;
|
|---|
| 33 | Procedure SetString( Index: LongInt; Value: string );
|
|---|
| 34 | Function GetObject( Index: LongInt ): TObject;
|
|---|
| 35 | Procedure SetObject( Index: LongInt; Value: TObject );
|
|---|
| 36 |
|
|---|
| 37 | Function GetItemCount: longint;
|
|---|
| 38 |
|
|---|
| 39 | Function GetSelectedObject: TObject;
|
|---|
| 40 | Procedure SetSelectedObject( Value: TObject );
|
|---|
| 41 | Function GetSelectedString: string;
|
|---|
| 42 | Function GetCheckedCount: longint;
|
|---|
| 43 | Protected
|
|---|
| 44 | Procedure SetupComponent; Override;
|
|---|
| 45 | Destructor Destroy; Override;
|
|---|
| 46 | Procedure MouseClick( Button: TMouseButton;
|
|---|
| 47 | ShiftState: TShiftState;
|
|---|
| 48 | X, Y: longint ); override;
|
|---|
| 49 | Procedure MouseDblClick( Button: TMouseButton;
|
|---|
| 50 | ShiftState: TShiftState;
|
|---|
| 51 | X, Y: longint ); override;
|
|---|
| 52 | Procedure DrawItem( Index: LongInt;
|
|---|
| 53 | rec: TRect;
|
|---|
| 54 | State: TOwnerDrawState ); Override;
|
|---|
| 55 | Procedure CharEvent( Var key: Char;
|
|---|
| 56 | RepeatCount: Byte ); Override;
|
|---|
| 57 |
|
|---|
| 58 | Procedure CheckCheckboxHit( X, Y: longint );
|
|---|
| 59 | Property Style;
|
|---|
| 60 | Public
|
|---|
| 61 | property ItemCount: longint read GetItemCount;
|
|---|
| 62 | Property Checked[ Index: LongInt ]: boolean Read GetChecked Write SetChecked;
|
|---|
| 63 | Property Objects[ Index: longint ]: TObject read GetObject Write SetObject;
|
|---|
| 64 |
|
|---|
| 65 | Function AddItemObject( TheString: string;
|
|---|
| 66 | TheObject: TObject;
|
|---|
| 67 | Checked: boolean ): longint;
|
|---|
| 68 | Procedure Clear; override;
|
|---|
| 69 | Property CheckedCount: longint read GetCheckedCount;
|
|---|
| 70 |
|
|---|
| 71 | // get the checked items (and objects) to dest
|
|---|
| 72 | Procedure GetCheckedItems( Dest: TStrings );
|
|---|
| 73 | // same as above, but don't clear dest beforehand
|
|---|
| 74 | Procedure AddCheckedItems( Dest: TStrings );
|
|---|
| 75 | Property SelectedObject: TObject read GetSelectedObject write SetSelectedObject;
|
|---|
| 76 | Property SelectedString: string read GetSelectedString;
|
|---|
| 77 |
|
|---|
| 78 | Published
|
|---|
| 79 | Property OnCheckBoxChanged:TNotifyEvent Read FOnCheckboxChanged Write FOnCheckboxChanged;
|
|---|
| 80 | End;
|
|---|
| 81 |
|
|---|
| 82 | TCheckListBoxType = TCustomCheckListBox;
|
|---|
| 83 |
|
|---|
| 84 | Exports
|
|---|
| 85 | TCustomCheckListBox, 'User', 'CustomCheckListBox.bmp';
|
|---|
| 86 | {$endif}
|
|---|
| 87 |
|
|---|
| 88 | Procedure GetCheckedItems( CheckListBox: TCheckListBoxType;
|
|---|
| 89 | Items: TStrings );
|
|---|
| 90 | Procedure GetCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 91 | Objects: TList );
|
|---|
| 92 | Procedure AddCheckedItems( CheckListBox: TCheckListBoxType;
|
|---|
| 93 | Items: TStrings );
|
|---|
| 94 | Procedure AddCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 95 | Objects: TList );
|
|---|
| 96 | Procedure AddCheckListItemObject( CheckListBox: TCheckListBoxType;
|
|---|
| 97 | Text: String;
|
|---|
| 98 | TheObject: TObject;
|
|---|
| 99 | Checked: boolean );
|
|---|
| 100 | Procedure SetAllCheckListItems( CheckListBox: TCheckListBoxType;
|
|---|
| 101 | Value: boolean );
|
|---|
| 102 | Function CheckListItemCount( CheckListBox: TCheckListBoxType ): integer;
|
|---|
| 103 | Function CheckListObject( CheckListBox: TCheckListBoxType;
|
|---|
| 104 | Index: integer ): TObject;
|
|---|
| 105 | Function SelectedCheckListObject( CheckListBox: TCheckListBoxType ): TObject;
|
|---|
| 106 | Function SelectedCheckListItem( CheckListBox: TCheckListBoxType ): string;
|
|---|
| 107 | Function CheckedCount( CheckListBox: TCheckListBoxType ): integer;
|
|---|
| 108 |
|
|---|
| 109 | Implementation
|
|---|
| 110 |
|
|---|
| 111 | {$ifdef os232}
|
|---|
| 112 | Uses
|
|---|
| 113 | PMWin, OS2Def;
|
|---|
| 114 | {$R CustomCheckListBox}
|
|---|
| 115 | {$endif}
|
|---|
| 116 |
|
|---|
| 117 | {$ifdef win32}
|
|---|
| 118 | Procedure AddCheckedItems( CheckListBox: TCheckListBoxType;
|
|---|
| 119 | Items: TStrings );
|
|---|
| 120 | var
|
|---|
| 121 | i: integer;
|
|---|
| 122 | begin
|
|---|
| 123 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 124 | if CheckListBox.Checked[ i ] then
|
|---|
| 125 | Items.AddObject( CheckListBox.Items[ i ],
|
|---|
| 126 | CheckListBox.Items.Objects[ i ] );
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | Procedure AddCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 130 | Objects: TList );
|
|---|
| 131 | var
|
|---|
| 132 | i: integer;
|
|---|
| 133 | begin
|
|---|
| 134 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 135 | if CheckListBox.Checked[ i ] then
|
|---|
| 136 | Objects.Add( CheckListBox.Items.Objects[ i ] );
|
|---|
| 137 | end;
|
|---|
| 138 |
|
|---|
| 139 | Procedure GetCheckedItems( CheckListBox: TCheckListBoxType;
|
|---|
| 140 | Items: TStrings );
|
|---|
| 141 | begin
|
|---|
| 142 | Items.Clear;
|
|---|
| 143 | AddCheckedItems( CheckListBox, Items );
|
|---|
| 144 | end;
|
|---|
| 145 |
|
|---|
| 146 | Procedure GetCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 147 | Objects: TList );
|
|---|
| 148 | begin
|
|---|
| 149 | Objects.Clear;
|
|---|
| 150 | AddCheckedObjects( CheckListBox, Objects );
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | Procedure AddCheckListItemObject( CheckListBox: TCheckListBoxType;
|
|---|
| 154 | Text: String;
|
|---|
| 155 | TheObject: TObject;
|
|---|
| 156 | Checked: boolean );
|
|---|
| 157 | var
|
|---|
| 158 | AddPosition: integer;
|
|---|
| 159 | begin
|
|---|
| 160 | AddPosition:= CheckListBox.Items.AddObject( Text, TheObject );
|
|---|
| 161 | CheckListBox.Checked[ AddPosition ]:= Checked;
|
|---|
| 162 | end;
|
|---|
| 163 |
|
|---|
| 164 | Procedure SetAllCheckListItems( CheckListBox: TCheckListBoxType;
|
|---|
| 165 | Value: boolean );
|
|---|
| 166 | var
|
|---|
| 167 | i: integer;
|
|---|
| 168 | begin
|
|---|
| 169 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 170 | CheckListBox.Checked[ i ] := Value;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | Function CheckListItemCount( CheckListBox: TCheckListBoxType ): integer;
|
|---|
| 174 | begin
|
|---|
| 175 | Result:= CheckListBox.Items.Count;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | Function CheckListObject( CheckListBox: TCheckListBoxType;
|
|---|
| 179 | Index: integer ): TObject;
|
|---|
| 180 | begin
|
|---|
| 181 | Result:= CheckListBox.Items.Objects[ Index ];
|
|---|
| 182 | end;
|
|---|
| 183 |
|
|---|
| 184 | Function SelectedCheckListObject( CheckListBox: TCheckListBoxType ): TObject;
|
|---|
| 185 | begin
|
|---|
| 186 | if CheckListBox.ItemIndex <> -1 then
|
|---|
| 187 | Result:= CheckListBox.Items.Objects[ CheckListBox.ItemIndex ]
|
|---|
| 188 | else
|
|---|
| 189 | Result:= nil;
|
|---|
| 190 | end;
|
|---|
| 191 |
|
|---|
| 192 | Function SelectedCheckListItem( CheckListBox: TCheckListBoxType ): string;
|
|---|
| 193 | begin
|
|---|
| 194 | if CheckListBox.ItemIndex <> -1 then
|
|---|
| 195 | Result:= CheckListBox.Items[ CheckListBox.ItemIndex ]
|
|---|
| 196 | else
|
|---|
| 197 | Result:= '';
|
|---|
| 198 | end;
|
|---|
| 199 |
|
|---|
| 200 | Function CheckedCount( CheckListBox: TCheckListBoxType ): integer;
|
|---|
| 201 | var
|
|---|
| 202 | i: integer;
|
|---|
| 203 | begin
|
|---|
| 204 | Result:= 0;
|
|---|
| 205 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 206 | if CheckListBox.Checked[ i ] then
|
|---|
| 207 | inc( Result );
|
|---|
| 208 | end;
|
|---|
| 209 |
|
|---|
| 210 | {$else}
|
|---|
| 211 | Procedure AddCheckedItems( CheckListBox: TCustomCheckListBox;
|
|---|
| 212 | Items: TStrings );
|
|---|
| 213 | begin
|
|---|
| 214 | CheckListBox.AddCheckedItems( Items );
|
|---|
| 215 | end;
|
|---|
| 216 |
|
|---|
| 217 | Procedure GetCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 218 | Objects: TList );
|
|---|
| 219 | var
|
|---|
| 220 | i: integer;
|
|---|
| 221 | begin
|
|---|
| 222 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 223 | if CheckListBox.Checked[ i ] then
|
|---|
| 224 | Objects.Add( CheckListBox.Objects[ i ] );
|
|---|
| 225 | end;
|
|---|
| 226 |
|
|---|
| 227 | Procedure GetCheckedItems( CheckListBox: TCustomCheckListBox;
|
|---|
| 228 | Items: TStrings );
|
|---|
| 229 | begin
|
|---|
| 230 | Items.Clear;
|
|---|
| 231 | AddCheckedItems( CheckListBox, Items );
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | Procedure AddCheckedObjects( CheckListBox: TCheckListBoxType;
|
|---|
| 235 | Objects: TList );
|
|---|
| 236 | begin
|
|---|
| 237 | Objects.Clear;
|
|---|
| 238 | AddCheckedObjects( CheckListBox, Objects );
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | Procedure AddCheckListItemObject( CheckListBox: TCustomCheckListBox;
|
|---|
| 242 | Text: String;
|
|---|
| 243 | TheObject: TObject;
|
|---|
| 244 | Checked: boolean );
|
|---|
| 245 | begin
|
|---|
| 246 | CheckListBox.AddItemObject( Text, TheObject, Checked );
|
|---|
| 247 | end;
|
|---|
| 248 |
|
|---|
| 249 | Procedure SetAllCheckListItems( CheckListBox: TCustomCheckListBox;
|
|---|
| 250 | Value: boolean );
|
|---|
| 251 | var
|
|---|
| 252 | i: integer;
|
|---|
| 253 | begin
|
|---|
| 254 | CheckListBox.BeginUpdate;
|
|---|
| 255 | for i:= 0 to CheckListBox.Items.Count - 1 do
|
|---|
| 256 | CheckListBox.Checked[ i ] := Value;
|
|---|
| 257 | CheckListBox.EndUpdate;
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | Function CheckListItemCount( CheckListBox: TCustomCheckListBox ): integer;
|
|---|
| 261 | begin
|
|---|
| 262 | Result:= CheckListBox.ItemCount;
|
|---|
| 263 | end;
|
|---|
| 264 |
|
|---|
| 265 | Function CheckListObject( CheckListBox: TCustomCheckListBox;
|
|---|
| 266 | Index: integer ): TObject;
|
|---|
| 267 | begin
|
|---|
| 268 | Result:= CheckListBox.Objects[ Index ];
|
|---|
| 269 | end;
|
|---|
| 270 |
|
|---|
| 271 | Function SelectedCheckListObject( CheckListBox: TCustomCheckListBox ): TObject;
|
|---|
| 272 | begin
|
|---|
| 273 | Result:= CheckListBox.SelectedObject;
|
|---|
| 274 | end;
|
|---|
| 275 |
|
|---|
| 276 | Function SelectedCheckListItem( CheckListBox: TCustomCheckListBox ): string;
|
|---|
| 277 | begin
|
|---|
| 278 | Result:= CheckListBox.SelectedString;
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | Function CheckedCount( CheckListBox: TCustomCheckListBox ): integer;
|
|---|
| 282 | begin
|
|---|
| 283 | Result:= CheckListBox.CheckedCount;
|
|---|
| 284 | end;
|
|---|
| 285 |
|
|---|
| 286 | {$endif}
|
|---|
| 287 |
|
|---|
| 288 | {$ifdef os2}
|
|---|
| 289 |
|
|---|
| 290 | Procedure TCustomCheckListBox.SetupComponent;
|
|---|
| 291 | Begin
|
|---|
| 292 | Inherited SetupComponent;
|
|---|
| 293 |
|
|---|
| 294 | Name := 'CustomCheckListBox';
|
|---|
| 295 | Style := lbOwnerdrawFixed;
|
|---|
| 296 | ItemHeight := 20;
|
|---|
| 297 | FBitmapList:= TBitmapList.Create;
|
|---|
| 298 | FBitmapList.AddResourceName('BmpLBUnChecked');
|
|---|
| 299 | FBitmapList.AddResourceName('BmpLBChecked');
|
|---|
| 300 | End;
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | Destructor TCustomCheckListBox.Destroy;
|
|---|
| 304 | Begin
|
|---|
| 305 | Inherited Destroy;
|
|---|
| 306 | FBitmapList.Destroy;
|
|---|
| 307 | End;
|
|---|
| 308 |
|
|---|
| 309 | Procedure TCustomCheckListBox.DrawItem( Index: LongInt;
|
|---|
| 310 | rec: TRect;
|
|---|
| 311 | State: TOwnerDrawState );
|
|---|
| 312 | var
|
|---|
| 313 | textWidth, textHeight: longint;
|
|---|
| 314 | destWidth, destHeight: longint;
|
|---|
| 315 | textY: Longint;
|
|---|
| 316 | s: string;
|
|---|
| 317 | Dest: Trect;
|
|---|
| 318 | X: longint;
|
|---|
| 319 | Bitmap: TBitmap;
|
|---|
| 320 | idx: longint;
|
|---|
| 321 | Item: TCheckListBoxData;
|
|---|
| 322 | Begin
|
|---|
| 323 |
|
|---|
| 324 | Dest:= rec;
|
|---|
| 325 | dec( Dest.top ); // minor adjustments since we seem to get a slightly
|
|---|
| 326 | inc( Dest.left ); // incorrect area to draw on...
|
|---|
| 327 | destWidth:= Dest.Right - Dest.left;
|
|---|
| 328 | destHeight:= Dest.Top - Dest.Bottom;
|
|---|
| 329 |
|
|---|
| 330 | // First draw the item background, in highlight colour if needed
|
|---|
| 331 | IF State * [odSelected] <> [] THEN
|
|---|
| 332 | Canvas.Brush.Color := clHighLight
|
|---|
| 333 | ELSE
|
|---|
| 334 | Canvas.Brush.Color := Color;
|
|---|
| 335 | Canvas.FillRect( Dest, Canvas.Brush.Color );
|
|---|
| 336 |
|
|---|
| 337 | X:= Dest.left;
|
|---|
| 338 |
|
|---|
| 339 | // I don't know why but the bitmap colour is affected by the pen colour ?!
|
|---|
| 340 | Canvas.Pen.Color := clBlack;
|
|---|
| 341 |
|
|---|
| 342 | Item:= Items.Objects[ Index ] as TCheckListBoxData;
|
|---|
| 343 |
|
|---|
| 344 | idx:= longint( Item.Checked );
|
|---|
| 345 | Bitmap:= FBitmapList.Bitmaps[ idx ];
|
|---|
| 346 | Canvas.Draw( X + 2,
|
|---|
| 347 | Dest.bottom + ( ItemHeight - Bitmap.Height ) div 2,
|
|---|
| 348 | Bitmap );
|
|---|
| 349 |
|
|---|
| 350 | inc( X, 20 );
|
|---|
| 351 | // Draw string
|
|---|
| 352 | s := Items[Index];
|
|---|
| 353 |
|
|---|
| 354 | Canvas.GetTextExtent( s, textWidth, textHeight);
|
|---|
| 355 |
|
|---|
| 356 | // Centre the text vertically in the available space
|
|---|
| 357 | textY:= Dest.Bottom + ((destHeight - textHeight) DIV 2);
|
|---|
| 358 | IF textY < Dest.Bottom THEN
|
|---|
| 359 | textY:= Dest.Bottom;
|
|---|
| 360 |
|
|---|
| 361 | Canvas.Pen.Color := PenColor;
|
|---|
| 362 | Canvas.TextOut( X, TextY, s);
|
|---|
| 363 |
|
|---|
| 364 | End;
|
|---|
| 365 |
|
|---|
| 366 | Procedure TCustomCheckListBox.CharEvent( Var key:Char;
|
|---|
| 367 | RepeatCount:Byte);
|
|---|
| 368 | Begin
|
|---|
| 369 | If key= ' ' Then
|
|---|
| 370 | Checked[ ItemIndex ]:= not Checked[ ItemIndex ];
|
|---|
| 371 | Inherited CharEvent( key, RepeatCount );
|
|---|
| 372 | End;
|
|---|
| 373 |
|
|---|
| 374 | Function TCustomCheckListBox.GetChecked(Index:LongInt):boolean;
|
|---|
| 375 | var
|
|---|
| 376 | Item: TCheckListBoxData;
|
|---|
| 377 | Begin
|
|---|
| 378 | Item:= Items.Objects[ Index ] as TCheckListBoxData;
|
|---|
| 379 | Result:= Item.Checked;
|
|---|
| 380 | End;
|
|---|
| 381 |
|
|---|
| 382 | Procedure TCustomCheckListBox.SetChecked(Index:LongInt;Value:boolean);
|
|---|
| 383 | var
|
|---|
| 384 | Item: TCheckListBoxData;
|
|---|
| 385 | Begin
|
|---|
| 386 | Item:= Items.Objects[ Index ] as TCheckListBoxData;
|
|---|
| 387 | Item.Checked:= Value;
|
|---|
| 388 | Invalidate;
|
|---|
| 389 | End;
|
|---|
| 390 |
|
|---|
| 391 | Procedure TCustomCheckListBox.CheckCheckboxHit( X, Y: longint );
|
|---|
| 392 | Var
|
|---|
| 393 | index: longint;
|
|---|
| 394 | p: TPoint;
|
|---|
| 395 | Begin
|
|---|
| 396 | if X < 20 then
|
|---|
| 397 | begin
|
|---|
| 398 | p.X:= X;
|
|---|
| 399 | p.Y:= Y;
|
|---|
| 400 | index:= ItemAtPos( p, true );
|
|---|
| 401 | if index<>-1 then
|
|---|
| 402 | Checked[ index ]:= not Checked[ index ];
|
|---|
| 403 | end;
|
|---|
| 404 | end;
|
|---|
| 405 |
|
|---|
| 406 | Procedure TCustomCheckListBox.MouseClick( Button: TMouseButton;
|
|---|
| 407 | ShiftState: TShiftState;
|
|---|
| 408 | X, Y: longint );
|
|---|
| 409 | Begin
|
|---|
| 410 | CheckCheckboxHit( X, Y );
|
|---|
| 411 | Inherited MouseClick( Button, ShiftState, X, Y );
|
|---|
| 412 | End;
|
|---|
| 413 |
|
|---|
| 414 | Procedure TCustomCheckListBox.MouseDblClick( Button: TMouseButton;
|
|---|
| 415 | ShiftState: TShiftState;
|
|---|
| 416 | X, Y: longint );
|
|---|
| 417 | Begin
|
|---|
| 418 | CheckCheckboxHit( X, Y );
|
|---|
| 419 | Inherited MouseDblClick( Button, ShiftState, X, Y );
|
|---|
| 420 | End;
|
|---|
| 421 |
|
|---|
| 422 | Function TCustomCheckListBox.GetString( index: longint ): string;
|
|---|
| 423 | Begin
|
|---|
| 424 | Result:= Items[ index ];
|
|---|
| 425 | End;
|
|---|
| 426 |
|
|---|
| 427 | Procedure TCustomCheckListBox.SetString( Index: LongInt; Value: string );
|
|---|
| 428 | Begin
|
|---|
| 429 | Items[ index ]:= Value;
|
|---|
| 430 | End;
|
|---|
| 431 |
|
|---|
| 432 | Function TCustomCheckListBox.GetObject( Index: LongInt ): TObject;
|
|---|
| 433 | var
|
|---|
| 434 | Item: TCheckListBoxData;
|
|---|
| 435 | Begin
|
|---|
| 436 | Item:= Items.Objects[ Index ] as TCheckListBoxData;
|
|---|
| 437 | Result:= Item.Data;
|
|---|
| 438 | End;
|
|---|
| 439 |
|
|---|
| 440 | Procedure TCustomCheckListBox.SetObject( Index: LongInt; Value: TObject );
|
|---|
| 441 | var
|
|---|
| 442 | Item: TCheckListBoxData;
|
|---|
| 443 | Begin
|
|---|
| 444 | Item:= Items.Objects[ Index ] as TCheckListBoxData;
|
|---|
| 445 | Item.Data:= Value;
|
|---|
| 446 | End;
|
|---|
| 447 |
|
|---|
| 448 | Function TCustomCheckListBox.AddItemObject( TheString: string;
|
|---|
| 449 | TheObject: TObject;
|
|---|
| 450 | Checked: boolean ): longint;
|
|---|
| 451 | var
|
|---|
| 452 | Item: TCheckListBoxData;
|
|---|
| 453 | Begin
|
|---|
| 454 | Item:= TCheckListBoxData.Create;
|
|---|
| 455 | Item.Data:= TheObject;
|
|---|
| 456 | Item.Checked:= Checked;
|
|---|
| 457 | Result:= Items.AddObject( TheString, Item );
|
|---|
| 458 | End;
|
|---|
| 459 |
|
|---|
| 460 | Procedure TCustomCheckListBox.Clear;
|
|---|
| 461 | var
|
|---|
| 462 | i: longint;
|
|---|
| 463 | Item: TCheckListBoxData;
|
|---|
| 464 | Begin
|
|---|
| 465 | for i:= 0 to Items.Count - 1 do
|
|---|
| 466 | begin
|
|---|
| 467 | Item:= Items.Objects[ i ] as TCheckListBoxData;
|
|---|
| 468 | Item.Destroy;
|
|---|
| 469 | end;
|
|---|
| 470 | Inherited Clear;
|
|---|
| 471 | End;
|
|---|
| 472 |
|
|---|
| 473 | Function TCustomCheckListBox.GetItemCount: longint;
|
|---|
| 474 | Begin
|
|---|
| 475 | Result:= Items.Count;
|
|---|
| 476 | End;
|
|---|
| 477 |
|
|---|
| 478 | Function TCustomCheckListBox.GetSelectedObject: TObject;
|
|---|
| 479 | Begin
|
|---|
| 480 | if ItemIndex<>-1 then
|
|---|
| 481 | Result:= Objects[ ItemIndex ]
|
|---|
| 482 | else
|
|---|
| 483 | Result:= nil;
|
|---|
| 484 | End;
|
|---|
| 485 |
|
|---|
| 486 | Procedure TCustomCheckListBox.SetSelectedObject( Value: TObject );
|
|---|
| 487 | Var
|
|---|
| 488 | Index: longint;
|
|---|
| 489 | Begin
|
|---|
| 490 | Index:= Items.IndexOfObject( Value );
|
|---|
| 491 | if Index <> -1 then
|
|---|
| 492 | ItemIndex:= Index;
|
|---|
| 493 | End;
|
|---|
| 494 |
|
|---|
| 495 | Function TCustomCheckListBox.GetSelectedString: string;
|
|---|
| 496 | Begin
|
|---|
| 497 | if ItemIndex<>-1 then
|
|---|
| 498 | Result:= Items[ ItemIndex ]
|
|---|
| 499 | else
|
|---|
| 500 | Result:= '';
|
|---|
| 501 | End;
|
|---|
| 502 |
|
|---|
| 503 | Function TCustomCheckListBox.GetCheckedCount: longint;
|
|---|
| 504 | var
|
|---|
| 505 | i: longint;
|
|---|
| 506 | Item: TCheckListBoxData;
|
|---|
| 507 | Begin
|
|---|
| 508 | Result:= 0;
|
|---|
| 509 | for i:= 0 to Items.Count - 1 do
|
|---|
| 510 | begin
|
|---|
| 511 | Item:= Items.Objects[ i ] as TCheckListBoxData;
|
|---|
| 512 | if Item.Checked then
|
|---|
| 513 | inc( Result );
|
|---|
| 514 | end;
|
|---|
| 515 | end;
|
|---|
| 516 |
|
|---|
| 517 | Procedure TCustomCheckListBox.AddCheckedItems( Dest: TStrings );
|
|---|
| 518 | var
|
|---|
| 519 | i: longint;
|
|---|
| 520 | Item: TCheckListBoxData;
|
|---|
| 521 | Begin
|
|---|
| 522 | for i:= 0 to Items.Count - 1 do
|
|---|
| 523 | begin
|
|---|
| 524 | Item:= Items.Objects[ i ] as TCheckListBoxData;
|
|---|
| 525 | if Item.Checked then
|
|---|
| 526 | Dest.AddObject( Items[ i ],
|
|---|
| 527 | Item.Data );
|
|---|
| 528 | end;
|
|---|
| 529 | end;
|
|---|
| 530 |
|
|---|
| 531 | Procedure TCustomCheckListBox.GetCheckedItems( Dest: TStrings );
|
|---|
| 532 | begin
|
|---|
| 533 | Dest.Clear;
|
|---|
| 534 | AddCheckedItems( Dest );
|
|---|
| 535 | end;
|
|---|
| 536 |
|
|---|
| 537 | Initialization
|
|---|
| 538 | {Register classes}
|
|---|
| 539 | RegisterClasses([TCustomCheckListBox]);
|
|---|
| 540 | {$endif}
|
|---|
| 541 |
|
|---|
| 542 | End.
|
|---|