Add line-break after every 7th word while typing in Textbox

Wilfred Eghenedji 326 Reputation points
2022-04-18T16:50:40.677+00:00

I am looking at a situation where when someone is typing in the textbox, line-break would take effect automatically after every 7th word. Please, is this possible!? Thank you.

Developer technologies VB
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-04-18T22:06:25.47+00:00

    Hi
    As I mentioned, this is a much more complex problem than I believe you think.
    Here is some code that is a very oversimplified idea. This code checks for 7 words after each key up event (based on ONLY space characters) and inserts a VbCrLf if so. It works as far as those parameters permit - so if user only types characters and spaces then it may work as you specified.
    To try this code, all you need to do is copy paste into your existing code. If no good easy to remove just be deleting the whole sub. This code references TextBox1, and you will need to edit to suit your code.

      Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Space Then
          Dim seq() As String = TextBox1.Text.Split(" "c)
          If (seq.Count - 1) Mod 7 = 0 Then
            TextBox1.AppendText(vbCrLf)
          End If
        End If
      End Sub
    

0 additional answers

Sort by: Most helpful

Your answer

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