private bool AddToClasses(FullSailClass course) { ListViewItem item = new ListViewItem(); item.Text = course.ToString(); item.Tag = course; switch (course.Track) { case "Central": item.ImageIndex = 0; break; case "iOS": item.ImageIndex = 1; break; case "Android": item.ImageIndex = 2; break; default: break; } lsv_Classes.Items.Add(item); return(true); }
//------------------------------------------------------------------------------------ //------------------------------------BUTTON CLICKS----------------------------------- // button handles edit/save edit/save private void btn_Edit_Click(object sender, EventArgs e) { if (btn_Edit.Text == "Save Edit") { // create a new object to hold the new data FullSailClass updatedCourse = (FullSailClass)lsv_Classes.SelectedItems[0].Tag; ValidatesTextInputs(updatedCourse); updatedCourse.Term = int.Parse(nud_Term.Value.ToString()); updatedCourse.CreditHours = double.Parse(nud_Hours.Value.ToString()); updatedCourse.Track = cbx_Track.Text; try { // update the database UpdateData(updatedCourse); // equal the new data to the selected items lsv_Classes.SelectedItems[0].Text = updatedCourse.ToString(); lsv_Classes.SelectedItems[0].Tag = updatedCourse; // deactivate fields txt_CName.Enabled = false; txt_CNumber.Enabled = false; nud_Term.Enabled = false; nud_Hours.Enabled = false; cbx_Track.Enabled = false; // change buttons to origional purpose btn_Edit.Text = "Edit"; btn_Delete.Text = "Delete"; btn_NewCourse.Enabled = true; } catch (Exception) { MessageBox.Show("Do Not Leave Blank"); } } else if (btn_Edit.Text == "Add") { try { // add new info to a new FullSailClass object FullSailClass course = new FullSailClass(); ValidatesTextInputs(course); course.Term = (int)nud_Term.Value; course.CreditHours = (double)nud_Hours.Value; course.Track = cbx_Track.Text; // gets the last added item from database and increments its ID for new item FullSailClass temp = (FullSailClass)lsv_Classes.Items[lsv_Classes.Items.Count - 1].Tag; course.ID = temp.ID + 1; // add to the list AddToClasses(course); // add to database string stm = "INSERT INTO Classes(ID, CourseName, CourseNumber, Term, CreditHours, Track) " + "VALUES(@itemId, @cName, @cNumber, @term, @cHours, @track)"; MySqlCommand cmd = new MySqlCommand(stm, conn); cmd.Parameters.AddWithValue("@itemId", course.ID); cmd.Parameters.AddWithValue("@cName", course.CourseName); cmd.Parameters.AddWithValue("@cNumber", course.CourseNumber); cmd.Parameters.AddWithValue("@term", course.Term); cmd.Parameters.AddWithValue("@cHours", course.CreditHours); cmd.Parameters.AddWithValue("@track", course.Track); MySqlDataReader rdr = cmd.ExecuteReader(); rdr.Close(); // change buttons to origional purpose btn_Edit.Text = "Edit"; btn_Delete.Text = "Delete"; } catch (Exception) { MessageBox.Show("Do Not Leave Blank"); } } else { // enables changes txt_CName.Enabled = true; txt_CNumber.Enabled = true; nud_Term.Enabled = true; nud_Hours.Enabled = true; cbx_Track.Enabled = true; // change names of buttons to change the functionality btn_Edit.Text = "Save Edit"; btn_Delete.Text = "Cancel"; } }