0

I wrote some code to check when the user inputs a new line and then deletes the line. All of that is happening on the TextChanged event on a Textbox control and the WordWarp property set to True.

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = TextBox1.Text.Replace(vbCr, "")
        TextBox1.Text = TextBox1.Text.Replace(vbLf, "")
    End Sub

The code kinda works, problem is that when the user inputs a new line on my Textbox control the text cursor gets sent back to the first character.

Is there any solution to this?

codiee
  • 31
  • 4
  • To create a new line, a User needs to press `Enter`. So, don't allow that `Key` to reach the Control. – Jimi May 02 '21 at 18:08
  • @Jimi but what if the user pastes some text with line breaks? – codiee May 02 '21 at 18:28
  • See proposed duplicate for the literal answer to your question. I.e. just set the caret position to the end of the text after the operations that put it somewhere else. If you need something different, please improve the question by including a proper [mcve] along with a detailed explanation of what that code does, how exactly that's different from what you want, and what _specifically_ you need help with. – Peter Duniho May 02 '21 at 18:45
  • Then build a Custom Control that intercepts `WM_PASTE` and strips line feeds from the Text, or simply doesn't allow to paste anything (if acceptable in your context). – Jimi May 02 '21 at 18:53
  • Actually I can check when Ctrl+V is pressed then put the clipboard text data on to a string called oldClipboard, check if the clipboard contains line breaks and new lines and if it contains remove then and set that as the clipboard text, then paste to the textbox with our modified clipboard and when pasting is done change the clipboard text data to string oldClipboard – codiee May 02 '21 at 18:57
  • Does this answer your question? [Position the caret in a MaskedTextbox](https://stackoverflow.com/questions/119284/position-the-caret-in-a-maskedtextbox) – Peter Duniho May 02 '21 at 19:02
  • @Jimi: all due respect, you are absolutely wrong about that. As I already explained, the technique for moving the caret is the same for any `TextBoxBase`...that the question presents itself in the context of `MaskedTextbox` is completely irrelevant. – Peter Duniho May 02 '21 at 19:04
  • Nope, you cannot filter `Ctrl+V`, a User can use `SHIFT+INS` or the ContextMenu. You need to filter `WM_PASTE`. Don't go on a hunt to find alternatives because you'll just complicate your code for no good reason. – Jimi May 02 '21 at 19:13
  • So, do what the answer suggests, to prevent the `Enter` Key from reaching the Control (you can do the same thing overriding `OnKeyDown`), then filter `WM_PASTE` overriding `WndProc`, so you can remove line feeds from the text that's been placed on the ClipBoard before it's inserted. Using a Custom Control, you'll also have a better grip on what an User is allowed to do and add more strict behaviors. -- After that, the position of the Caret is irrelevant (it's irrelevant anyway, since this is not your *problem*). – Jimi May 02 '21 at 19:55

0 Answers0