Hanoun Fouad Hanoun Fouad - 4 months ago 19
Vb.net Question

VB.net | Continue writing in a second line of richtextbox

I have a function that converts an int to its actual string value, and it works constantly while I type the number in a textbox and the function converts it into a richtextbox.

The problem is that when it converts, i want to continue writing in a new line in the richtextbox so it can be printed as two lines on a paper.

Here is what i tried to do.

TextBox2.Text = " " & NumberToText(AmountBox.Text)
If TextBox2.Text.Length() > 60 Then
TextBox2.AppendText(Environment.NewLine)
End If


TextBox2 is the name of my richtextbox, and i chose 60 which is the average number of letters i want on my first line.

How can i let it continue converting the same number but on the second line ?

I want the output to be something like:

Four Hundreds Twenty Three Millions Four Hundreds Twenty -line1
Five Thousands Four Hundreds Twelve -line2

Any idea?

Answer

If you don't want to cut words, modify your function like this:

Public Function NumberToText(texto As String) As String
    'Append this to what you are are already doing
    Dim words As String() = rtn.Split(" ") 'rtn is the actual return of your method
    Dim result As String = String.Empty
    Dim line As String = String.Empty

    For Each word As String In words
        If line.Length + word.Length < 59 Then
            line &= " " & word
        Else
            result &= Environment.NewLine & line
            line = word
        End If
    Next
    result &= Environment.NewLine & line

    Return result
End Function

UPDATE

If your function NumberToText has a lot of returns, you can use an intermediate function, and call this instead of the original:

Public Function NumberToTextSplited(texto as string) as string
    Dim rtn as string = NumberToText(texto) 'call the original method
    Dim words As String() = rtn.Split(" ") 
    Dim result As String = String.Empty
    Dim line As String = String.Empty

    For Each word As String In words
        If line.Length + word.Length < 59 Then
            line &= " " & word
        Else
            result &= Environment.NewLine & line
            line = word
        End If
    Next
    result &= Environment.NewLine & line

    Return result
End Fuction