PDA

Click to See Complete Forum and Search --> : [RESOLVED] Retrieve Text from RTB and place in a string array


DeanMc
Jan 27th, 2009, 06:45 AM
Hi guys,

So I have found a way to getting text out of an RTB like so:


Imports System.IO
Imports System.Text

Class Window1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

Dim tr As New TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd)
Dim ms As New MemoryStream
tr.Save(ms, DataFormats.Text)
Dim MyArray As String = ASCIIEncoding.Default.GetString(ms.ToArray())




End Sub
End Class


The issue im having is that the string from the last line only seems to be 1 string I thought it would be an array of strings? It could be my rusty array logic? any idea's?

chris128
Jan 27th, 2009, 06:54 AM
Try this instead:


Dim MyArray() As String = ASCIIEncoding.Default.GetString(ms.ToArray())



Notice the () after MyArray

DeanMc
Jan 27th, 2009, 07:01 AM
I tried that, I get everything after the = highlighted blue and the error 'value of the type string cannot be converted to 1 dimensional Array of string. hmm it seems I may have to pick out each item of the flow document individually!

chris128
Jan 27th, 2009, 07:03 AM
Actually no sorry ignore that, I didnt properly look at what you were doing. Your calling the GetString method right? Well that just returns one string, not an array of strings.
Do you need it to be an array for some reason? Like an item in the array for every line in the RTB? If you wanted to get separate lines in the RTB you could do this:

Dim MyArray() As String = ASCIIEncoding.Default.GetString(ms.ToArray).Split(vbNewLine)

Obviously that wont count lines that are just wordwrapped onto the next line, it will just split the text using the new line character

chris128
Jan 27th, 2009, 07:10 AM
Just seen your last post, I'm afraid I dont know much about FlowDocuments yet so probably cant help, but if you can explain exactly what you have and what your doing I can try :)

DeanMc
Jan 27th, 2009, 07:10 AM
That's exactly what I need your a legend chris, wow my VB is very rusty. As to why I need it well processing Xaml for highlighting is easier done on a string by string basis.

chris128
Jan 27th, 2009, 07:11 AM
haha no worries :D
Now be a good boy and mark the thread as resolved :P

DeanMc
Jan 27th, 2009, 07:27 AM
Done, I dont suppose you want to learn about regex and then do all my expression for me? no, oh ok :(

chris128
Jan 27th, 2009, 07:41 AM
Nah I hate regex!

Well I like it, but I'm no good at it :)