private void ColourListBox_SelectedIndexChanged(object sender, EventArgs e) { if (ColourListBox.SelectedIndex > -1) { //Update the textboxes with the new selected item Colour selectedColour = (Colour)ColourListBox.SelectedItem; ColourIDTextBox.Text = selectedColour.Id.ToString(); ColourNameTextBox.Text = selectedColour.Name; } }
private void SelectColourFromListBox(int colourId) { foreach (var item in ColourListBox.Items) { Colour colours = (Colour)item; if (colours.Id == colourId) { //select the colour as it has been found ColourListBox.SelectedItem = item; return; } } }
private void SaveColourButton_Click(object sender, EventArgs e) { if (ColourIDTextBox.Text == string.Empty) { //Create a new colour entry Colour colours = new Colour(); colours.Name = ColourNameTextBox.Text; //call the method in ColoursMaanger to save the data Colour saveColour = ColoursManager.AddColour(colours); //create a message to indicate it was successful MessageBox.Show("New colour added."); //refresh the list to display the new information List <Colour> coloursList = ColoursManager.GetColourList(); ColourListBox.DataSource = coloursList; //select the new colour from the list SelectColourFromListBox(saveColour.Id); } else { //Update existing colour entry int colourId = int.Parse(ColourIDTextBox.Text); string colourName = ColourNameTextBox.Text; if (ColoursManager.UpdateColour(colourId, colourName) == 1) { //update is successful MessageBox.Show("Colour has been updated."); //Get the list of types, and refresh the list box //Re-select the value in the list box List <Colour> colourList = ColoursManager.GetColourList(); ColourListBox.DataSource = colourList; } else { MessageBox.Show("Colour has NOT been updated."); } } }