Browse Source

add ItemIndex property

Johann 6 years ago
parent
commit
4c5c5525d3
2 changed files with 24 additions and 4 deletions
  1. 23 3
      bccombobox.pas
  2. 1 1
      test/test_bccombobox/umain.pas

+ 23 - 3
bccombobox.pas

@@ -19,21 +19,24 @@ type
     FHoverItem: integer;
     FListBox: TListBox;
     procedure ButtonClick(Sender: TObject);
+    function GetItemIndex: integer;
     function GetItems: TStrings;
     procedure ListBoxClick(Sender: TObject);
     procedure ListBoxMouseLeave(Sender: TObject);
     procedure ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
       Y: Integer);
     procedure ListBoxSelectionChange(Sender: TObject; User: boolean);
+    procedure SetItemIndex(AValue: integer);
     procedure SetItems(AValue: TStrings);
   protected
-
+    procedure UpdateCaption;
   public
     constructor Create(AOwner: TComponent); override;
     property HoverItem: integer read FHoverItem;
     property Button: TBCButton read FButton write FButton;
     property ListBox: TListBox read FListBox write FListBox;
     property Items: TStrings read GetItems write SetItems;
+    property ItemIndex: integer read GetItemIndex write SetItemIndex;
   published
 
   end;
@@ -78,6 +81,11 @@ begin
   end;
 end;
 
+function TBCComboBox.GetItemIndex: integer;
+begin
+  result := FListBox.ItemIndex;
+end;
+
 function TBCComboBox.GetItems: TStrings;
 begin
   Result := FListBox.Items;
@@ -86,7 +94,6 @@ end;
 procedure TBCComboBox.ListBoxClick(Sender: TObject);
 begin
   FForm.Visible := false;
-  FButton.Caption := FListBox.Items[FListBox.ItemIndex];
 end;
 
 procedure TBCComboBox.ListBoxMouseLeave(Sender: TObject);
@@ -111,7 +118,12 @@ end;
 
 procedure TBCComboBox.ListBoxSelectionChange(Sender: TObject; User: boolean);
 begin
-  FButton.Caption := FListBox.Items[FListBox.ItemIndex];
+  UpdateCaption;
+end;
+
+procedure TBCComboBox.SetItemIndex(AValue: integer);
+begin
+  FListBox.ItemIndex := AValue;
 end;
 
 procedure TBCComboBox.SetItems(AValue: TStrings);
@@ -119,6 +131,14 @@ begin
   Items := AValue;
 end;
 
+procedure TBCComboBox.UpdateCaption;
+begin
+  if FListBox.ItemIndex<>-1 then
+    FButton.Caption := FListBox.Items[FListBox.ItemIndex]
+  else
+    FButton.Caption := '';
+end;
+
 constructor TBCComboBox.Create(AOwner: TComponent);
 begin
   inherited Create(AOwner);

+ 1 - 1
test/test_bccombobox/umain.pas

@@ -39,7 +39,7 @@ begin
   BCComboBox1.Items.Add('Three');
 
   // Selecting items
-  BCComboBox1.ListBox.ItemIndex := 0;
+  BCComboBox1.ItemIndex := 0;
 
   // Style ListBox
   BCComboBox1.ListBox.Style := lbOwnerDrawFixed;