private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "") { try { books.Clear(); for (int i = 0; i < data.title.Count; i++) { book bk = new book(); bk.id = i + 1; bk.title = data.title[i]; bk.author = data.author[i]; bk.publisher = data.publisher[i]; bk.location = data.location[i]; bk.subject = data.subject[i]; bk.genre = data.genre[i]; bk.year = data.year[i]; books.Add(bk); } book bk2 = new book(); bk2.id = data.title.Count + 1; bk2.title = textBox1.Text; bk2.author = textBox2.Text; bk2.publisher = textBox3.Text; bk2.location = textBox4.Text; bk2.subject = textBox6.Text; bk2.genre = textBox5.Text; bk2.year = numericUpDown1.Value.ToString(); data.title.Add(textBox1.Text); data.author.Add(textBox2.Text); data.publisher.Add(textBox3.Text); data.location.Add(textBox4.Text); data.subject.Add(textBox6.Text); data.genre.Add(textBox5.Text); data.year.Add(numericUpDown1.Value.ToString()); books.Add(bk2); string json = JsonConvert.SerializeObject(books); File.WriteAllText("books.json", json); MessageBox.Show("Book added successfully"); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox6.Clear(); f1.load(); } catch (Exception ex) { MessageBox.Show("Error " + ex.Message); } } else { MessageBox.Show("Title cannot be empty"); } }
private void button2_Click(object sender, EventArgs e) { try { if (listBox1.SelectedIndex == -1) { MessageBox.Show("Select the item you want remove"); } else { DialogResult dialogResult = MessageBox.Show("Are you sure you want to remove the '" + data.title[listBox1.SelectedIndex] + "'?", "Remove", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { data.id.Remove(listBox1.SelectedIndex); data.title.RemoveAt(listBox1.SelectedIndex); data.author.RemoveAt(listBox1.SelectedIndex); data.publisher.RemoveAt(listBox1.SelectedIndex); data.location.RemoveAt(listBox1.SelectedIndex); data.genre.RemoveAt(listBox1.SelectedIndex); data.subject.RemoveAt(listBox1.SelectedIndex); data.year.RemoveAt(listBox1.SelectedIndex); for (int i = 0; i < data.title.Count; i++) { book bk = new book(); bk.title = data.title[i]; bk.author = data.author[i]; bk.publisher = data.publisher[i]; bk.location = data.location[i]; bk.subject = data.subject[i]; bk.genre = data.genre[i]; bk.year = data.year[i]; books.Add(bk); } string json = JsonConvert.SerializeObject(books); books.Clear(); File.WriteAllText("books.json", json); load(); } } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } }
private void btnaddbook_Click(object sender, EventArgs e) { book book = new book(); mdi.main(this, book); }