Advertisement
HEX0x29A

ListBox.OnChange

Mar 26th, 2017
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.60 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TListBox = class(StdCtrls.TListBox)
  11.   private
  12.     FItemIndex: Integer;
  13.     FOnChange: TNotifyEvent;
  14.     procedure CNCommand(var AMessage: TWMCommand); message CN_COMMAND;
  15.   protected
  16.     procedure Change; virtual;
  17.     procedure SetItemIndex(const Value: Integer); override;
  18.   published
  19.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  20.   end;
  21.  
  22.   TForm1 = class(TForm)
  23.     ListBox1: TListBox;
  24.     Edit1: TEdit;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure ListBox1OnChange(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. { TListBox }
  39.  
  40. procedure TListBox.Change;
  41. begin
  42.   if Assigned(FOnChange) then
  43.     FOnChange(Self);
  44. end;
  45.  
  46. procedure TListBox.CNCommand(var AMessage: TWMCommand);
  47. begin
  48.   inherited;
  49.   if (AMessage.NotifyCode = LBN_SELCHANGE) and (FItemIndex <> ItemIndex) then
  50.   begin
  51.     FItemIndex := ItemIndex;
  52.     Change;
  53.   end;
  54. end;
  55.  
  56. procedure TListBox.SetItemIndex(const Value: Integer);
  57. begin
  58.   inherited;
  59.   if FItemIndex <> ItemIndex then
  60.   begin
  61.     FItemIndex := ItemIndex;
  62.     Change;
  63.   end;
  64. end;
  65.  
  66. {$R *.dfm}
  67.  
  68. procedure TForm1.FormCreate(Sender: TObject);
  69. begin
  70.   ListBox1.OnChange := ListBox1OnChange;
  71. end;
  72.  
  73. procedure TForm1.ListBox1OnChange(Sender: TObject);
  74. begin
  75.   if ListBox1.ItemIndex >= 0 then
  76.     Edit1.Text := ListBox1.Items[ListBox1.ItemIndex]
  77.   else
  78.     Edit1.Clear;
  79. end;
  80.  
  81. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement