123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- namespace System.Windows.Forms
- {
- public class TextBox : Control
- {
- private System.Windows.Controls.TextBox textBox;
- private System.Windows.Controls.PasswordBox passwordBox;
- protected new System.Windows.Controls.ContentControl control { get; }
- private bool IsPassword => control.Content == passwordBox;
- private System.Windows.Controls.Control InnerControl => control.Content as System.Windows.Controls.Control;
- internal bool IsFocused => InnerControl.IsFocused;
- internal bool IsMouseOver => InnerControl.IsMouseOver;
- public override string Text
- {
- get
- {
- if (IsPassword)
- return passwordBox.Password;
- else
- {
- if (Multiline)
- return textBox.Text;
- return textBox.Text.Replace(((char)1).ToString(), "\r\n");
- }
- }
- set
- {
- if (IsPassword)
- passwordBox.Password = value;
- else
- {
- if (Multiline)
- textBox.Text = value;
- else
- textBox.Text = string.IsNullOrEmpty(value) ? value : value.Replace("\r\n", ((char)1).ToString());
- }
- }
- }
- public int MaxLength
- {
- get => textBox.MaxLength;
- set => textBox.MaxLength = value;
- }
- private bool multiline;
- public bool Multiline
- {
- get => multiline;
- set
- {
- multiline = value;
- WordWrap = value ? WordWrap : false;
- textBox.AcceptsReturn = value || AcceptsReturn;
- }
- }
- public bool AcceptsReturn
- {
- get => textBox.AcceptsReturn;
- set => textBox.AcceptsReturn = value;
- }
- public bool AcceptsTab
- {
- get => textBox.AcceptsTab;
- set => textBox.AcceptsTab = value;
- }
- public bool ReadOnly
- {
- get => textBox.IsReadOnly;
- set => textBox.IsReadOnly = value;
- }
- public bool Modified { get; set; }
- public int SelectionStart
- {
- get => textBox.SelectionStart;
- set => textBox.SelectionStart = value;
- }
- public int SelectionLength
- {
- get => textBox.SelectionLength;
- set => textBox.SelectionLength = value;
- }
- public string SelectedText
- {
- get => textBox.SelectedText;
- set => textBox.SelectedText = value;
- }
- private bool wordWrap;
- public bool WordWrap
- {
- get => wordWrap;
- set
- {
- wordWrap = value;
- if (Multiline)
- textBox.TextWrapping = value ? TextWrapping.Wrap : TextWrapping.NoWrap;
- }
- }
- private ScrollBars scrollBars;
- public ScrollBars ScrollBars
- {
- get => scrollBars;
- set
- {
- scrollBars = value;
- if (value == ScrollBars.None)
- {
- textBox.HorizontalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Hidden;
- textBox.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Hidden;
- }
- else if (value == ScrollBars.Horizontal)
- {
- textBox.HorizontalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible;
- textBox.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Hidden;
- }
- else if (value == ScrollBars.Vertical)
- {
- textBox.HorizontalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Hidden;
- textBox.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible;
- }
- else if (value == ScrollBars.Both)
- {
- textBox.HorizontalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible;
- textBox.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible;
- }
- }
- }
- private char passwordChar;
- public char PasswordChar
- {
- get => passwordChar;
- set
- {
- passwordChar = value;
- control.Content = value != 0 ? passwordBox : textBox;
- }
- }
- public bool UseSystemPasswordChar
- {
- get => PasswordChar == '*';
- set => PasswordChar = value ? '*' : '\0';
- }
- private HorizontalAlignment textAlign;
- public HorizontalAlignment TextAlign
- {
- get => textAlign;
- set
- {
- textAlign = value;
- textBox.HorizontalContentAlignment = value switch
- {
- HorizontalAlignment.Center => Windows.HorizontalAlignment.Center,
- HorizontalAlignment.Right => Windows.HorizontalAlignment.Right,
- _ => Windows.HorizontalAlignment.Left
- };
- }
- }
- public override BorderStyle BorderStyle
- {
- get => textBox.BorderThickness.Left == 0 ? BorderStyle.None : BorderStyle.FixedSingle;
- set
- {
- textBox.BorderThickness = passwordBox.BorderThickness = new Thickness(value == BorderStyle.None ? 0 : 1);
- if (value == BorderStyle.None)
- {
- textBox.Padding = passwordBox.Padding = new Thickness(0);
- }
- }
- }
- public bool HideSelection
- {
- get => !textBox.IsInactiveSelectionHighlightEnabled;
- set => textBox.IsInactiveSelectionHighlightEnabled = !value;
- }
- private CharacterCasing characterCasing;
- public CharacterCasing CharacterCasing
- {
- get => characterCasing;
- set
- {
- characterCasing = value;
- textBox.CharacterCasing = value switch
- {
- CharacterCasing.Upper => Windows.Controls.CharacterCasing.Upper,
- CharacterCasing.Lower => Windows.Controls.CharacterCasing.Lower,
- _ => Windows.Controls.CharacterCasing.Normal
- };
- }
- }
- public bool CanUndo => textBox.CanUndo;
- public string[] Lines
- {
- get => Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
- set => Text = String.Join(Environment.NewLine, value);
- }
- public override Padding Padding
- {
- get => Helper.ThicknessToPadding(textBox.Padding);
- set => textBox.Padding = passwordBox.Padding = Helper.PaddingToThickness(value);
- }
- public override Drawing.Size PreferredSize => new Drawing.Size(Width, (int)((BorderStyle == BorderStyle.None ? 16 : 20) * DpiScale));
- protected override void SetControlHeight(int value)
- {
- if (multiline)
- base.SetControlHeight(value);
- }
- public int GetCharIndexFromPosition(System.Drawing.Point pos) =>
- textBox.GetCharacterIndexFromPoint(new System.Windows.Point(pos.X / DpiScale, pos.Y / DpiScale), true);
- public void Select(int start, int length) => textBox.Select(start, length);
- public void SelectAll() => textBox.SelectAll();
- public void Copy() => textBox.Copy();
- public void Cut() => textBox.Cut();
- public void Paste() => textBox.Paste();
- public void Undo() => textBox.Undo();
- public new void Focus()
- {
- if (control.IsLoaded)
- InnerControl.Focus();
- else
- control.Dispatcher.InvokeAsync(() => InnerControl.Focus(), Threading.DispatcherPriority.Loaded);
- }
- public TextBox()
- {
- textBox = new() { Tag = this };
- passwordBox = new() { Tag = this };
- textBox.Padding = passwordBox.Padding = new Thickness(0, 1, 0, 2);
- control = new();
- SetControl(control);
- control.Content = textBox;
- Width = 100;
- Height = 22;
- WordWrap = true;
- textBox.TextChanged += (sender, e) =>
- {
- Modified = true;
- OnTextChanged(e);
- };
- // WPF TextBox has issues with regular DragOver & Drop events
- control.PreviewDragOver += (sender, e) =>
- {
- OnDragOver(Helper.GetDragEventArgs(control, e));
- e.Handled = true;
- };
- control.PreviewDrop += (sender, e) =>
- {
- OnDragDrop(Helper.GetDragEventArgs(control, e));
- e.Handled = true;
- };
- // this control needs its own logic of DoubleClick (see Control.cs for explanation)
- control.MouseDoubleClick += (sender, e) => OnDoubleClick(e);
- // SWF consistency: some text boxes handle Enter&Esc in their KeyPress handler
- control.PreviewKeyDown += (sender, e) =>
- {
- if (e.Key == Input.Key.Enter || e.Key == Input.Key.Escape)
- {
- var args = new KeyPressEventArgs(e.Key == Input.Key.Enter ? (char)13 : (char)27);
- OnKeyPress(args);
- e.Handled = args.Handled;
- }
- };
- }
- }
- }
|