private void addBtn_Click(object sender, EventArgs e) { try { Book b = new Book(nameField.Text, subNameField.Text, writerField.Text, subwriterField.Text, translatorField.Text, Int32.Parse(editionField.Text), Int32.Parse(amountField.Text), publisherField.Text, Int32.Parse(publishedYrBox.Text), ISBNField.Text, deweyField.Text, categoryBox.Text); DBCommand.addBook(b); } catch (FormatException e2) { int num = 0; String message = ""; if (Int32.TryParse(amountField.Text, num)) { if (num <= 0) { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } } else { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } if (Int32.TryParse(editionField.Text, num)) { if (num <= 0) { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } } else { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } if (Int32.TryParse(publishedYrBox.Text, num)) { if (num <= 0) { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } } else { message += "\nกรุณากรอกจำนวนเล่มให้ถูกต้อง"; } /* * if (amountField.Text == "") MessageBox.Show("กรุณากรอกจำนวนเล่ม"); * else if(Int32.Parse(amountField.Text) <= 0) MessageBox.Show("กรุณากรอกจำนวนเล่มที่ถูกต้อง"); * else if(editionField.Text == "") MessageBox.Show("กรุณากรอกครั้งที่พิมพ์"); * else if (Int32.Parse(editionField.Text) <= 0) MessageBox.Show("กรุณากรอกครั้งที่พิมพ์ที่ถูกต้อง"); * else if (publishedYrBox.Text == "") MessageBox.Show("กรุณากรอกปีที่พิมพ์"); * else if (Int32.Parse(publishedYrBox.Text) <= 0) MessageBox.Show("กรุณากรอกปีที่พิมพ์ที่ถูกต้อง"); */ } }
private void addBtn_Click(object sender, EventArgs e) { try { Convert.ToInt32(amountField.Text); } catch (FormatException e2) { MessageBox.Show("กรุณากรอกจำนวนเล่มที่ถูกต้อง"); return; } try { if (editionField.Text != "-") { Convert.ToInt32(editionField.Text); } } catch (FormatException e2) { MessageBox.Show("กรุณากรอกครั้งที่พิมพ์ที่ถูกต้อง"); return; } try { if (publishedYrBox.Text != "-") { Convert.ToInt32(publishedYrBox.Text); } } catch (FormatException e2) { MessageBox.Show("กรุณากรอกปีที่พิมพ์ที่ถูกต้อง"); return; } Book b = new Book(nameField.Text, subNameField.Text, writerField.Text, subwriterField.Text, translatorField.Text, editionField.Text, amountField.Text, publisherField.Text, publishedYrBox.Text, ISBNField.Text, deweyField.Text, categoryBox.Text); DBCommand.addBook(b); }