none
textbox RRS feed

  • Question

  • So you know how in cmd the text (except for what you type before you press enter) can't be backspaced I would like to do this with a Textbox but I don't know how. please help.

    Donny

    Sunday, September 17, 2017 11:46 PM

Answers

  • Hello Donny,

    As I could understand, what you want is to protect the previously entered text soon after pressing "Enter" key. For this purpose, I suggest you to use a Rich Text box control instead of a simple text box control. Open a new project and add a rich text box control to your project and try below. This will function same as MS command prompt.

    Public Class Form1
    
    
        Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
    
                RichTextBox1.Select(0, Len(RichTextBox1.Text))
                RichTextBox1.SelectionProtected = True
                RichTextBox1.Select(Len(RichTextBox1.Text), Len(RichTextBox1.Text))
            End If
        End Sub
    End Class


    Monday, September 18, 2017 7:50 AM

All replies

  • Hi

    This shows how to supress both BackSpace, Delete, Ctr-V and Ctr-X in a TextBox (TextBox3 in this example - change as needed). NOTE: this does not prevent Paste into the TextBox using the Contexct Menu. If you want to stop Copy/Paste from the Context Menu then set the TextBox Property 'ShortcutsEnabled' to False.

        Private Sub TextBox3_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox3.KeyDown
            Select Case e.KeyData
                ' suppresses backspace, delete, Ctl-V, Ctl-X
                Case Keys.Back, Keys.Delete, Keys.Control Or Keys.V, Keys.Control Or Keys.X
                    e.Handled = True
                    e.SuppressKeyPress = True
            End Select
        End Sub


    Regards Les, Livingston, Scotland




    Monday, September 18, 2017 12:03 AM
  • Hi Donny,

    According to your description, you want to prevent backspace key in the TextBox, am I right? If yes, please take a look the following code:

    Dim m_TextBeforeTheChange As String
        Dim m_CursorPosition As Integer = 0
        Dim m_BackPressed As Boolean = False
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            m_TextBeforeTheChange = TextBox1.Text
            m_BackPressed = If((e.KeyData.Equals(Keys.Back)), True, False)
    
        End Sub
    
        Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
            TextBox1.SelectionStart = If((m_BackPressed), m_CursorPosition + 1, TextBox1.SelectionStart)
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            If m_BackPressed Then
                m_CursorPosition = TextBox1.SelectionStart
                TextBox1.Text = m_TextBeforeTheChange
            End If
        End Sub

    The Background key is disable, but you can use Delete key to delete something.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 18, 2017 5:32 AM
    Moderator
  • Hello Donny,

    As I could understand, what you want is to protect the previously entered text soon after pressing "Enter" key. For this purpose, I suggest you to use a Rich Text box control instead of a simple text box control. Open a new project and add a rich text box control to your project and try below. This will function same as MS command prompt.

    Public Class Form1
    
    
        Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
    
                RichTextBox1.Select(0, Len(RichTextBox1.Text))
                RichTextBox1.SelectionProtected = True
                RichTextBox1.Select(Len(RichTextBox1.Text), Len(RichTextBox1.Text))
            End If
        End Sub
    End Class


    Monday, September 18, 2017 7:50 AM