For a game I am trying to make a textbox that will have a text similar to "0 / 100". I would like that text to:
Always persist, ie clicking the textbox to NOT make the text disappear. I know I can create event handlers for OnFocus/LoseFocus, but that still makes the text disappear.
The user be able to only enter values in the left portion of the text. Ie I would like the user to only modify the "XXX" portion in this example: "XXX / 100".
I found this question, but it is for CSS.
Also, I am aware I could make something look like what I want, but I would prefer to do that only if what I want can't be achieved with only a textbox.
You can use a RichTextBox control to accomplish that:
var rtb = new RichTextBox();
rtb.ShortcutsEnabled = false;
rtb.Location = new Point(16, 16);
rtb.Size = new Size(96, 23);
rtb.Multiline = false;
rtb.Text = " / 100";
rtb.SelectAll();
rtb.SelectionProtected = true;
rtb.Select(0, 0);
rtb.KeyPress += rtb_KeyPress;
this.Controls.Add(rtb);
void rtb_KeyPress(object sender, KeyPressEventArgs e) {
RichTextBox rtb = sender as RichTextBox;
if (rtb.SelectionStart == rtb.TextLength) {
e.Handled = true;
} else if (!Char.IsDigit(e.KeyChar)) {
e.Handled = true;
} else if (rtb.TextLength == 9) {
if (rtb.SelectionLength > 0) {
if (rtb.SelectionStart > 3) {
e.Handled = true;
}
} else {
e.Handled = true;
}
} else if (rtb.SelectionStart == 0 && rtb.SelectionLength == 0) {
rtb.SelectionProtected = false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.