Please help, Multiline string? [urgent]

Senoska

Member
Joined
Jun 27, 2006
Messages
15
Programming Experience
Beginner
Sorry I dont really know how to explaine what im doing and I know my code is really sloppy, this is the first "real" program I have ever made, Ill try my best to explaine but I dont know very much about coding so its going to come off as a bit 'wack'.

First off background of what im trying to do[i dunno if this will help]
I need to make it so when someone wants to make a comment about someone then can click "Comment" and type in what they need and it will write it to an XML file, I already have all of XML stuff down. But I need a multi line string for example:

VB.NET:
Member is skipping manditory events

Recently has been harassing other members

Other random line here

Random like here as well, lol

Something like that in 1 text box, but when I save it it will make it all one line[itll looke like]:

VB.NET:
Member is skipping manditory eventsRecently has been harassing other membersOther random line hereRandom like here as well, lol
how can I fix this?

I made a class for all of my strings and whatnot to seriolise them [sorry im bad at spelling] so my CURRENT code is this:

VB.NET:
Private Sub EditComments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditComments.Click
        If idEditing = -1 Then
            On Error Resume Next
        Else
            personEditing.Comments = txtComments.Text
        End If
        txtComments.ReadOnly = True
        SaveList()
        ReadList()
    End Sub

and it pushs it all into one line, I need it so itll show exactly how it was typed[see above] please help me out sorry I really really need to get this finished like in 2-3 hours and im totaly behind what I should be becasue of this one thing. Please I dont mean to rush but help me its urgent ;; Thank you very much :)
-Senoska :p
 
you can set a property for the textbox "Multiline" to true to let the user type multiple lines in..

and you could do
VB.NET:
textbox.text.replace(vbnewline," ")
which will just replace a new line with a space when you want to process it
 
When you save a multiline TextBoxs Text property to a Xml node using its innerText property the linefeeds are preserved, also when retrieving the data later. Here is a basic example, add to form a multiline TextBox, a Button, a ComboBox and the code below:
VB.NET:
Dim filename As String = Application.StartupPath & "\data.xml"
Dim xmlDoc As New Xml.XmlDocument

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    If IO.File.Exists(filename) = True Then
        'load existing
        xmlDoc.Load(filename)
        'update combobox
        listitems()
    Else
        'create new
        Dim decl As Xml.XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "iso-8859-1", Nothing)
        xmlDoc.AppendChild(decl)
        Dim elm As Xml.XmlNode = xmlDoc.CreateNode(Xml.XmlNodeType.Element, "", "myroot", "")
        xmlDoc.AppendChild(elm)
    End If
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
    xmlDoc.Save(filename)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    Dim newElement As Xml.XmlNode = xmlDoc.CreateElement("text")
    newElement.InnerText = TextBox1.Text
    xmlDoc.DocumentElement.AppendChild(newElement)
    'update combobox
    listitems()
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
    Dim nodes As Xml.XmlNodeList = xmlDoc.DocumentElement.SelectNodes("text")
    If nodes.Count > 0 And ComboBox1.SelectedIndex <> -1 Then
        TextBox1.Text = nodes(ComboBox1.SelectedIndex).InnerText
    Else
        TextBox1.Text = "no node to display"
    End If
End Sub

Private Sub listitems()
    ComboBox1.Items.Clear()
    For i As Integer = 0 To xmlDoc.DocumentElement.SelectNodes("text").Count - 1
        ComboBox1.Items.Add(i.ToString)
    Next
    If ComboBox1.Items.Count > 0 Then ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1
End Sub
Try it out and see how it works, ask again if there something about this you don't understand, it is tested and works just fine for saving and loading multiline text.
 
Im not totaly sure why this was moved I have all the XML working, that isnt the problem so much, I use a class(example:)

clsPerson:
VB.NET:
<Serializable()> _
Public Class Person
    Public Name As String
    Public MainJob As String
    Public SubJob As String
    Public Secondary As String
    Public Warning As String
    Public COP As String
    Public Zilart As String
    Public ToAU As String
    Public Fishing As String
    Public Woodworking As String
    Public Smithing As String
    Public Goldsmithing As String
    Public Clothcraft As String
    Public Leathercraft As String
    Public Bonecraft As String
    Public Alchemy As String
    Public Cooking As String
    Public Points As String
    Public Email As String
    Public Math1 As String
    Public Comments As String
    Public IsALeader As String
    Public Crafting As String
    Public Trial As String
    Public MemberStat As String
    Public Status As String
 
 
 
    Public Overrides Function ToString() As String
        Return Status & Name
    End Function
