private void CountryListBox_SelectedIndexChanged(object sender, EventArgs e) { if (CountryListBox.SelectedIndex > -1) { Country selectedCountry = (Country)CountryListBox.SelectedItem; CountryCodeTextBox.Text = selectedCountry.CountryCode; CountryNameTextBox.Text = selectedCountry.Name; } }
private void SelectCountryFromListBox(string countryCode) { foreach (var item in CountryListBox.Items) { Country countries = (Country)item; if (countries.CountryCode == countryCode) { CountryListBox.SelectedItem = item; return; } } }
private void SaveCountryButton_Click(object sender, EventArgs e) { if (CountryCodeTextBox.Text != string.Empty) { //Create a new country entry Country country = new Country(); country.Name = CountryNameTextBox.Text; //call the method in ColoursMaanger to save the data Country saveCountry = CountryManager.AddCountry(country); //create a message to indicate it was successful MessageBox.Show("A new country has been added."); //refresh the list to display the new information List <Country> countriesList = CountryManager.GetCountryList(); CountryListBox.DataSource = countriesList; //select the new colour from the list SelectCountryFromListBox(saveCountry.CountryCode); } else { //Save changes to an existing item //get the selected country code and the name from the text boxes string countryCode = CountryCodeTextBox.Text; string countryName = CountryNameTextBox.Text; //execute the update from ColoursManager //then check the result //Converted the 1 from an int to a string int num = 1; string stringNum = num.ToString(); if (CountryManager.UpdateCountry(countryCode, countryName) == stringNum) { //create a message to indicate update was successful MessageBox.Show("Country has been Updated"); //refresh the list to show the updated values List <Country> countryList = CountryManager.GetCountryList(); CountryListBox.DataSource = countryList; } else { //create a message to indicate update was NOT successful MessageBox.Show("Country NOT updated."); } } }