santiago14,
Cita:
Empezado por santiago14
...si no se define un archivo de texto, el combo actúa de manera natural...
|
Recuerda que en las versiones anteriores (v1, v2 y v3) siempre existió el archivo histórico por defecto
'History.txt',
pero si no se llama el método LoadHistory (v2 y v3) el ComboBox Modificado funcionara como un ComboBox estándar.
Cita:
Empezado por santiago14
...¿No perderá rendimiento cuando el listado se haga medio grande?...
...sería bueno tener un historial por cada combo...y que el archivo de texto se cree automáticamente...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils, ExtCtrls;
type
TComboBox = class(StdCtrls.TComboBox)
private
CtrlLoad : Boolean;
UnFlicker : Boolean;
UpDown : Boolean;
AuxText : String;
HistoryName : String;
StoredItems : TStringList;
CountItems : Byte;
procedure FilterItems;
procedure StoredItemsChange(Sender: TObject);
procedure LoadHistory(FileHistory : String = ''; CountHistory : Byte = 10);
procedure ComboExit(Sender: TObject);
procedure ComboCloseUp(Sender: TObject);
procedure ComboKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure CNCommand(var Message: TWMCommand); Message CN_COMMAND;
protected
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
end;
type
TForm1 = class(TForm)
ComboBox1 : TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
Button1 : TButton;
ComboBox5: TComboBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TComboBox.Create(Owner: TComponent);
begin
inherited;
AutoComplete := False;
StoredItems := TStringList.Create;
StoredItems.OnChange := StoredItemsChange;
Self.OnExit := ComboExit;
Self.OnCloseUp := ComboCloseUp;
Self.OnKeyDown := ComboKeyDown;
UnFlicker := False;
CtrlLoad := False;
end;
destructor TComboBox.Destroy;
begin
StoredItems.Free;
inherited;
end;
procedure TComboBox.LoadHistory(FileHistory : String = ''; CountHistory : Byte = 10);
begin
CtrlLoad := True;
Self.DropDownCount := CountHistory;
CountItems := CountHistory;
if FileHistory = EmptyStr then
HistoryName := 'History_' + Self.Name + '.txt'
else
HistoryName := FileHistory;
if FileExists(HistoryName) then
Self.StoredItems.LoadFromFile(HistoryName);
end;
procedure TComboBox.CNCommand(var Message: TWMCommand);
begin
inherited;
if Message.NotifyCode = CBN_EDITUPDATE then
FilterItems;
end;
procedure TComboBox.FilterItems;
var
i : Integer;
StartPos, EndPos : Integer;
begin
SendMessage(Handle, CB_GETEDITSEL, WPARAM(@StartPos), LPARAM(@EndPos));
AuxText := Text;
if Text <> EmptyStr then
begin
Items.Clear;
for i := 0 to StoredItems.Count - 1 do
begin
if PosEx(LowerCase(Text), LowerCase(StoredItems[i])) > 0 then
Items.Add(StoredItems[i]);
end;
end
else
Items.Assign(StoredItems);
if UnFlicker then
SendMessage(Handle, CB_SHOWDROPDOWN, Integer(True), 0);
Text := AuxText;
SendMessage(Handle, CB_SETEDITSEL, 0, MakeLParam(StartPos, EndPos));
UnFlicker := True;
end;
procedure TComboBox.StoredItemsChange(Sender: TObject);
begin
if Assigned(StoredItems) then
FilterItems;
end;
procedure TComboBox.ComboExit(Sender: TObject);
var
i : Integer;
begin
if (Items.IndexOf(Text) = -1) and (Text <> EmptyStr) and CtrlLoad then
begin
StoredItems.Insert(0,Text);
for i := StoredItems.Count - 1 downto CountItems do
StoredItems.Delete(i);
StoredItems.SaveToFile(HistoryName);
Self.ItemIndex := 0;
end;
end;
procedure TComboBox.ComboCloseUp(Sender: TObject);
begin
If (AuxText <> EmptyStr) and (UpDown = False) then
begin
Text := AuxText;
end;
UpDown := False;
end;
procedure TComboBox.ComboKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_UP) or (Key = VK_DOWN) then
UpDown := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
begin
for i := 0 to ComponentCount - 1 do
begin
if Components[i] is TComboBox then
begin
if TComboBox(Components[i]).Text <> EmptyStr then
ShowMessage(TComboBox(Components[i]).Text);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.LoadHistory('History_1.txt');
ComboBox2.LoadHistory('History_2.txt',12);
ComboBox3.LoadHistory;
ComboBox4.LoadHistory('',5);
end;
end.
El código anterior es la Versión 4 del código sugerido en el
Msg #16 que permite:
Simular de forma básica la función SHAutoComplete en un componente TComboBox modificado.
Notas:
1- El método
LoadHistory permite definir el
Archivo Histórico y su Cantidad Máxima de Items, asociado al ComboBox Modificado.
2- Si se llama el método
LoadHistory sin parámetros,
se creara por defecto el archivo 'History_' + Self.Name + '.txt' con un límite máximo de 10 items.
3- Los Items ahora son
Insertados al principio de la lista en lugar de
Adicionados al final de esta, por lo tanto esta será de
reiniciación cíclica en función del número máximo de items por archivo definidos explicita o implícitamente en el método LoadHistory.
4- El número
máximo de Items por archivo histórico es
255.
5- Si se omite el método
LoadHistory, el ComboBox Modificado
funcionara como un ComboBox estándar.
El ejemplo esta disponible en el link:
http://terawiki.clubdelphi.com/Delph...omboBox+V4.rar
Espero sea útil
Nelson.