End Class
 
<Serializable()> _
Public Class Person2
    Public EventName As String
    Public EventDate As String
    Public EventPoint As String
    Public EventRequired As String
    Public EventArea As String
    Public EventTime As String
    Public TOD As String
    Public EventLeader As String
    Public EventType As String
 
    Public Overrides Function ToString() As String
        Return "Event Name: " & EventName & " - " & "Event Date: " & EventDate _
& " - " & "Event Time: " & EventTime & " - " & "Event Required: " & EventRequired
    End Function
End Class

then in my main forum:

VB.NET:
    Sub SaveList()
        If lstInfo.Items.Count > 0 Then
            Dim users(lstInfo.Items.Count - 1) As Person
            lstInfo.Items.CopyTo(users, 0)
 
            Dim objSW As System.IO.StreamWriter
            objSW = New System.IO.StreamWriter("Members.xml", False)
            Dim x As New System.Xml.Serialization.XmlSerializer(users.GetType)
            x.Serialize(objSW, users)
            x = Nothing
            objSW.Close()
            objSW.Dispose()
        End If
    End Sub
 
 
    Sub ReadList()
        If System.IO.File.Exists("Members.xml") Then
            lstInfo.Items.Clear()
 
            Dim users() As Person
 
            Dim objSR As System.IO.StreamReader
            objSR = New System.IO.StreamReader("Members.xml")
            Dim x As New System.Xml.Serialization.XmlSerializer(GetType(Person()))
            users = x.Deserialize(objSR)
            x = Nothing
            objSR.Close()
            objSR.Dispose()
 
            If users.Length > 0 Then
                For intA As Integer = 0 To users.Length - 1
                    lstInfo.Items.Add(users(intA))
                Next intA
            End If
        End If
    End Sub
 
    Private Sub EditComments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditComments.Click
        If idEditing = -1 Then
            On Error Resume Next
        Else
            personEditing.Comments = txtComments.Text
        End If
        txtComments.ReadOnly = True
        SaveList()
        ReadList()
    End Sub

but like I said before, it will combine it all into 1 string.. I dont know if what u said will help me but it sounds like completly redoing what I have done already, Like I stated in my opening post I have all the XML crap [as far as I know o0] stuff done, now I just need it to display the multi line stuff, if you can take what I already have and some how make it read multiline stuff it would help me out a-bunch, im really sorry to drag u around like this lol im really nub at programing, so im not sure if u told me the right stuff but I think id kinda have to erase what ive already done, just a picture of what im doing:(if it helps at all)
helpmeout4vi.jpg



again not trying to rush you but this is really urgent I need to get this finished before midnights no questions and im pritty far behind ;; so please if u can help >_< thank you very much
-Senoska
 
Last edited by a moderator:
I finally got around to have a look at the serialization/deserialization issue here. What happens is that new lines in TextBox is delimited by the ascii characters 13+10 which is 'carriage return+line feed'. When you serialize this part of text is preserved in Xml file. When deserializing the new lines are read as only linefeeds (chr 10) and the Textbox does not recognize this as a proper line feed, instead it writes some squares which usually mean 'unprintable character'.

That was the explanation as of what I observed when testing this, the only solution I know is to 'manually' at some point after deserialization do a string replace of vbLf to vbNewLine - at least if you want to display in a TextBox.

Here is one interesting note, the RichTextBox will recognize any Linefeed character (vbCr or vbLf or vbCrLf or vbNewLine) and display correctly after deserialization, so it is an option for you to use the RichTextBox instead of the plain TextBox.

Another option is to use Binary serialization using the BinaryFormatter.
 
Here is basically the code for binary serialization, taken into context with your request above, serialization:
VB.NET:
Dim fs As New IO.FileStream("Members.bin", IO.FileMode.Create)
Dim x2 As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
x2.Serialize(fs, users)
fs.Close()
fs.Dispose()
deserialization:
VB.NET:
Dim fs As New IO.FileStream("Members.bin", IO.FileMode.Open)
Dim x2 As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
users = x2.Deserialize(fs)
fs.Close()
fs.Dispose()
 
Back
Top