Share via

Advise on algorithm

OSVBNET 1,401 Reputation points
2022-07-26T19:55:41.473+00:00

Hello,
Having a very strict controlled multiline TextBox, using KeyPress to allow alphabets and numbers plus CTRL + C/V and Enter

To control what is pasted I also use TextChanged with a loop from 0 to TextBox.TextLength to evaluate and omit each unwanted character:
For MyLoop = 0 To MessageTextBox.TextLength - 1
Select Case Convert.ToInt32(MessageTextBox.Text.Chars(MyLoop))
Case 32, 45, 46, 48 To 57, 64, 65 To 90, 97 To 122, 13
FileNameFilter = FileNameFilter + MessageTextBox.Text.Chars(MyLoop)
End Select
Next
MessageTextBox.Text = FileNameFilter

Everything is working fine except Enter, which will not survive the TextChanged event check!

Not sure if doing it the best way possible, but how can I also get Enter to work?
Thank you for helping out

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2022-07-27T01:57:52.543+00:00

Hi @OSVBNET ,
If you want to use the Enter key to wrap lines, you need 10 instead of 13.

    Private Sub MessageTextBox_TextChanged(sender As Object, e As EventArgs) Handles MessageTextBox.TextChanged  
        Dim FileNameFilter As String = ""  
        For MyLoop = 0 To MessageTextBox.TextLength - 1  
            Select Case Convert.ToInt32(MessageTextBox.Text.Chars(MyLoop))  
                Case 32, 45, 46, 48 To 57, 64, 65 To 90, 97 To 122, 10  
                    FileNameFilter = FileNameFilter + MessageTextBox.Text.Chars(MyLoop)  
            End Select  
        Next  
        MessageTextBox.Text = FileNameFilter  
        MessageTextBox.SelectionStart = MessageTextBox.TextLength  
    End Sub  

Best Regards.
Jiachen Li

----------

If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.