Solved

How to use the list of counties from Windows in Delphi.

Posted on 2004-08-25
8
406 Views
Last Modified: 2008-01-09
In Windows XP there is somewhere a list of countries.
This list I would like to use in Delphi 7 Pro

I want it to be displayed in a combobox, and after selecting is the country-index (if there is an index) of the windows-list must be stored in my table. Also I don't know if the eventual index is fixed. Maybe when a new country is added all indexes change and I don't have the correct link in my database.
What would be a good way to use this country-list?
Should I store the list in my own table (to be certain about the index)?
Is there also a international code (for telephone) present?

There are a lot of assumptions from my part, so if you can help I would be very greatfull when you supply some working code.

Thanks Stef
0
Comment
Question by:Delphiwizard
  • 5
  • 2
8 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 11900818
//........ Unit1_Q_21089487.pas

unit Unit1_Q_21089487;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids,    ComCtrls, ValEdit, Registry;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ValueListEditor: TValueListEditor;
    EditReg: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private   { Private-Deklarationen }
  public    { Public-Deklarationen }
    procedure Get_Countries(Key_Name: string; Vle: TValueListEditor);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Get_Countries(Key_Name: string; Vle: TValueListEditor);
var
  I:      Integer;
  RegPh:  TRegistry;
  RegC:   TRegistry;
  SLPh:   TStringList;
  SL:     TStringList;
