MenuStrip.text里面的字符串最后会有额外的行 [英] String inside of MenuStrip.text is getting extra line at the end

查看:52
本文介绍了MenuStrip.text里面的字符串最后会有额外的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我修剪了一个字符串,并尝试将该信息放入menustrip的文本中,最后在它的末尾添加一行。我已经测试了这个库,它是文本文件中唯一的文本行读取,以及程序保存后的其他信息,没有运气。

感谢您的阅读



所以,我试图得到这个:

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:

Set to: [H:\Guitar]





在菜单条子菜单中显示为2行:



shows up in menu strip sub menu as 2 lines :

Set to: [H:\Guitar
]





表单加载时的代码:



Code during form load:

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:

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

推荐答案

问题是 Chr(13)。这是一种终端分隔符。请参阅: http://en.wikipedia.org/wiki/Newline [ ^ ]。



没有结束 - 行字符应出现在大多数控件和其他UI元素中,例如菜单项。在调试器下运行代码,确保永远不要插入这样的东西。即使在其他情况下,当你真的需要插入这样的东西时,你不应该使用 Chr(13)或其他任何东西作为立即常量 。正如您在上面引用的文章中所看到的,它取决于操作系统,因此您需要使用静态属性 System.Environment.NewLine 而不是:

http://msdn.microsoft.com/en-us/library/system.environment .newline.aspx [ ^ ]。



您的代码中还有一个小问题:使用字符串连接。字符串是不可变的,因此反复使用字符串连接是不好的(我必须解释原因)。相反,更喜欢使用 string.Format 。如果它不合适(例如,在循环中),请使用可变类 System.Text.StringBuilder

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx [ ^ ]。



-SA
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


这篇关于MenuStrip.text里面的字符串最后会有额外的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