Solved

Example of MRU in ComboBox

Posted on 2002-07-21
6
959 Views
Last Modified: 2008-02-01
HI

Im making an app that can log onto different servers but only one at once. I would like it that when the app loads it can read the registry and grab a list of servers that have been used before and put them into a combobox OR if they enter a new name it stores it into the registry with the others.

I have looked at some components but they all seem to be with files and not entries in a registry like i need.

Example will get the points.
Many thanks
B.
0
Comment
Question by:BRowley
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 7168767
Hi,
Form.OnCreate loads server names from HKEY_CURRENT_USER\Software\Geobul\Servers

ComboBox1.OnKeyPress adds a new server name entered in the combobox.

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  KeyPath: string = 'Software\Geobul\Servers';
  ValueNames: TStringList;

implementation

{$R *.DFM}

uses Registry;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  with TRegistry.Create do begin
    // HKEY_CURRENT_USER by default
    if OpenKey(KeyPath, false) then begin
      ValueNames := TStringList.Create;
      try
        ComboBox1.Items.Clear;
        GetValueNames(ValueNames);
        for i := 0 to ValueNames.Count - 1 do begin
          ComboBox1.Items.Add(ReadString(ValueNames[i]));
        end;
      finally
        ValueNames.Free;
      end;
      CloseKey;
    end;
    Free;
  end;
end;

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin // Enter key
    if ComboBox1.Items.IndexOf(ComboBox1.Text) = -1 then begin // new server was entered
      with TRegistry.Create do begin
        if OpenKey(KeyPath, true) then begin
          WriteString('Server'+ IntToStr(ComboBox1.Items.Count), ComboBox1.Text);
          ComboBox1.Items.Add(ComboBox1.Text);
        end;
      end;
    end;
  end;
end;

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 7168800
I'm sorry, use the following KeyPress event instead of the previous one. Registry.Free is missing there.

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin // Enter key
    if ComboBox1.Items.IndexOf(ComboBox1.Text) = -1 then begin // new server was entered
      with TRegistry.Create do begin
        if OpenKey(KeyPath, true) then begin
          WriteString('Server'+ IntToStr(ComboBox1.Items.Count), ComboBox1.Text);
          ComboBox1.Items.Add(ComboBox1.Text);
        end;
        Free;
      end;
    end;
  end;
end;
0
 

Author Comment

by:BRowley
ID: 7169240
Excellent. Dont suppose you know how to stop the beep on enter on the combobox do you please?

Anyway many thanks for your time and help.
Regards
Barry
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:geobul
ID: 7169298
Thank for the points, Barry.

About beep:

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
...
  ComboBox1.Items.Add(ComboBox1.Text);
  Key := #0; // add this line to suppress the beep
...
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 7169723
Actually, it'd be better to place that line (Key := #0;)immediately after:
if Key = #13 then begin // Enter key

That way all beeps will be ignored. In my previous comment only the beeps when adding new items are suppressed.

Regards, Geo
0
 

Author Comment

by:BRowley
ID: 7169736
Thank you.
regards
B.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 86
can't find the executable in Simulator 1 80
Firemonkey DbLookupComboBox equivalent ? 2 35
update joined tables 2 30
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now