/// <summary> /// 增加文字到textbox /// </summary> /// <param name="control"></param> /// <param name="text">增加的文字</param> /// <param name="time">是否在前面加上时间</param> public static void AppendText(TextBoxBase control, string text, bool time = false) { if (control.InvokeRequired) { Action <string> action = x => { if (time) { control.AppendText(DateTime.Now.ToString() + " " + x + "\r\n"); } else { control.AppendText(x + "\r\n"); } control.SelectionStart = control.Text.Length; control.SelectionLength = 0; control.Focus(); }; control.Invoke(action, text); } else { if (time) { control.AppendText(DateTime.Now.ToString() + " " + text + "\r\n"); } else { control.AppendText(text + "\r\n"); } control.SelectionStart = control.Text.Length; control.SelectionLength = 0; control.Focus(); } }
public static void Insert(this TextBoxBase txtbox, string text, bool removeSelectedText = true, bool selectNewlyInserted = true) { if (string.IsNullOrEmpty(text)) { return; } var oldSelectStart = txtbox.SelectionStart; var oldSelectLength = txtbox.SelectionLength; if (txtbox.SelectionLength > 0) { if (removeSelectedText) { txtbox.Text = txtbox.Text.Remove(oldSelectStart, oldSelectLength).Insert(oldSelectStart, text); txtbox.Focus(); txtbox.SelectionStart = selectNewlyInserted ? oldSelectStart : oldSelectStart + text.Length; txtbox.SelectionLength = selectNewlyInserted ? text.Length : 0; } else { txtbox.Text = txtbox.Text.Insert(oldSelectStart, text); txtbox.Focus(); txtbox.SelectionStart = selectNewlyInserted ? oldSelectStart : oldSelectStart + text.Length; txtbox.SelectionLength = selectNewlyInserted ? text.Length : 0; } } else { txtbox.Text = txtbox.Text.Insert(oldSelectStart, text); txtbox.Focus(); txtbox.SelectionStart = selectNewlyInserted ? oldSelectStart : oldSelectStart + text.Length; txtbox.SelectionLength = selectNewlyInserted ? text.Length : 0; } }
public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { // If we don't have a control, return if (sender is TextBoxBase) { TextBoxBase control = (TextBoxBase)sender; if ((bool)e.NewValue) { // Focus this control control.Focus(); // Select all text control.SelectAll(); } } if (sender is PasswordBox) { PasswordBox password = (PasswordBox)sender; if ((bool)e.NewValue) { // Focus this control password.Focus(); // Select all text password.SelectAll(); } } }
internal static void FocusOnRightClick(TextBoxBase textBox, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { textBox.Focus(); } }
private static void SetCaretAt(TextBoxBase textBox, int position) { textBox.Focus(); textBox.SelectionStart = position; textBox.SelectionLength = 0; textBox.ScrollToCaret(); }
private void ShowMe(TextBoxBase control) { this.SetAutoCompleteLocation(control, true); this.Visible = true; this.isVisible = true; activeControl.Focus(); }
public void MyShow(List <InteliSenseData> data, List <InteliSenseData> commands = null) { if (!HasData(data, commands)) { Hide(); } else { InitListBox(data, commands); if (Visibility > Visibility.Visible) { Show(); } UpdateLocation(); _editor.Focus(); } }
public static bool ParseInputD(TextBoxBase control, out ulong value) { if (!ulong.TryParse(control.Text, out value)) { control.Focus(); control.SelectAll(); return(false); } return(true); }
public static bool ParseInputH(TextBoxBase control, out ulong value) { if (!ulong.TryParse(control.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value)) { control.Focus(); control.SelectAll(); return(false); } return(true); }
private static void PushCode(TextBoxBase target, string barcode) { target.Focus(); foreach (char Character in barcode) { SendKeys.SendWait(Character.ToString()); } SendKeys.Send("{ENTER}"); }
/// <summary> /// 设置光标位置 /// </summary> /// <param name="p_ctl"></param> private void m_mthSetMousePosition(Control p_ctl) { TextBoxBase txt = p_ctl as TextBoxBase; if (txt != null) { txt.SelectionStart = txt.Text.Length; txt.SelectionLength = 0; txt.Focus(); } }
private void cajaLN_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { if (string.IsNullOrEmpty(caja.Text)) { caja.Text = Texto; caja.ForeColor = Color.Silver; } siguiente.Focus(); } else if (Char.IsDigit(e.KeyChar) || Char.IsLetter(e.KeyChar)) { if (caja.Text.Equals(Texto) || caja.ForeColor == Color.Silver) { caja.Clear(); caja.ForeColor = Color.Black; } } }
protected override void Append(LoggingEvent loggingEvent) { var logInfo = string.Empty; if (_textBox == null) { if (string.IsNullOrEmpty(FormName) || string.IsNullOrEmpty(TextBoxName)) { return; } Form form = Application.OpenForms[FormName]; if (form == null) { return; } TableLayoutPanel layoutPanel = form.Controls[TableLayoutPanelName] as TableLayoutPanel; if (layoutPanel == null) { return; } Panel panel = layoutPanel.Controls[PanelName] as Panel; if (panel == null) { return; } TabControl tabControl = panel.Controls[TabControlName] as TabControl; if (tabControl == null) { return; } TabPage tabPage = tabControl.Controls[TabPageName] as TabPage; if (tabPage == null) { return; } _textBox = tabPage.Controls[TextBoxName] as TextBox; if (_textBox == null) { return; } form.FormClosed += (s, e) => _textBox = null; } _textBox.BeginInvoke((MethodInvoker) delegate { _textBox.AppendText(RenderLoggingEvent(loggingEvent)); _textBox.Focus(); _textBox.Select(_textBox.TextLength, 0); _textBox.ScrollToCaret(); }); }
public static bool FocusWithIndex(this TextBoxBase textBox, int?index) { if (textBox.Focus()) { if (index.HasValue) { textBox.Select(index.Value > textBox.TextLength ? textBox.TextLength : index.Value, 0); } return(true); } return(false); }
/// <summary> /// 验证非空 /// </summary> /// <param name="textBox"></param> /// <param name="message"></param> /// <param name="name"></param> /// <returns></returns> protected bool VerifyRequired(TextBoxBase textBox, Label message = null, string name = null) { if (string.IsNullOrWhiteSpace(textBox.Text)) { if (message != null) { message.ForeColor = Color.Red; message.Text = string.Format("{0}不能为空", name); } textBox.Focus(); return(true); } return(false); }
private void OSKB_Click(object sender, EventArgs e) { if (m_focusTextBox == null) { return; } string sendertag = ((Button)sender).Tag.ToString(), tstr = m_focusTextBox.Text; switch (sendertag) { case "ENTER": m_focusTextBox.Focus(); SendKeys.Send("{TAB}"); break; case "CLEAR": m_focusTextBox.Text = string.Empty; break; case "BACKSPACE": m_focusTextBox.Text = tstr.Length > 0 ? tstr.Substring(0, tstr.Length - 1) : tstr; break; case "CAPSLOCK": m_capsOn = !m_capsOn; this.SuspendLayout(); btn_capslock.BackColor = m_capsOn ? SelectedControlColor : ActionControlColor; for (int i = 97; i <= 122; i++) { tlpAlphaPad.Controls["btn_" + (char)i].Text = ((char)(i - (m_capsOn ? 32 : 0))).ToString(); } btn_bracesleft.Text = ((char)(m_capsOn ? 123 : 91)).ToString(); btn_bracesright.Text = ((char)(m_capsOn ? 125 : 93)).ToString(); this.ResumeLayout(true); break; default: if (m_focusTextBox.SelectionLength == m_focusTextBox.Text.Length) { tstr = string.Empty; } if (tstr.Length < m_focusTextBox.MaxLength) { m_focusTextBox.Text = tstr + ((Button)sender).Text; } break; } }
protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e) { // if event is called from ComboBox itself and not from any of underlying controls // and if textBox is defined in control template if (e.OriginalSource == e.Source && textBox != null) { textBox.Focus(); textBox.SelectAll(); e.Handled = true; } else { base.OnPreviewGotKeyboardFocus(e); } }
private void SendKeyToTextBox(string code) { if (textBoxInput == null) { return; } if (code.Equals("(") || code.Equals(")") || code.Equals("+")) { code = "{" + code + "}"; } textBoxInput.Focus(); SendKeys.Send(code); }
/// <summary> /// Left click handler. We handle left clicks to ensure the text box gets focus, and if /// it already has focus we reshow the keyboard. This means a user can reshow the /// keyboard when the text box already has focus, i.e. they don't have to swap focus to /// another control and then back to the text box. /// </summary> /// <param name="sender">The text box that was clicked on.</param> /// <param name="e">The left mouse click event arguments.</param> private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { TextBoxBase textBox = sender as TextBoxBase; if (null != textBox) { if (textBox.IsKeyboardFocusWithin) { // TODO: Show on-screen keyboard } else { // Ensure focus is set to the text box - the focus handler will then show the // keyboard. textBox.Focus(); e.Handled = true; } } }
/// <summary> /// Repeatedly attempts to focus the text box until focus is achieved. /// </summary> /// <param name="textBox"></param> public static void Activate(this TextBoxBase textBox) { DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(100); timer.Tick += (tmr, args) => { if (textBox.IsKeyboardFocused) { timer.Stop(); timer = null; } textBox.Focus(); }; timer.Start(); }
private bool RegexVerifyValue(string regex, TextBoxBase textBox, Label message = null, string name = null) { if (!string.IsNullOrWhiteSpace(textBox.Text)) { var str = textBox.Text.Trim(); var reg = new Regex(regex); if (!reg.IsMatch(str)) { if (message != null) { message.ForeColor = Color.Red; message.Text = string.Format("{0}格式有误", name); } textBox.Focus(); return(true); } } return(false); }
public override bool Save() { string txt = textBox.Text.Trim(); NumberStyles style = NumberStyles.Integer; if (txt.StartsWith("0x")) { txt = txt.Substring(2); style = NumberStyles.HexNumber; } int val; if (!int.TryParse(txt, style, NumberFormatInfo.InvariantInfo, out val)) { textBox.Focus(); MessageService.ShowMessage("${res:Dialog.ProjectOptions.PleaseEnterValidNumber}"); return(false); } Set(val.ToString(NumberFormatInfo.InvariantInfo)); return(true); }
/// <summary> /// Moves the caret to a specific line of a textbox /// </summary> /// <param name="t"></param> /// <param name="lineNumber"></param> public static void GoToLine(this TextBoxBase t, int lineNumber) { int i = 1; int intStart = 0; int intEnd = 0; foreach (Match m in Regex.Matches(t.Text, "^.*?$", RegexOptions.Multiline)) { if (i == lineNumber) { intStart = m.Index; intEnd = intStart + m.Length; break; } i++; } t.Select(intStart, intEnd - intStart); t.ScrollToCaret(); t.Focus(); }
}//method private bool DoSearch(TextBoxBase textBox, string fragment, int start) { textBox.SelectionLength = 0; // Compile the regular expression. Regex r = new Regex(fragment, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. Match m = r.Match(textBox.Text.Substring(start)); if (m.Success) { int i = 0; Group g = m.Groups[i]; CaptureCollection cc = g.Captures; Capture c = cc[0]; textBox.SelectionStart = c.Index + start; textBox.SelectionLength = c.Length; textBox.Focus(); textBox.ScrollToCaret(); return(true); } return(false); }//method
private static void FocusTextBox(TextBoxBase textBox) { DeferredAction.Create(context => { if (textBox == null || Application.Current == null || Application.Current.MainWindow == null) { return; } textBox.Focus(); textBox.SelectAll(); FocusManager.SetFocusedElement(Application.Current.MainWindow, textBox); Keyboard.Focus(textBox); if (textBox.IsVisible == false || textBox.IsKeyboardFocused) { context?.Dispose(); } else { context?.Defer(TimeSpan.FromSeconds(0.25)); } }).Defer(TimeSpan.FromSeconds(0.25)); }
public virtual bool Validate(int flags, bool setFocusIfNotValid) { ShowErrorIcon(""); if ((flags & (int)ValidatingFlag.Max) == 0) { return(true); } if ((flags & (int)ValidatingFlag.Max_IfEmpty) != 0 && m_textBox.Text.Length == 0) { if ((flags & (int)ValidatingFlag.Beep_IfEmpty) != 0) { NativeMethods.MessageBeep(MessageBoxIcon.Exclamation); } if ((flags & (int)ValidatingFlag.SetValid_IfEmpty) != 0) { UpdateText(); return(true); } if ((flags & (int)ValidatingFlag.ShowIcon_IfEmpty) != 0) { ShowErrorIcon(ErrorMessage); } if ((flags & (int)ValidatingFlag.ShowMessage_IfEmpty) != 0) { ShowErrorMessageBox(ErrorMessage); } if (setFocusIfNotValid) { m_textBox.Focus(); } return(false); } if ((flags & (int)ValidatingFlag.Max_IfInvalid) != 0 && m_textBox.Text.Length != 0 && !IsValid()) { if ((flags & (int)ValidatingFlag.Beep_IfInvalid) != 0) { NativeMethods.MessageBeep(MessageBoxIcon.Exclamation); } if ((flags & (int)ValidatingFlag.SetValid_IfInvalid) != 0) { UpdateText(); return(true); } if ((flags & (int)ValidatingFlag.ShowIcon_IfInvalid) != 0) { ShowErrorIcon(ErrorMessage); } if ((flags & (int)ValidatingFlag.ShowMessage_IfInvalid) != 0) { ShowErrorMessageBox(ErrorMessage); } if (setFocusIfNotValid) { m_textBox.Focus(); } return(false); } return(true); }
public static void FocusAndSelectAll(this TextBoxBase control) { control.Focus(); control.SelectAll(); }
public bool Focus() { return(TextBox.Focus()); }
public static void SetSelect(this TextBoxBase textBox) { textBox.Focus(); textBox.SelectAll(); }
public static void scrollToEnd(TextBoxBase txb) { txb.Focus(); txb.Select(txb.TextLength, 0); }