/// <summary> /// 输入与删除当前焦点控件字符 /// </summary> /// <param name="focus"></param> /// <param name="inputChar"></param> /// <param name="flag"></param> public static void inputText(IInputElement focus, string inputChar, string flag) { if (focus == null) { return; } if (focus is TextBox) { TextBox tb = (TextBox)focus; int index = tb.SelectionStart; if (flag == "backspace") { if (index > 0) { tb.Text = tb.Text.Substring(0, index - 1) + tb.Text.Substring(index); tb.SelectionStart = index - 1; } } else if (flag == "del") { tb.Text = ""; } else { tb.Text = tb.Text.Substring(0, index) + inputChar + tb.Text.Substring(index); tb.SelectionStart = index + inputChar.Length; } Keyboard.Focus(tb); return; } if (focus is PasswordBox) { PasswordBox pb = (PasswordBox)focus; var Select = pb.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); var GetSelect = pb.GetType().GetMethod("get_Selection", BindingFlags.Instance | BindingFlags.NonPublic); int index = pb.Password.Length; if (flag == "backspace") { if (index > 0) { pb.Password = pb.Password.Substring(0, index - 1); } } else if (flag == "del") { pb.Password = ""; } else { pb.Password = pb.Password + inputChar; } Keyboard.Focus(pb); Select.Invoke(pb, new object[] { pb.Password.Length, 0 }); return; } }
private static void SetPasswordBoxSelection(PasswordBox passwordBox, int start, int length) { var select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); select.Invoke(passwordBox, new object[] { start, length }); }
/// <summary> /// 密码框光标处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox pwdBox = (PasswordBox)sender; var select = pwdBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); select.Invoke(pwdBox, new object[] { pwdBox.Password.Length + 1, pwdBox.Password.Length + 1 }); }
private static void PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox password = sender as PasswordBox; SetBoundPassword(password, password.Password); // set cursor past the last character in the password box password.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(password, new object[] { password.Password.Length, 0 }); }
private static void PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox password = sender as PasswordBox; SetBoundPassword(password, password?.Password); password?.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic)? .Invoke(password, new object[] { password.Password.Length, 0 }); }
private static void SetSelectionOrCaretPosition(this PasswordBox passwordBox, int start, int length = 0) { if (_select == null) { _select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); } _select.Invoke(passwordBox, new object[] { start, length }); passwordBox.Focus(); }
protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e); if (!IsDisplayPassword && passwordBox != null) { passwordBox.Focus(); passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(passwordBox, new object[] { this.Text.Length, 0 }); } else { this.Focus(); this.Select(this.Text.Length, 0); } }
private void SetSelection(PasswordBox passwordBox, int start, int length) { try { if (start < 0x0) { start = passwordBox.Password.Length; } passwordBox.GetType().GetMethod("Select", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(passwordBox, new object[] { start, length }); } catch (Exception exception) { DebugLog.Assert(false, exception.Message); passwordBox.SelectAll(); } }
private void OnClickExecute(Image sender) { IsShown = !IsShown; OnMouseEnterExecute(sender); if (IsShown) { textBox.Focus(); textBox.SelectionStart = textBox.Text.Length; } else { passwordBox.Focus(); passwordBox.GetType() .GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(passwordBox, new object[] { passwordBox.Password.Length, 0 }); } }
public void TogglePasswordVisibility() { VisiblePassword = !VisiblePassword; if (_textBox == null || _passwordBox == null) { return; } if (_textBox.IsFocused) { _passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(_passwordBox, new object[] { _textBox.SelectionStart, _textBox.SelectionLength }); _passwordBox.Focus(); } else if (_passwordBox.IsFocused) { _textBox.Focus(); } }
public static void SetSelection(this PasswordBox passwordBox, int start, int length) { passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(passwordBox, new object[] { start, length }); }
public DynamicFocusSection() { var content = new Panel(); var focusControlCheckBox = new CheckBox { Text = "Focus Control", Checked = true }; var addContentButton = new Button { Text = "Add Control" }; var count = 0; addContentButton.Click += (sender, e) => { Control control; switch ((count++) % 9) { case 0: control = new TextBox(); break; case 1: control = new TextArea(); break; case 2: control = new CheckBox { Text = "A Check Box" }; break; case 3: control = new RadioButton { Text = "A Radio Button" }; break; case 4: control = new DropDown { Items = { "Item 1", "Item 2", "Item 3" } }; break; case 5: control = new DateTimePicker(); break; case 6: control = new ColorPicker(); break; case 7: control = new PasswordBox(); break; case 8: control = new ListBox { Items = { "Item 1", "Item 2", "Item 3" } }; break; case 9: control = new PasswordBox(); break; default: throw new InvalidOperationException(); } if (focusControlCheckBox.Checked ?? false) { control.Focus(); } content.Content = new TableLayout( null, new Label { Text = string.Format("Control: {0}", control.GetType().Name) }, new TableRow(control), null ); }; Content = new TableLayout( new TableLayout(new TableRow(null, addContentButton, focusControlCheckBox, null)), content ); }