如何在文本框中添加新显示而不替换第一个输入

时间:2017-04-18 02:13:44

标签: vb.net visual-studio-2010 chat newline messenger

您好我正在创建一个类似应用程序的聊天。你能帮助我吗? 当我输入新消息时,初始显示的消息将被替换:(

请参阅下面的代码:

Private Sub saveMessage()    

    FileName = Format(Now, "MMddyyyyhhmmss")
    Dim RecipientFile As String

    If CurrentRecipient = "Edward" Then
        RecipientFile = RecipientFolder & FileName & ".txt"
    ElseIf CurrentRecipient = "Criziel" Then
        RecipientFile = RecipientFolder & FileName & ".txt"
    ElseIf CurrentRecipient = "Jerome" Then
        RecipientFile = RecipientFile & FileName & ".txt"
    Else
        Exit Sub
    End If

    Dim Writer As IO.StreamWriter
    Writer = New IO.StreamWriter(RecipientFile)
    Writer.Write(MainRichTextBox.Text)

    Writer.Close()

    ShowtextRichTextBox.Text = (User & " : ") & MainRichTextBox.Text
    MainRichTextBox.Clear()
End Sub

提前谢谢! :*

1 个答案:

答案 0 :(得分:0)

您的以下代码只是将最新值分配(替换)到Rich TextBox

ShowtextRichTextBox.Text = (User & " : ") & MainRichTextBox.Text

相反,您应该附加如下文字

ShowtextRichTextBox.Text &= (User & " : ") & MainRichTextBox.Text

此外,您可以尝试使用RichTextBox的inbuild方法来附加文本,例如ShowtextRichTextBox.AppendText((User & " : ") & MainRichTextBox.Text)

注意:在追加时,您还应在新文本ShowtextRichTextBox.Text &= Environment.NewLine & (User & " : ") & MainRichTextBox.Text之前添加换行符。

修改后的代码,

Private Sub saveMessage()

    FileName = Format(Now, "MMddyyyyhhmmss")
    Dim RecipientFile As String

    If CurrentRecipient = "Edward" Then
        RecipientFile = RecipientFolder & FileName & ".txt"
    ElseIf CurrentRecipient = "Criziel" Then
        RecipientFile = RecipientFolder & FileName & ".txt"
    ElseIf CurrentRecipient = "Jerome" Then
        RecipientFile = RecipientFile & FileName & ".txt"
    Else
        Exit Sub
    End If

    Dim Writer As IO.StreamWriter
    Writer = New IO.StreamWriter(RecipientFile)
    Writer.Write(MainRichTextBox.Text)

    Writer.Close()

    ShowtextRichTextBox.Text &= Environment.NewLine & (User & " : ") & MainRichTextBox.Text
    MainRichTextBox.Clear()
End Sub