1
0

FrmBookListMaker.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace BooksManageSystem
  10. {
  11. public partial class FrmBookListMaker : Form
  12. {
  13. public FrmBookListMaker(List<Book> booklist,string frmTitle,string buttonOK)
  14. {
  15. InitializeComponent();
  16. this.Text = frmTitle;
  17. this.btn_OK.Text = buttonOK;
  18. this._listSelector = new List<BookSelector>(20);
  19. this._booklist = booklist;
  20. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  21. initData();
  22. }
  23. List<BookSelector> _listSelector;
  24. List<Book> _booklist;
  25. private void btn_Add_Click(object sender, EventArgs e)
  26. {
  27. AddOne(null);
  28. }
  29. private Color odd = Color.FromArgb(217, 192, 255);
  30. private Color even = Color.FromArgb(151, 165, 249);
  31. void AddOne(Book book)
  32. {
  33. var s = new BookSelector();
  34. s.SetData(_booklist);
  35. _listSelector.Add(s);
  36. if (_listSelector.Count % 2 == 1)
  37. {
  38. s.BackColor = odd;
  39. }
  40. else
  41. {
  42. s.BackColor = even;
  43. }
  44. s.Disposed += s_Disposed;
  45. if (book !=null )
  46. {
  47. s.Book = book;
  48. }
  49. drawOne(s);
  50. }
  51. void s_Disposed(object sender, EventArgs e)
  52. {
  53. _listSelector.Remove(sender as BookSelector);
  54. redrawThis();
  55. }
  56. private void redrawThis()
  57. {
  58. this.panel1.Controls.Clear();
  59. for (int i = 0; i < _listSelector.Count; i++)
  60. {
  61. this.panel1.Controls.Add(_listSelector[i]);
  62. if ((i+1) % 2 == 1)
  63. {
  64. _listSelector[i].BackColor = odd;
  65. }
  66. else
  67. {
  68. _listSelector[i].BackColor = even;
  69. }
  70. _listSelector[i].Location = new Point(0, i * _listSelector[i].Size.Height);
  71. }
  72. }
  73. private void drawOne(BookSelector selec)
  74. {
  75. this.panel1.Controls.Add(selec);
  76. selec.Location = new Point(0, (_listSelector.Count-1) * selec.Size.Height);
  77. this.panel1.Update();
  78. }
  79. private void btn_OK_Click(object sender, EventArgs e)
  80. {
  81. try
  82. {
  83. if (_listSelector.Count==0)
  84. {
  85. MessageBox.Show("列表为空! ","空列表", MessageBoxButtons.OK, MessageBoxIcon.Error);
  86. return;
  87. }
  88. if (textBox1.Text == "")
  89. {
  90. MessageBox.Show("请填写备注信息,以便日后查询","注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
  91. return;
  92. }
  93. this.BookListToOperate = new Dictionary<Book, int>();
  94. foreach (var item in _listSelector)
  95. {
  96. if (item.Book != null)
  97. {
  98. BookListToOperate.Add(item.Book, item.BookCount);
  99. }
  100. }
  101. this.NoteTag = textBox1.Text;
  102. this.OperTime = this.dtp_Op_time.Value;
  103. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  104. }
  105. catch (ArgumentException)
  106. {
  107. MessageBox.Show("清单的书名称不能重复,请通过数量修改清单", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. return;
  109. }
  110. catch(Exception erra)
  111. {
  112. MessageBox.Show(erra.Message);
  113. return;
  114. }
  115. }
  116. public Dictionary<Book,int> BookListToOperate { get; set; }
  117. public string NoteTag { get; set; }
  118. public DateTime OperTime { get; set; }
  119. #region Test2
  120. void initData()
  121. {
  122. this.checkedListBox1.Items.AddRange(_booklist.ToArray());
  123. this.checkedListBox1.DisplayMember = "BookName";
  124. }
  125. #endregion
  126. private void btn_AddChecked_Click(object sender, EventArgs e)
  127. {
  128. this.btn_AddChecked.Text = "请等待...";
  129. this.btn_AddChecked.Enabled = false;
  130. this.checkedListBox1.Visible = false;
  131. Action action = new Action(delegate()
  132. {
  133. foreach (var item in checkedListBox1.CheckedItems)
  134. {
  135. AddOne(item as Book);
  136. }
  137. });
  138. var res = this.Invoke(action);
  139. //MessageBox.Show("请调整购买数量,完成后点击购买","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  140. this.btn_AddChecked.Visible = false;
  141. this.btn_OK.Enabled = true;
  142. }
  143. void AddFinishCallback(IAsyncResult res)
  144. {
  145. //var action = res.AsyncState as Action;
  146. this.Invoke(new Action(delegate() { this.btn_AddChecked.Visible = false; }));
  147. }
  148. private void FrmBookListMaker_SizeChanged(object sender, EventArgs e)
  149. {
  150. this.panel1.Size = new Size(this.Size.Width -this.panel1.Location.X - 25, this.Size.Height - this.panel1.Location.Y - 50 - this.panel3.Height);
  151. this.checkedListBox1.Size = new Size(this.panel1.Width - 25, this.panel1.Height - 5);
  152. }
  153. private void btn_saveList_Click(object sender, EventArgs e)
  154. {
  155. try
  156. {
  157. if (_listSelector.Count == 0)
  158. {
  159. MessageBox.Show("列表为空 ", "空列表", MessageBoxButtons.OK, MessageBoxIcon.Error);
  160. return;
  161. }
  162. if (textBox1.Text == "")
  163. {
  164. MessageBox.Show("请填写备注信息,备注信息是列表的名字", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
  165. return;
  166. }
  167. this.BookListToOperate = new Dictionary<Book, int>();
  168. foreach (var item in _listSelector)
  169. {
  170. if (item.Book != null)
  171. {
  172. BookListToOperate.Add(item.Book, item.BookCount);
  173. }
  174. }
  175. new DBcon().SaveBookList(this.BookListToOperate.Keys.ToList<Book>(), textBox1.Text);
  176. MessageBox.Show("保存成功");
  177. }
  178. catch (Exception err)
  179. {
  180. MessageBox.Show(err.Message);
  181. }
  182. }
  183. private void btn_LoadList_Click(object sender, EventArgs e)
  184. {
  185. var fblv =new FrmBookListViewer();
  186. if (fblv.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  187. {
  188. this.btn_AddChecked.Text = "请等待...";
  189. this.btn_AddChecked.Enabled = false;
  190. this.checkedListBox1.Visible = false;
  191. Action action = new Action(delegate()
  192. {
  193. foreach (var item in fblv.BookList)
  194. {
  195. var q = from Book a in _booklist
  196. where a.BookID == item.BookID
  197. select a;
  198. AddOne(q.First());
  199. }
  200. });
  201. var res = this.Invoke(action);
  202. //MessageBox.Show("请调整购买数量,完成后点击购买","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  203. this.btn_AddChecked.Visible = false;
  204. this.btn_OK.Enabled = true;
  205. }
  206. }
  207. }
  208. }