Home
What's New
Delphi 3.0 Tips
Questions
Development Tools
Links

Updating a ListBox as one changes an Edit


procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if ListBox1.ItemIndex >= 0 then
    Edit1.Text := ListBox1.Items[ListBox1.ItemIndex];
end;

procedure TForm1.Edit1Change(Sender: TObject);
var i : byte;
begin
  if (ListBox1.Items.Count = 0)
  or (Edit1.GetTextLen = 0) then exit;

  for i := 0 to ListBox1.Items.Count - 1 do
    if StrIComp (PChar (Edit1.Text), PChar (copy (ListBox1.Items[i], 1, Edit1.GetTextLen))) = 0 then begin
      ListBox1.ItemIndex := i;
      exit;
    end;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Shift <> [] then exit;
  with ListBox1 do
    if Key = VK_Down then
      ItemIndex := 1 + ItemIndex
    else if Key = VK_Up then
      ItemIndex := ItemIndex - 1;

end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  {This function assumes the Edit and ListBox controls
   are in a dialog box which closes when an TButton, 
   namely OKBtn, is pressed}
  OKBtn.Click;
end;