PINEditorControl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.WPF;
  9. namespace InABox.DynamicGrid
  10. {
  11. public class PINEditorControl : DynamicEditorControl<string>
  12. {
  13. private Button ClearPIN;
  14. private Button CreatePIN;
  15. private TextBox Editor;
  16. protected override FrameworkElement CreateEditor()
  17. {
  18. var DockPanel = new DockPanel
  19. {
  20. HorizontalAlignment = HorizontalAlignment.Stretch
  21. };
  22. Editor = new TextBox
  23. {
  24. VerticalAlignment = VerticalAlignment.Stretch,
  25. VerticalContentAlignment = VerticalAlignment.Center,
  26. HorizontalContentAlignment = HorizontalAlignment.Center,
  27. Focusable = false,
  28. IsReadOnly = true,
  29. Background = new SolidColorBrush(Colors.Gainsboro)
  30. };
  31. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  32. Editor.TextChanged += (o, e) => CheckChanged();
  33. CreatePIN = new Button
  34. {
  35. Content = "Create",
  36. Width = 50,
  37. Margin = new Thickness(5, 0, 0, 0),
  38. Focusable = true
  39. };
  40. CreatePIN.SetValue(DockPanel.DockProperty, Dock.Right);
  41. CreatePIN.Click += CreatePINClick;
  42. ClearPIN = new Button
  43. {
  44. Content = "Clear",
  45. Width = 50,
  46. Margin = new Thickness(5, 0, 0, 0),
  47. Focusable = true
  48. };
  49. ClearPIN.SetValue(DockPanel.DockProperty, Dock.Right);
  50. ClearPIN.Click += ClearPINClick;
  51. DockPanel.Children.Add(ClearPIN);
  52. DockPanel.Children.Add(CreatePIN);
  53. DockPanel.Children.Add(Editor);
  54. return DockPanel;
  55. }
  56. private void CreatePINClick(object sender, RoutedEventArgs e)
  57. {
  58. using (var cursor = new WaitCursor())
  59. {
  60. while (true)
  61. {
  62. var random = new Random(DateTime.Now.Millisecond);
  63. var pin = "";
  64. for (int i = 0; i < ClientFactory.PINLength; i++)
  65. pin += random.Next(10).ToString();
  66. var ok = new Client<User>().Load(new Filter<User>(x => x.PIN).IsEqualTo(pin)).FirstOrDefault() == null;
  67. if (ok)
  68. {
  69. Editor.Text = pin;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. private void ClearPINClick(object sender, RoutedEventArgs e)
  76. {
  77. Editor.Text = "";
  78. }
  79. public override int DesiredHeight()
  80. {
  81. return 25;
  82. }
  83. public override int DesiredWidth()
  84. {
  85. return 150 + (int)CreatePIN.Width + (int)ClearPIN.Width + 10;
  86. ;
  87. }
  88. protected override string RetrieveValue()
  89. {
  90. return Editor.Text;
  91. }
  92. protected override void UpdateValue(string value)
  93. {
  94. Editor.Text = value;
  95. }
  96. public override void SetFocus()
  97. {
  98. CreatePIN.Focus();
  99. }
  100. public override void SetColor(Color color)
  101. {
  102. //Editor.Background = new SolidColorBrush(color);
  103. }
  104. }
  105. }