WinForms NumericUpDown:有没有办法阅读有关文本选择的信息?

Tec*_*Man 2 numericupdown selection textselection winforms

WinForms NumericUpDown 允许我们使用 Select(Int32, Int32) 方法选择其中的文本范围。有什么方法可以设置/检索选定文本的起点、选定的字符数和选定的文本部分,就像我们可以使用 SelectionStart、SelectionLength 和 SelectedText 属性对其他类似文本框的控件执行此操作一样吗?

Lar*_*ech 5

NumericUpDown 控件具有可从控件集合访问的内部 TextBox。它是集合中继 UpDownButtons 控件之后的第二个控件。由于 WinForms 不再处于开发阶段,因此可以肯定地说 NumericUpDown 控件的底层架构不会改变。

通过继承 NumericUpDown 控件,您可以轻松公开这些 TextBox 属性:

public class NumBox : NumericUpDown {
  private TextBox textBox;

  public NumBox() {
    textBox = this.Controls[1] as TextBox;
  }

  public int SelectionStart {
    get { return textBox.SelectionStart; }
    set { textBox.SelectionStart = value; }
  }

  public int SelectionLength {
    get { return textBox.SelectionLength; }
    set { textBox.SelectionLength = value; }
  }

  public string SelectedText {
    get { return textBox.SelectedText; }
    set { textBox.SelectedText = value; }
  }
}
Run Code Online (Sandbox Code Playgroud)