protected async void addEntryButton_Click(object sender, EventArgs e) { // Clear the list box on the form and validate the input from the form this.resultsListBox.Items.Clear(); string lastName = this.lastNameTextBox.Text.TrimEnd(); string firstName = this.firstNameTextBox.Text.TrimEnd(); string phoneNumber = this.phoneNumberTextBox.Text.TrimEnd(); if (isInputValid(lastName) && isInputValid(firstName) && isInputValid(phoneNumber)) { return; } // Create an instance of the PhoneBookService client and scope it to the service inovocation using (PhoneBookServiceClient client = new PhoneBookServiceClient()) { try { var results = await client.AddEntryAsync(lastName, firstName, phoneNumber); // Add the success message to the list box this.resultsListBox.Items.Add(results); } catch (Exception ex) { // If an exception is thrown and the excpetion's message to the list box on the form this.resultsListBox.Items.Add(ex.Message); } } }
protected async void findButton_Click(object sender, EventArgs e) { // Clear the list box on the form and validate the input from the form this.resultsListBox.Items.Clear(); string lastName = this.findLastNameTextBox.Text.TrimEnd(); if (isInputValid(lastName)) { // if the input is invalid, then simply return and do no work return; } // Create an instance of the PhoneBookService client and scope it to the service inovocation using (PhoneBookServiceClient client = new PhoneBookServiceClient()) { try { // Call the service's GetEntriesAsync method to get the requested entries var results = await client.GetEntriesAsync(lastName); foreach (string result in results) { // Loop over each of the returned contacts and add them to the list box on the form this.resultsListBox.Items.Add(result); } } catch (Exception ex) { // If an exception is thrown and the excpetion's message to the list box on the form this.resultsListBox.Items.Add(ex.Message); } } }