FrmAddNewBook.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace BooksManageSystem
  9. {
  10. public partial class FrmAddNewBook : Form
  11. {
  12. Book _book;
  13. int bkID;
  14. public FrmAddNewBook(Book bk)
  15. {
  16. InitializeComponent();
  17. this._book = bk;
  18. }
  19. private void FrmAddNewBook_Load(object sender, EventArgs e)
  20. {
  21. if (this._book != null)
  22. {
  23. this.txt_Bookname.Text = _book.BookName;
  24. this.txt_Count.Text = _book.Count.ToString();
  25. this.txt_Price.Text = _book.Price.ToString();
  26. this.txt_orderPrice.Text = _book.OrderPrice.ToString();
  27. this.cb_IsStuBook.Checked = _book.IsStuBook;
  28. this.bkID = _book.BookID;
  29. this.Text = "修改书籍";
  30. }
  31. else
  32. {
  33. this.txt_Count.Text = "0";
  34. this.txt_Count.Enabled = false;
  35. }
  36. }
  37. private Book book;
  38. private bool checkVaild()
  39. {
  40. double pr, ordpr;
  41. int c;
  42. if (!double.TryParse(txt_Price.Text, out pr))
  43. {
  44. return false;
  45. }
  46. if (!double.TryParse(txt_orderPrice.Text, out ordpr))
  47. {
  48. return false;
  49. }
  50. if (!int.TryParse(txt_Count.Text, out c))
  51. {
  52. return false;
  53. }
  54. Book bk = new Book(999, txt_Bookname.Text.Trim(), pr, ordpr, c, cb_IsStuBook.Checked);
  55. this.book = bk;
  56. return true;
  57. }
  58. private void btn_OK_Click(object sender, EventArgs e)
  59. {
  60. try
  61. {
  62. if (_book == null)
  63. {
  64. add();
  65. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  66. }
  67. else
  68. {
  69. modify();
  70. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  71. }
  72. }
  73. catch (Exception err)
  74. {
  75. MessageBox.Show(err.Message);
  76. }
  77. }
  78. private void add()
  79. {
  80. if (checkVaild())
  81. {
  82. this.btn_OK.Enabled = false;
  83. this.btn_OK.Text = "请稍后...";
  84. this.btn_OK.Update();
  85. new DBcon().AddNewBook(book.BookName, book.Price, book.OrderPrice, book.Count, book.IsStuBook);
  86. this.btn_OK.Enabled = true;
  87. this.btn_OK.Text = "确认";
  88. MessageBox.Show("添加成功!");
  89. }
  90. else
  91. {
  92. MessageBox.Show("输入的数据不正确!");
  93. }
  94. }
  95. private void modify()
  96. {
  97. if (checkVaild())
  98. {
  99. this.btn_OK.Enabled = false;
  100. this.btn_OK.Text = "请稍后...";
  101. this.btn_OK.Update();
  102. new DBcon().ModifyBook(bkID, book.BookName, book.Price, book.OrderPrice, book.Count, book.IsStuBook);
  103. this.btn_OK.Enabled = true;
  104. this.btn_OK.Text = "确认";
  105. MessageBox.Show("修改成功!");
  106. }
  107. else
  108. {
  109. MessageBox.Show("输入的数据不正确!");
  110. }
  111. }
  112. }
  113. }