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
Solved

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

Posted on 2004-08-25
8
408 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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