private void ButtonCreateContact_Click(object sender, EventArgs e) { bool noProblems = true; //Reset the colors of controls that change. TextBoxFullName.BackColor = colorBackground; ComboBoxContactFolder.BackColor = colorBackground; TextBoxEmail1.BackColor = colorBackground; TextBoxEmail2.BackColor = colorBackground; TextBoxEmail3.BackColor = colorBackground; //Check for required fields. if (TextBoxFullName.Text.Length == 0) { TextBoxFullName.BackColor = colorBackgroundError; noProblems = false; } if (ComboBoxContactFolder.Text.Length == 0) { ComboBoxContactFolder.BackColor = colorBackgroundError; noProblems = false; } //Check if there is at least one email. if ((TextBoxEmail1.Text.Length == 0) && (TextBoxEmail1.Text.Length == 0) && (TextBoxEmail1.Text.Length == 0)) { TextBoxEmail1.BackColor = colorBackgroundError; noProblems = false; } //Check the email addresses are already in use. if (TextBoxEmail1.Text.Length > 0) { if (Contacts.DoesLookupContain(TextBoxEmail1.Text)) { TextBoxEmail1.BackColor = colorBackgroundError; noProblems = false; } } if (TextBoxEmail2.Text.Length > 0) { if (Contacts.DoesLookupContain(TextBoxEmail2.Text)) { TextBoxEmail2.BackColor = colorBackgroundError; noProblems = false; } } if (TextBoxEmail3.Text.Length > 0) { if (Contacts.DoesLookupContain(TextBoxEmail3.Text)) { TextBoxEmail3.BackColor = colorBackgroundError; noProblems = false; } } //If all requirement are met then create the contact and add the details. if (noProblems) { Outlook.ContactItem newContact = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olContactItem) as Outlook.ContactItem; newContact.FullName = TextBoxFullName.Text; if (TextBoxEmail1.Text.Length > 0) { newContact.Email1Address = TextBoxEmail1.Text; } if (TextBoxEmail2.Text.Length > 0) { newContact.Email2Address = TextBoxEmail2.Text; } if (TextBoxEmail3.Text.Length > 0) { newContact.Email3Address = TextBoxEmail3.Text; } if (PictureBoxIcon.Image is object) { newContact.AddPicture("Icon.png"); } newContact.Save(); Application.DoEvents(); Thread.Sleep(100); //Op.EmailForCreatedContact = newContact.Email1Address; if (ComboBoxContactFolder.Text != "Contacts") { newContact.Move(InTouch.Contacts.Folders[ComboBoxContactFolder.Text]);; } //Op.NextFormRegion = ContactFormRegion.InTouchSettings; if (newContact is object) { Marshal.ReleaseComObject(newContact); } } if (noProblems) { Close(); } }