开发者

Need to exclued enter key from textbox

The folowing c开发者_JAVA百科ode to read text from a text based file into a textbox however the enter key is also displayed (as two small boxes) I need my code to subtract the absolute last character in the textbox how can this be done?

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim ReadALPHA As System.IO.StreamReader

    ReadALPHA = System.IO.File.OpenText("C:\Alpha.val")
    TextBox1.Text = (ReadALPHA.ReadToEnd)
    ReadALPHA.Close()

    Dim ReadBETA As System.IO.StreamReader

    ReadBETA = System.IO.File.OpenText("C:\BETA.val")
    TextBox2.Text = (ReadBETA.ReadToEnd)
    ReadBETA.Close()


    'TextBox1.Text = "<Enter first value>"
    'TextBox2.Text = "<Enter second value>"

End Sub


You can replace the character, like this:

TextBox2.Text = (ReadBETA.ReadToEnd).Replace(Environment.NewLine, " ")

But it's always better to check if the string is null:

String.IsNullOrEmpty before.

Dim Buffer As String
Buffer = ReadBETA.ReadToEnd
If Not String.IsNullOrEmpty(Buffer) Then
   TextBox2.Text = Buffer.Replace(Environment.NewLine, " ")
End If

Or even better, check for those nasty characters with an Extension Method:

Imports System.Runtime.CompilerServices

Public Module ExtMethods

    <Extension()> _
    Public Function ReplaceEnterKeyword(ByVal Value As String, Optional ByVal CharSubstitution As String = "") As String
        If Not String.IsNullOrEmpty(Value) Then
            With Value
                Value = .Replace(Microsoft.VisualBasic.ControlChars.CrLf, CharSubstitution) _
                        .Replace(Microsoft.VisualBasic.ControlChars.Cr, CharSubstitution) _
                        .Replace(Microsoft.VisualBasic.ControlChars.Lf, CharSubstitution)
            End With
        End If
        Return (Value)
    End Function

End Module

and call it like this:

TextBox2.Text = Buffer.ReplaceEnterKeyword(" ");


TextBox2.Text = (ReadBETA.ReadToEnd)

If Textbox2.Text.Length > 0 Then

TextBox2.Text = TextBox2.Text.Remove(TextBox2.Text.Length - 1, 1)

End If

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