需要从文本框中排除输入密钥

时间:2011-02-18 08:18:39

标签: vb.net textbox

以下代码将文本从基于文本的文件读取到文本框中,但输入键也会显示(作为两个小方框)我需要我的代码来减去文本框中的绝对最后一个字符如何才能完成?< / p>

    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

2 个答案:

答案 0 :(得分:2)

您可以替换字符,如下所示:

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

但最好还是检查字符串是否为空:

之前的String.IsNullOrEmpty。

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

甚至更好,使用扩展方法检查那些令人讨厌的字符:

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

并将其称为:

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

答案 1 :(得分:0)

TextBox2.Text =(ReadBETA.ReadToEnd)

如果Textbox2.Text.Length&gt; 0然后

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

结束如果