Ver Mensaje Individual
  #1  
Antiguo 28-03-2012
elarys elarys is offline
Miembro
 
Registrado: abr 2007
Posts: 94
Reputación: 20
elarys Va por buen camino
Busqueda y edicion en PopupMenu

Necesito crear un componente heredado de un TPopupMenu a este, agregarle un TEdit... y al escribir en el edit filtrar (poner visible := false los menu item) o como sea de forma automatica, aquellos menuitem del popupmenu que no contengan el texto escrito, desde el comienzo que se vayan filtrando

algo asi seria la busqueda que quiero que haga
Código Delphi [-]
    if AnsiStartsText(Edit1.Text, Popupmenu) then
      filtrar

Lo hice con un TEdit en el Form
Y a los menuitem los hago con varios TLabel que se van creando segun un TStringList
Y lo que hice hacer lo mas parecido su funcionamiento como si fuera un popupmenu
ya sea pasar el mouse por encima y hace como que toma el foco, luego con las teclas arriba y abajo igual
Código Delphi [-]
unit UMenu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, StrUtils;

type
  TFMenu = class(TForm)
    edtSearch: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure edtSearchChange(Sender: TObject);
    procedure edtSearchKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure edtSearchKeyPress(Sender: TObject; var Key: Char);
  private
    Menus:TStringList;
    Focus:integer;
    procedure PaintMenu(Menus:TStringList);
    procedure MenuClick(Sender: TObject);
    procedure MouseEnter(Sender: TObject);
    procedure MouseLeave(Sender: TObject);
  public
    procedure CreateList;
  end;

var
  FMenu: TFMenu;

implementation

uses
  Unit1, Unit2;

{$R *.dfm}

procedure TFMenu.PaintMenu(Menus:TStringList);
var
  i, top:integer;
  newLabel:TLabel;
  Menu:TStringList;
begin
  Menu := TStringList.Create;
  top := 32;

  for i := ComponentCount -1 downto 0 do
    if Components[i] Is TLabel then
      Components[i].Free;

  for i := 0 to Menus.Count - 1 do
  begin
    newLabel := TLabel.Create(FMenu);
    newLabel.Parent := FMenu;
    newLabel.AlignWithMargins := True;
    newLabel.Anchors := [akLeft, akTop, akRight, akBottom];
    newLabel.AutoSize := False;
    newLabel.Margins.Bottom := 0;
    newLabel.Margins.Left := 0;
    newLabel.Margins.Right := 0;
    newLabel.Margins.Top := 0;
    newLabel.Layout := tlCenter;
    newLabel.Top := top;
    newLabel.Left := 0;
    newLabel.Width := 216;
    newLabel.Height := 18;
    newLabel.Name := 'newMenu'+ IntToStr(i);
    newLabel.Tag := i + 1;
    newLabel.OnMouseEnter := MouseEnter;
    newLabel.OnMouseLeave := MouseLeave;

    newLabel.OnClick := MenuClick;
    newLabel.Caption := '   ' + Menus[i];
    top := top + 18;
  end;
end;

procedure TFMenu.edtSearchChange(Sender: TObject);
var
  i:integer;
  newMenuList:TStringList;
begin
  newMenuList := TStringList.Create;
  for i := 0 to Menus.Count - 1 do
    if AnsiStartsText(edtSearch.Text, Menus[i]) then
      newMenuList.Add(Menus[i]);

  newMenuList.Sort;
  PaintMenu(newMenuList);
  newMenuList.Clear;
end;

procedure TFMenu.edtSearchKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  i : integer;
begin
  if Key = VK_DOWN then
  begin
    Focus := Focus + 1;

    if Focus = ComponentCount then
      Focus := 1;

    if Focus = 0 then
      Focus := 1;
  end;

  if Key = VK_UP then
  begin
    Focus := Focus - 1;

    if Focus = -1 then
      Focus := ComponentCount - 1;

    if Focus = 0 then
      Focus := ComponentCount - 1;
  end;

  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] Is TLabel then
    begin
      TLabel(Components[i]).Color := clBtnFace;
      TLabel(Components[i]).Font.Color := clWindowText;
    end;
  end;

  if Components[Focus] Is TLabel then
  begin
    TLabel(Components[Focus]).Color := clActiveCaption;
    TLabel(Components[Focus]).Font.Color := clWindow;
  end;
end;

procedure TFMenu.edtSearchKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if TLabel(Components[Focus]).Tag = 1 then
    begin
      Form1 := TForm1.Create(Self);
      Form1.ShowModal;
    end;

    if TLabel(Components[Focus]).Tag = 2 then
    begin
      Form2 := TForm2.Create(Self);
      Form2.ShowModal;
    end;
  end;
end;

procedure TFMenu.CreateList;
begin
  Menus := TStringList.Create;

  Menus.Add('ABI Activity');
  Menus.Add('Cargo Release Statistics');
  Menus.Add('Cargo Status');
  Menus.Add('Commodity Log');
  Menus.Add('Container Activity');
  Menus.Add('Cost Submission Report');
  Menus.Add('Customer ABI Statement Report');
  Menus.Add('Customer Duty Due Report');
  Menus.Add('Detailed Log');
  Menus.Add('Duty Payment');
  Menus.Add('Entry Number Labels');
  Menus.Add('Entry Summary Data Transfer Report');
  Menus.Add('FDA PNC Pending');
  Menus.Add('Insurance Bond Report');
  Menus.Add('Notice of Liquidation');
  Menus.Add('Part Number Report');
  Menus.Add('POD Report');
  Menus.Add('Quick Log');
  Menus.Add('TIB Expiration');
  Menus.Sort;

  PaintMenu(Menus);
end;

procedure TFMenu.FormCreate(Sender: TObject);
begin
  CreateList;
  Focus := 0;
end;

procedure TFMenu.MouseEnter(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] Is TLabel then
    begin
      TLabel(Components[i]).Color := clBtnFace;
      TLabel(Components[i]).Font.Color := clWindowText;
    end;
  end;

  TLabel(Sender).Color := clActiveCaption;
  TLabel(Sender).Font.Color := clWindow;
  Focus := TLabel(Sender).Tag;
end;

procedure TFMenu.MouseLeave(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] Is TLabel then
    begin
      TLabel(Components[i]).Color := clBtnFace;
      TLabel(Components[i]).Font.Color := clWindowText;
    end;
  end;
end;

procedure TFMenu.MenuClick(Sender: TObject);
var
  Form1:TForm1;
  Form2:TForm2;
begin
  if TLabel(Sender).Tag = 1 then
  begin
    Form1 := TForm1.Create(Self);
    Form1.ShowModal;
  end;

  if TLabel(Sender).Tag = 2 then
  begin
    Form2 := TForm2.Create(Self);
    Form2.ShowModal;
  end;
end;

end.

Este codigo me funciona y podria seguir mejorandolo, pero necesito hacerlo con un popupmenu y un edit
y con esto crear mi componente TPopupMenuEdit... Alguna idea.

Editado: si no me entienden y tampoco ven muy bien mi codigo, creen una nueva aplicacion agreguen mi unidad como la principal y ejecuten, para que vean el funcionamiento que le quiero dar a mi componente.
En el evento MenuClick, cree dos nuevos formularios sin nada para probar el evento onclick de cada elemento creado TLabel.
Acepto sugerencias de hacerlo con otros componentes pero el componente final tiene que quedar como un popupmenu y un edit arriba de todos los menu items.

Última edición por elarys fecha: 28-03-2012 a las 21:22:24.
Responder Con Cita