Jump to content
William23668

Adding phone numbers in addressbook

Recommended Posts

Hi

 

I was trying to add phone number in the address book demo like that hard coded but the number was never saved, why is that ?

 

Contact.Phones.AddPhone('Home', '767867679678');

 

procedure TForm1.ActionAddContactExecute(Sender: TObject);
var
  Contact: TAddressBookContact;
  eMails: TContactEmails;
  Photo: TBitmapSurface;
begin
  Contact := AddressBook1.CreateContact(FDefaultSource);
  try
    try
      Contact.FirstName := edtFirstName.Text;
      Contact.LastName := edtLastName.Text;
      Contact.Phones.AddPhone('Home', '767867679678'); //<<<--------
      if not Image1.Bitmap.Size.IsZero then
      begin
        Photo := TBitmapSurface.Create;
        try
          Photo.Assign(Image1.Bitmap);
          Contact.Photo := Photo;
          Image1.Bitmap.SetSize(0, 0);
        finally
          Photo.Free;
        end;
      end;
      // Add the work mail
      eMails := TContactEmails.Create;
      try
        eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkMail.Text);
        Contact.eMails := eMails;
      finally
        eMails.Free;
      end;
      // Save a newly created contact to Address Book
      AddressBook1.SaveContact(Contact);

    except
      on E: EAddressBookException do
        ShowMessage('Cannot create the contact.' + E.Message);
    end;

    // Add the contact to the selected group, if any
    try
      if InRange(ComboBox1.ItemIndex, 0, FGroups.Count - 1) then
        AddressBook1.AddContactIntoGroup
          (FGroups.Items[ComboBox1.ItemIndex], Contact);
    except
      on E: EAddressBookException do
        ShowMessage('Cannot add the newly created contact to group.' + E.Message);
    end;

    ListViewContacts.BeginUpdate;
    try
      AddListViewItem(Contact);
    finally
      ListViewContacts.EndUpdate;
    end;
    TabControl1.ActiveTab := TabItemContacts;
  finally
    Contact.Free;
  end;
  // Clear the Add Contact Form
  ClearAddContactForm;
end;

 

Share this post


Link to post

do you try some like this:

var
  Contact: TAddressBookContact;
  Phones: TContactPhones;
 begin
  Contact := AddressBook1.CreateContact(AddressBook1.DefaultSource);
  try
   // Add a mobile phone number
    Phones := TContactPhones.Create;
    try
      Phones.AddPhone(TContactPhone.TLabelKind.Mobile, '+33-6-46-51-3531');
      Contact.Phones := Phones;
    finally
      Phones.Free;
    end;
  // Save the newly created contact
  AddressBook1.SaveContact(Contact);  
  finally
    Contact.Free;
  end;
end;

 

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

@programmerdelphi2k

I tried like you said but still number was not added. Also sometimes I get stackoverflow erro and external exception 78 error

 

procedure TForm1.ActionAddContactExecute(Sender: TObject);
var
  Contact: TAddressBookContact;
  Phones: TContactPhones;
  eMails: TContactEmails;
  Photo: TBitmapSurface;
begin
  Contact := AddressBook1.CreateContact(FDefaultSource);
  try
    try
      Contact.FirstName := edtFirstName.Text;
      Contact.LastName := edtLastName.Text;

      Phones := TContactPhones.Create;
      try
        Phones.AddPhone(TContactPhone.TLabelKind.Mobile, '767867679678');
        Contact.Phones := Phones;
      finally
        Phones.Free;
      end;

      if not Image1.Bitmap.Size.IsZero then
      begin
        Photo := TBitmapSurface.Create;
        try
          Photo.Assign(Image1.Bitmap);
          Contact.Photo := Photo;
          Image1.Bitmap.SetSize(0, 0);
        finally
          Photo.Free;
        end;
      end;
      // Add the work mail
      eMails := TContactEmails.Create;
      try
        eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkMail.Text);
        Contact.eMails := eMails;
      finally
        eMails.Free;
      end;
      // Save a newly created contact to Address Book
      AddressBook1.SaveContact(Contact);

    except
      on E: EAddressBookException do
        ShowMessage('Cannot create the contact.' + E.Message);
    end;

    // Add the contact to the selected group, if any
    try
      if InRange(ComboBox1.ItemIndex, 0, FGroups.Count - 1) then
        AddressBook1.AddContactIntoGroup
          (FGroups.Items[ComboBox1.ItemIndex], Contact);
    except
      on E: EAddressBookException do
        ShowMessage('Cannot add the newly created contact to group.' + E.Message);
    end;

    ListViewContacts.BeginUpdate;
    try
      AddListViewItem(Contact);
    finally
      ListViewContacts.EndUpdate;
    end;
    TabControl1.ActiveTab := TabItemContacts;
  finally
    Contact.Free;
  end;
  // Clear the Add Contact Form
  ClearAddContactForm;
end;

 

Share this post


Link to post

I think all already selected. The first name and last name is added after I recieve external exception 68 error !! but no phone number

 

 

image.thumb.png.dd3af0e2e59b0f52b286f4558e61c016.png

 

image.thumb.png.d63f9cf9c4c5eff62ebfac140c5af655.png

Share this post


Link to post

It's right! because if you "Free it" your reference on "phones list" would be "null"... provocating a "AV"!

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post
19 hours ago, programmerdelphi2k said:

It's right! because if you "Free it" your reference on "phones list" would be "null"... provocating a "AV"!

Understand now thanks

Edited by William23668

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×