2017-04-18 49 views
1

嗨,我正在創建一個類似於應用程序的聊天。你能幫助我嗎? 當我進入一個新的消息是越來越取代了最初顯示的消息:(如何在不替換第一個輸入的情況下在文本框中添加新顯示

請參考下面我的代碼:

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 

預先感謝您:*

回答

0

你下面的代碼只是分配(替換)的最新值的格式文本框,

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 
+0

阿魯娜嗨!有效 !非常感謝 ! :* – Bele

相關問題