8 Replies - 6377 Views - Last Post: 09 August 2012 - 07:06 PM Rate Topic: -----

#1 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 193
  • Joined: 10-March 12

insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 10:27 AM

i have a textbox which i use for a text editor and a listbox which i use for a "line numbering" feature, just as Notepad++ has. For every new line that i type in the textbox, a new number is added to the listbox elements, like this:

Posted Image

this, however functions in a proper way only if the wordwrap property of the textbox is set to FALSE, or if i specificaly press ENTER key at the end of the line. If word wrap is TRUE and the caret hits the right edge of the textbox, it goes to the next line, but the listbox is not adding a new item, because it still sees it as a single line! (wordwrap is not adding a "new line" character when moving the caret to the next line). From here, you can imagine, a lot of problems arise, the major error being "index was outside the bounds of the array". What i was wondering was: is there any way to detect when word wrap is adding a new line? Is word wrapping itself containing a "new line" character that could be detected in any way? Is it possible to replace that word wrapping functionality of moving the caret to a new line when a text line is too long with a VbCrLf character? This is the code i use so far for the textbox and the "line numbering" listbox:

    Dim IsMouseSelected As Boolean

    Private Sub doc_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles doc.MouseUp
        If doc.Selectionstart > 0 Then
            ListBox1.SelectedIndex = doc.GetLineFromCharIndex(doc.Selectionstart)
        End If
    End Sub

    Private Sub doc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doc.TextChanged
        If doc.WordWrap = False Then
            SyncLineNumbers()
            If doc.Text = "" Then
                ListBox1.Items.Clear()
                ListBox1.Items.Add("1")
                ListBox1.SelectedIndex = 0
            End If
            If doc.Selectionstart > 0 Then
                ListBox1.SelectedIndex = doc.GetLineFromCharIndex(doc.Selectionstart)
            End If
        End If
    End Sub

    Private Sub SyncLineNumbers()
        If doc.Lines.Count <> ListBox1.Items.Count Then
            Do While doc.Lines.Count > ListBox1.Items.Count
                ListBox1.Items.Add((ListBox1.Items.Count + 1).ToString)
            Loop
            Do While doc.Lines.Count < ListBox1.Items.Count
                ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
            Loop
        End If
    End Sub

    Private Sub ListBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        IsMouseSelected = True
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If doc.WordWrap = False Then
            Dim ss As Integer = 0
            Dim gotoLineNumber As Integer = ListBox1.SelectedIndex + 1  ' put line number to goto in here
            Dim LineIndex As Integer = 0
            Dim i As Integer

            If IsMouseSelected = True Then
                While LineIndex <> gotoLineNumber - 1
                    For i = 0 To Me.doc.Lines(LineIndex).Length + 1
                        ss += 1
                    Next
                    LineIndex += 1
                End While
                If doc.Text <> "" Then
                    Me.doc.Selectionstart = ss
                    doc.SelectionLength = doc.Lines(ListBox1.SelectedIndex).Length
                    doc.Focus()
                    IsMouseSelected = False
                End If
            End If
        End If
    End Sub



thank you for any ideea around this matter!

Is This A Good Question/Topic? 0
  • +

Replies To: insert VBCrLf when word wrapping in multiline textbox

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 284
  • Joined: 22-August 11

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 10:47 AM

I don't think it can detect a new line. You'll have to tell it when to do a new line.
I found this from here.
Hopefully this helps give you an idea on what you can do.
It starts a new line after 100 characters.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oRead As System.IO.StreamReader
Dim EntireFile As String
Dim oFile As System.IO.File
oRead = oFile.OpenText("C:/rentbacs")
EntireFile = oRead.ReadToEnd ' reads contents of file into a variable

Dim sb As New System.Text.StringBuilder(EntireFile) ' creates new stringbuilder instance using Entirefile var
System.Console.WriteLine(sb.Length) ' prints length of your string


'Now loop through the stringbuilder string and use insert command
Dim count As Integer = 0
Dim tempcount As Integer = 0

While count <= sb.Length

If tempcount = 100 Then
sb.Insert(count, vbCrLf)
'reset tempcount, so we start searching for next 100 characters
tempcount = 0
End If

tempcount = tempcount + 1
count = count + 1
End While

'newly edited string
Dim mainString As String = sb.ToString