begin
  EditReg.Text := Key_Name;
  RegPh := TRegistry.Create;
  try
    RegPh.LazyWrite := False;
    RegPh.RootKey := HKEY_LOCAL_MACHINE;
    RegPh.OpenKey(Key_Name, False);
    RegC := TRegistry.Create;
    try
      RegC.LazyWrite := False;
      RegC.RootKey := HKEY_LOCAL_MACHINE;
      SLPh := TStringList.Create;
      try
        RegPh.GetKeyNames(SLPh);
        SL := TStringList.Create;
        try

          for I := 0 to SLPh.Count -1 do
          begin
            RegC.OpenKey(Key_Name+'\'+SLPh.Strings[I], False);
            RegC.GetValueNames(SL);
            Vle.InsertRow(SLPh.Strings[I], RegC.ReadString('Name'), True);
            RegC.CloseKey;
          end;
        finally
          SL.Free;
        end;
      finally
        SLPh.Free;
      end;
    finally
      RegC.Free;
    end;
  finally
    RegPh.CloseKey;
    RegPh.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Get_Countries('SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Country List', ValueListEditor);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  EditReg.Text := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I:      Integer;
  N:      Integer;
begin
  N := ValueListEditor.RowCount - 1;
  for I := N downto 1 do
    ValueListEditor.DeleteRow(I);
  FormCreate(Self);
end;

end.

//........ Unit1_Q_21089487.dfm

object Form1: TForm1
  Left = 212
  Top = 114
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 478
  ClientWidth = 688
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ValueListEditor: TValueListEditor
    Left = 4
    Top = 26
    Width = 640
    Height = 446
    TabOrder = 0
    ColWidths = (
      300
      334)
  end
  object Button2: TButton
    Left = 594
    Top = 4
    Width = 48
    Height = 21
    Caption = 'Clear'
    TabOrder = 1
    OnClick = Button2Click
  end
  object EditReg: TEdit
    Left = 144
    Top = 2
    Width = 448
    Height = 21
    ReadOnly = True
    TabOrder = 2
  end
  object Button1: TButton
    Left = 4
    Top = 4
    Width = 128
    Height = 21
    Caption = 'Get Countries'
    TabOrder = 3
    OnClick = Button1Click
  end
end
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 250 total points
ID: 11901613
The countries are saved into the windows registry by Phone code:
001 USA
101 Anguilla
102 Antigua and Barbuda
....  ..............................

So the phone code is unique for every country and never changes ....

//........

unit Unit1_Q_21107859;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids,    ComCtrls, ValEdit, Registry;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ValueListEditor: TValueListEditor;
    EditReg: TEdit;
    cbxCountries: TComboBox;
    EditPhoneCode: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cbxCountriesCloseUp(Sender: TObject);
  private   { Private-Deklarationen }
  public    { Public-Deklarationen }
    procedure Get_Countries(Key_Name: string; Vle: TValueListEditor);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Get_Countries(Key_Name: string; Vle: TValueListEditor);
var
  I:      Integer;
  RegPh:  TRegistry;
  RegC:   TRegistry;
  SLPh:   TStringList;
  SL:     TStringList;
begin
  cbxCountries.Clear;
  EditReg.Text := Key_Name;
  RegPh := TRegistry.Create;
  try
    RegPh.LazyWrite := False;
    RegPh.RootKey := HKEY_LOCAL_MACHINE;
    RegPh.OpenKey(Key_Name, False);
    RegC := TRegistry.Create;
    try
      RegC.LazyWrite := False;
      RegC.RootKey := HKEY_LOCAL_MACHINE;
      SLPh := TStringList.Create;
      try
        RegPh.GetKeyNames(SLPh);
        SL := TStringList.Create;
        try

          for I := 0 to SLPh.Count -1 do
          begin
            RegC.OpenKey(Key_Name+'\'+SLPh.Strings[I], False);
            RegC.GetValueNames(SL);
            Vle.InsertRow(SLPh.Strings[I], RegC.ReadString('Name'), True);
            cbxCountries.AddItem(RegC.ReadString('Name'), TObject(StrToInt(SLPh.Strings[I])));
            RegC.CloseKey;
          end;
        finally
          SL.Free;
        end;
      finally
        SLPh.Free;
      end;
    finally
      RegC.Free;
    end;
  finally
    RegPh.CloseKey;
    RegPh.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Get_Countries('SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Country List', ValueListEditor);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  EditReg.Text := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I:      Integer;
  N:      Integer;
begin
  N := ValueListEditor.RowCount - 1;
  for I := N downto 1 do
    ValueListEditor.DeleteRow(I);
  FormCreate(Self);
end;

procedure TForm1.cbxCountriesCloseUp(Sender: TObject);
begin
  EditPhoneCode.Text
  := IntToStr(Integer(cbxCountries.Items.Objects[cbxCountries.ItemIndex]));
end;

end.

//........

object Form1: TForm1
  Left = 212
  Top = 114
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 478
  ClientWidth = 504
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ValueListEditor: TValueListEditor
    Left = 4
    Top = 56
    Width = 428
    Height = 416
    TabOrder = 0
    ColWidths = (
      138
      284)
  end
  object Button2: TButton
    Left = 392
    Top = 2
    Width = 40
    Height = 21
    Caption = 'Clear'
    TabOrder = 1
    OnClick = Button2Click
  end
  object EditReg: TEdit
    Left = 104
    Top = 2
    Width = 288
    Height = 21
    ReadOnly = True
    TabOrder = 2
  end
  object Button1: TButton
    Left = 4
    Top = 4
    Width = 96
    Height = 21
    Caption = 'Get Countries'
    TabOrder = 3
    OnClick = Button1Click
  end
  object cbxCountries: TComboBox
    Left = 144
    Top = 28
    Width = 288
    Height = 21
    Style = csDropDownList
    DropDownCount = 32
    ItemHeight = 13
    TabOrder = 4
    OnCloseUp = cbxCountriesCloseUp
  end
  object EditPhoneCode: TEdit
    Left = 4
    Top = 28
    Width = 140
    Height = 21
    ReadOnly = True
    TabOrder = 5
  end
end
0
 
LVL 7

Expert Comment

by:sftweng
ID: 11902140
>> So the phone code is unique for every country and never changes ...

Not entirely true - Canada is also 001
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11904743
Sorry, I didn't know that, but into my Windows XP registry phone code 107 is Canadaan ....
106 Virgin Islands, British ....
108 Cayman Islands ....
I am a little bit confused
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 12

Expert Comment

by:esoftbg
ID: 11908004
Delphiwizard,
you could use a database where the keys from registry will be a primary key values and you will use another field (not-unique) where to store the real phone-codes ....
0
 

Author Comment

by:Delphiwizard
ID: 11923849
Emil,
Thank you for the great example you mailed me. You are of great help to me.

Stef.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11924577
Stef,
You are welcome !

Emil.
0
 

Author Comment

by:Delphiwizard
ID: 12943468
One addition to the get_country procedure. If someone needs to have the actual international phonecodes (which aren't unique) then they should use the following registry value.

     RegC.ReadInteger('CountryCode');


Usage could be something like this:

          for I := 0 to SLPh.Count -1 do
          begin
            RegistryC.OpenKey(Key_Name+'\'+SLPh.Strings[I], False);
            RegistryC.GetValueNames(SLC);
            If (NOT DM.TLanden.Locate('LandCode', SLPh.Strings[I], [])) then
              DM.TLanden.Append;
            DM.TLanden.Edit;
            DM.TLanden.FieldByName('LandCode').AsString     := SLPh.Strings[I];
            DM.TLanden.FieldByName('TelefoonCode').AsString := IntToStr(RegistryC.ReadInteger('CountryCode'));
            DM.TLanden.FieldByName('Land').AsString         := RegistryC.ReadString('Name');
            DM.TLanden.Post;
            RegistryC.CloseKey;
          end;

Regards, Stef
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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

12 Experts available now in Live!

Get 1:1 Help Now