BookSelector.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace BooksManageSystem
  10. {
  11. public partial class BookSelector : UserControl
  12. {
  13. List<Book> list;
  14. public BookSelector()
  15. {
  16. InitializeComponent();
  17. this.BackColorChanged += BookSelector_BackColorChanged;
  18. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  19. this.comboBox1.MouseWheel += comboBox1_MouseWheel;
  20. }
  21. int indexTemp;
  22. bool mousewheel;
  23. void comboBox1_MouseWheel(object sender, MouseEventArgs e)
  24. {
  25. if (e.Delta != 0 && !this.comboBox1.DroppedDown)
  26. {
  27. indexTemp = this.comboBox1.SelectedIndex;
  28. this.mousewheel = true;
  29. }
  30. }
  31. void BookSelector_BackColorChanged(object sender, EventArgs e)
  32. {
  33. //this.btn_dispose.BackColor = this.BackColor;
  34. //this.numericUpDown1.BackColor = this.BackColor;
  35. }
  36. public void SetData(List<Book> bookList)
  37. {
  38. this.list = bookList;
  39. this.comboBox1.Items.AddRange(list.ToArray());
  40. this.comboBox1.DisplayMember = "BookName";
  41. this.comboBox1.ValueMember = "BookID";
  42. }
  43. public int BookCount
  44. {
  45. get
  46. {
  47. return (int)this.numericUpDown1.Value;
  48. }
  49. }
  50. public Book Book
  51. {
  52. get
  53. {
  54. return this.comboBox1.SelectedItem as Book;
  55. }
  56. set
  57. {
  58. this.comboBox1.SelectedItem = value;
  59. }
  60. }
  61. private void btn_dispose_Click(object sender, EventArgs e)
  62. {
  63. this.Dispose();
  64. }
  65. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  66. {
  67. if (mousewheel)
  68. {
  69. this.comboBox1.SelectedIndex = indexTemp;
  70. }
  71. }
  72. private void comboBox1_MouseClick(object sender, MouseEventArgs e)
  73. {
  74. this.mousewheel = false;
  75. }
  76. private void comboBox1_MouseDown(object sender, MouseEventArgs e)
  77. {
  78. this.mousewheel = false;
  79. }
  80. }
  81. }