Solved

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

Posted on 2004-08-25
8
404 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

17 Experts available now in Live!

Get 1:1 Help Now