'Now we write back the new string to the file
Dim fs2 As New FileStream("C:/rentbacs", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim s2 As New StreamWriter(fs2)
'overwriting string to file
s2.Write(mainString)
'closing the file
s2.Close()

End Sub

Was This Post Helpful? 1
  • +
  • -

#3 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 193
  • Joined: 10-March 12

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 10:51 AM

i found that answer myself, but it does NOT help me, because a hundred of "wwww"'s is not the same width as a hundred of "iiiii"... So, because not all the characters have the same width, the lines lenght would be very chaotic...
Was This Post Helpful? 0
  • +
  • -

#4 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 193
  • Joined: 10-March 12

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 11:07 AM

i think you gave me an ideea, torind_2000... if i combine your code with a font whose letters and characters each occupy the same amount of horizontal space (such as Courier), it should work!
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2236
  • View blog
  • Posts: 9,407
  • Joined: 29-May 08

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 11:22 AM

Also use Enviroment.NewLine as some culture don't vbCrLf as the end of line.
Was This Post Helpful? 2
  • +
  • -

#6 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 193
  • Joined: 10-March 12

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 11:23 AM

didnt know about that... noted for further usage! thanks.
Was This Post Helpful? 0
  • +
  • -

#7 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 284
  • Joined: 22-August 11

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 11:30 AM

If you get it working please post it for us to check out, I'd be interested in seeing how you solved it.
Was This Post Helpful? 0
  • +
  • -

#8 chris.whitley  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 39
  • Joined: 26-October 10

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 12:43 PM

I don't know if this will help or not.

I was curious about the issue, so I created a new VB WPF Application. I added a textbox, listbox, and a label.

The properties I set on the text box were:
  • AcceptsReturn = True,
  • Text = Cambria 11pt (I like Cambria)
  • TextWrapping = WrapWithOverFlow


All other settings are default. Settings on Listbox and lable were default as well.

Here is the code I put in (I used your SyncLineNumbers() Method)

Class MainWindow 

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBox1.TextChanged
        Me.lblNumChar.Content = Len(TextBox1.Text)
        SyncLineNumbers()


    End Sub


    Private Sub SyncLineNumbers()
        Do While Me.TextBox1.LineCount <> Me.ListBox1.Items.Count
            Do While Me.TextBox1.LineCount > Me.ListBox1.Items.Count
                ListBox1.Items.Add(ListBox1.Items.Count + 1).ToString
            Loop
            Do While Me.TextBox1.LineCount < Me.ListBox1.Items.Count
                ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
            Loop
        Loop


    End Sub


End Class




Here is the XAML code generated
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="287" HorizontalAlignment="Left" Margin="135,12,0,0" Name="TextBox1" VerticalAlignment="Top" Width="356" FontFamily="Cambria" TextWrapping="WrapWithOverflow" AutoWordSelection="False" AcceptsReturn="True" />
        <ListBox Height="287" HorizontalAlignment="Left" Margin="91,12,0,0" Name="ListBox1" VerticalAlignment="Top" Width="45" FontFamily="Cambria" />
        <Label Height="28" HorizontalAlignment="Left" Margin="19,21,0,0" Name="lblNumChar" VerticalAlignment="Top" Width="40" FontFamily="Cambria" />
    </Grid>
</Window>




When I use this, without pressing enter and allowing the text to overflow to the next line, it successfully updates the listbox to add a new line number.

Posted Image
Was This Post Helpful? 1
  • +
  • -

#9 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 193
  • Joined: 10-March 12

Re: insert VBCrLf when word wrapping in multiline textbox

Posted 09 August 2012 - 07:06 PM

well, i did find a solution, but its working only halfway :)
here is what i did:

    Dim InsertNewLine As Boolean
    Dim NumberOfCharacterPerLine As Integer

    Private Sub doc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doc.TextChanged
        index = doc.Selectionstart
        currentLine = doc.GetLineFromCharIndex(index)
        currentColumn = index - doc.GetFirstCharIndexFromLine(currentLine)
        NumberOfCharacterPerLine = doc.Size.Width / 10.11278195488722 ' finds how many characters fit into a line, 10.11278195488722 is the width of one character, in pixels
        If currentColumn > NumberOfCharacterPerLine - 1 Then
            InsertNewLine = True
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If InsertNewLine = True Then
            doc.Text = doc.Text & Environment.NewLine
            InsertNewLine = False
        End If
    End Sub




however, every time the caret riches the number of characters at which the code will insert a new line, the new line is inserted, but the caret moves to the begining of the document, first line.
that wouldnt be a problem, i could simply make the caret go to the end of the document, but here is the issue... what if the user wrote something long (say, 10 lines) and suddenly decides to edit something at line 5? If the new edit would bring a new line, the carot would not move to its previous position, meaning line 5 + the new line, it would still go to last line. I must find a way to preserve the position of the caret after the text of the textbox is rewritten and then, send the carot to that position...

PS - chris.whitley, it would be a great advice if i would create a WPF application, but im not.. it is a simple WinForm... thanks anyway for the suggestion.

This post has been edited by rusoaica: 09 August 2012 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1