PINEditorControl.cs 3.6 KB

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