Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - InputQuery/InputBox-History


Dunkel - Sa 07.07.07 18:04
Titel: InputQuery/InputBox-History
Hallo zusammen!

Kann man bei InputQuery oder InputBox eine History der zuletzt eingegebenen Texte speichern/setzen?
Oder gibt es eine Komponente welche ähnliches macht wie die oben genannten Funktionen (sprich einfach nur eine Texteingabe vom User abfragen) mit der Möglichkeit eine History anzulegen?
Oder muss ich mir da selber was basteln?

Besten Dank im Voraus für eure Antworten!


Narses - So 08.07.07 11:09

Moin!

AFAIK gibt´s dafür keine fertige Kompo. :nixweiss: Aber eben schnell einen passenden Dialog zusammenstellen kann ja nicht soo schwer sein... :zwinker:

cu
Narses


Dunkel - So 08.07.07 12:49

Danke!
Habe ich mir fast schon gedacht, dass ich mir da selber was proggen muss. Dabei herausgekommen ist das:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
function InputQueryEx(const ACaption, APrompt: string; const AHistory: TStrings; var Value: string): Boolean;
  function GetAveCharSize(Canvas: TCanvas): TPoint;
  var
    I: Integer;
    Buffer: array[0..51] of Char;
  begin
    for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A'));
    for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));
    GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
    Result.X := Result.X div 52;
  end;
var
  Form: TForm;
  Prompt: TLabel;
  ComboBox: TCombobox;
  DialogUnits: TPoint;
  ButtonTop, ButtonWidth, ButtonHeight: Integer;
begin
  Result := False;
  Form := TForm.Create(Application);
  with Form do
    try
      Canvas.Font := Font;
      DialogUnits := GetAveCharSize(Canvas);
      BorderStyle := bsDialog;
      Caption := ACaption;
      ClientWidth := MulDiv(180, DialogUnits.X, 4);
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := APrompt;
        Left := MulDiv(8, DialogUnits.X, 4);
        Top := MulDiv(8, DialogUnits.Y, 8);
        Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
        WordWrap := True;
      end;
      ComboBox := TCombobox.Create(Form);
      with ComboBox do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := MulDiv(164, DialogUnits.X, 4);
        MaxLength := 255;
        Items:= AHistory;
        Style:= csSimple;
        MaxLength := 255;
        Text := Value;
        SelectAll;
      end;
      ButtonTop := ComboBox.Top + ComboBox.Height + 15;
      ButtonWidth := MulDiv(50, DialogUnits.X, 4);
      ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := 'OK';
        ModalResult := idOK;
        Default := True;
        SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := 'Cancel';
        ModalResult := idCancel;
        Cancel := True;
        SetBounds(MulDiv(92, DialogUnits.X, 4), ButtonTop, //ComboBox.Top + ComboBox.Height + 15,
          ButtonWidth, ButtonHeight);
        Form.ClientHeight := Top + Height + 13;
      end;
      if ShowModal = idOK then
      begin
        Value := ComboBox.Text;
        Result := True;
      end;
    finally
      Form.Free;
    end;
end;


Das ist einfach nur eine modifizierte Input-Query-Funktion. Diese hat statt einem TEdit eine TComboBox auf der Form. Diese TComboBox wird anhand des neuen Aufrufparameters AHistory, in der sich die History befindet, befüllt. Die History muss dann im Hauptprogramm verwaltet werden (also laden, speichern, hinzufügen & lösche von Einträgen).
Funktioniert super.