123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using InABox.Clients;
- using InABox.Core;
- using InABox.WPF;
- namespace InABox.DynamicGrid
- {
- public class PINEditorControl : DynamicEditorControl<string>
- {
- private Button ClearPIN;
- private Button CreatePIN;
- private TextBox Editor;
- protected override FrameworkElement CreateEditor()
- {
- var DockPanel = new DockPanel
- {
- HorizontalAlignment = HorizontalAlignment.Stretch
- };
- Editor = new TextBox
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- Focusable = false,
- IsReadOnly = true,
- Background = new SolidColorBrush(Colors.Gainsboro)
- };
- Editor.SetValue(DockPanel.DockProperty, Dock.Left);
- Editor.TextChanged += (o, e) => CheckChanged();
- CreatePIN = new Button
- {
- Content = "Create",
- Width = 50,
- Margin = new Thickness(5, 0, 0, 0),
- Focusable = true
- };
- CreatePIN.SetValue(DockPanel.DockProperty, Dock.Right);
- CreatePIN.Click += CreatePINClick;
- ClearPIN = new Button
- {
- Content = "Clear",
- Width = 50,
- Margin = new Thickness(5, 0, 0, 0),
- Focusable = true
- };
- ClearPIN.SetValue(DockPanel.DockProperty, Dock.Right);
- ClearPIN.Click += ClearPINClick;
- DockPanel.Children.Add(ClearPIN);
- DockPanel.Children.Add(CreatePIN);
- DockPanel.Children.Add(Editor);
- return DockPanel;
- }
- private void CreatePINClick(object sender, RoutedEventArgs e)
- {
- using (var cursor = new WaitCursor())
- {
- while (true)
- {
- var random = new Random(DateTime.Now.Millisecond);
- var pin = "";
- for (int i = 0; i < ClientFactory.PINLength; i++)
- pin += random.Next(10).ToString();
- var ok = new Client<User>().Load(new Filter<User>(x => x.PIN).IsEqualTo(pin)).FirstOrDefault() == null;
- if (ok)
- {
- Editor.Text = pin;
- break;
- }
- }
- }
- }
- private void ClearPINClick(object sender, RoutedEventArgs e)
- {
- Editor.Text = "";
- }
- public override int DesiredHeight()
- {
- return 25;
- }
- public override int DesiredWidth()
- {
- return 150 + (int)CreatePIN.Width + (int)ClearPIN.Width + 10;
- ;
- }
- protected override string RetrieveValue()
- {
- return Editor.Text;
- }
- protected override void UpdateValue(string value)
- {
- Editor.Text = value;
- }
- public override void SetFocus()
- {
- CreatePIN.Focus();
- }
- public override void SetColor(Color color)
- {
- //Editor.Background = new SolidColorBrush(color);
- }
- }
- }
|