WinForms NumericUpDown allows us to select a range of the text inside it using the Select(Int32, Int32) method. Is there any way to set/retrieve the starting point of text selected, the number of characters selected and the selected part of text like we can do that for other textbox-like controls using the SelectionStart, SelectionLength and SelectedText properties?
The NumericUpDown control has an internal TextBox accessible from the controls collection. It's the second control in the collection following the UpDownButtons control. Since WinForms isn't under development any longer, it's practically safe to say the underlying architecture of the NumericUpDown control isn't going to change.
By inheriting from the NumericUpDown control, you can easily expose those TextBox properties:
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; }
}
}