Is it possible to make a text field in a Windows forms application work in "rewritable mode", i.e. replace characters when user types instead of adding?Otherwise, is there a standard way to get this behavior?
Try using MaskedTextBox and set InsertKeyMode to InsertKeyMode.Overwrite.
MaskedTextBox box = ...; box.InsertKeyMode = InsertKeyMode.Overwrite;
The standard way would be to select existing text when you land in a text field, and then when the user enters it, it will automatically replace the existing text
Masked, KeyPress.
private void Box_KeyPress(object sender, KeyPressEventArgs e) { TextBox Box = (sender as TextBox); if (Box.SelectionStart < Box.TextLength && !Char.IsControl(e.KeyChar)) { int CacheSelectionStart = Box.SelectionStart; //Cache SelectionStart as its reset when the Text property of the TextBox is set. StringBuilder sb = new StringBuilder(Box.Text); //Create a StringBuilder as Strings are immutable sb[Box.SelectionStart] = e.KeyChar; //Add the pressed key at the right position Box.Text = sb.ToString(); //SelectionStart is reset after setting the text, so restore it Box.SelectionStart = CacheSelectionStart + 1; //Advance to the next char } }
. , e.Handled Keypress, . ( VB), : -
Private Sub txtScreen_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtScreen.KeyPress If txtScreen.SelectionStart < txtScreen.TextLength AndAlso Not [Char].IsControl(e.KeyChar) Then Dim SaveSelectionStart As Integer = txtScreen.SelectionStart Dim sb As New StringBuilder(txtScreen.Text) sb(txtScreen.SelectionStart) = e.KeyChar 'Add the pressed key at the right position txtScreen.Text = sb.ToString() 'SelectionStart is reset after setting the text, so restore it 'Advance to the next char txtScreen.SelectionStart = SaveSelectionStart + 1 e.Handled = True End If End Sub
RichTextBox TextBox.
RichTextBox
TextBox
: https://social.msdn.microsoft.com/Forums/en-US/966c3af9-6674-4f48-b487-7afbef05f0cb/overwrite-mode-in-a-textbox
, KeyPress , , - , KeyPress, , Windows, , , . If, , , , :
If tb.SelectionStart < tb.TextLength AndAlso Not [Char].IsControl(e.KeyChar) Then tb.SelectedText = "" End If
, , ,
Sal
Source: https://habr.com/ru/post/1717738/More articles:Erlang erlIDE: which -compile options are supported? - compiler-constructionCreate a G3 facsimile image with ASN.1 wrapper for ldap photo - c #ParseTree will not work with ruby 1.9. What for? - ruby | fooobar.comOracle Duration Function - sqlКак переопределить курсор: текст в меню asp: для элементов без кликов? - asp.netCross-language coding standards? - javascriptBitwise operations: online resources? - bit-manipulationPhp.ini Due to Ajax loading slowly? - performanceStop my program and go to Debugger without setting breakpoints (Visual Studio / GCC and C ++) - debuggingMac OSX system menu name with Java? - javaAll Articles