?
Solved

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

Posted on 2004-08-25
8
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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