Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having an issue where I trim a string down, and attempt to put that information inside of a menustrip's text, it ends up adding a line to the end of it. I have tested this with the library being the only line of text from the text file its reading from, and with other information the program saves after that, no luck.
Thanks for reading

So where I'm attempting to get this:
VB
Set to: [H:\Guitar]


shows up in menu strip sub menu as 2 lines :
VB
Set to: [H:\Guitar
]


Code during form load:
VB
Dim FileToCheck = (Application.StartupPath & "\settings.gtr")
If File.Exists(FileToCheck) = True Then
    TempBox.Text = (My.Computer.FileSystem.ReadAllText(FileToCheck))
    TempBox.Text = Replace(TempBox.Text, vbLf & vbCr, "")

    If (TempBox.Text.Contains("library=")) Then
        Dim startLocation As Int32 = InStr(TempBox.Text, "library=") + 7
        Dim ValueToReplace = (TempBox.Text.Substring(startLocation, InStr(startLocation, TempBox.Text, Chr(13)) - startLocation))
        ToolStripMenuItem2.Visible = True
        ToolStripMenuItem2.Text = ("Set to: [" & ValueToReplace & "]")
    End If
End If


Code to save library path:
VB
Public Sub WriteLibraryPath(ByVal path As String)
    TempBox.Text = ""
    Dim Settings = (Application.StartupPath & "\settings.gtr")
    If File.Exists(Settings) = True Then
        TempBox.Text = My.Computer.FileSystem.ReadAllText(Settings)
        TempBox.Text = Replace(TempBox.Text, vbLf & vbCr, "")
        If (TempBox.Text.Contains("library=")) Then
            Dim startLocation As Int32 = InStr(TempBox.Text, "library=") + 7
            Dim ValueToReplace = TempBox.Text.Substring(startLocation, InStr(startLocation, TempBox.Text, Chr(13)) - startLocation)
            TempBox.Text = TempBox.Text.Replace(ValueToReplace, path + vbNewLine)

        Else
            TempBox.Text = TempBox.Text & "library=" & path & vbNewLine
        End If
        File.WriteAllText(Settings, TempBox.Text)
    End If

    If File.Exists(Settings) = False Then
        Dim objwriter As New StreamWriter(Settings)
        objwriter.WriteLine("library=" & path)
        objwriter.Close()
        objwriter.Dispose()
    End If
End Sub
Posted
Updated 27-Jul-13 20:33pm
v3

1 solution

The problem is Chr(13). This is one of the end-of-line separators. Please see: http://en.wikipedia.org/wiki/Newline[^].

None of end-of-line characters should appear in most controls and other UI elements, such as a menu item. Run your code under the debugger and make sure you never insert such thing. And even in other cases, when you really need to insert such thing, you should not use Chr(13) or anything else as an immediate constant. As you can see from the article referenced above, it depends on OS, so you need to use the static property System.Environment.NewLine instead:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx[^].

One more, minor problem in your code: using string concatenation. Strings are immutable, so using string concatenation repeatedly is bad (do I have to explain why). Instead, prefer using string.Format. If it is not suitable (for example, in loops), use the mutable class System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

—SA
 
Share this answer
 
Comments
Joezer BH 28-Jul-13 3:21am    
5ed
Sergey Alexandrovich Kryukov 28-Jul-13 12:13pm    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900