调用sub来更改USERname颜色

时间:2015-05-14 04:09:21

标签: vb.net

我想在RitchTextBox中更改USERname颜色,我使用下面的代码来调用SUB,但所有文本现在都是红色的?

更新

Sub AddMessage(txtUsername As String, txtSend As String)
    box.SelectionColor = Color.Red
    box.AppendText(vbCrLf & txtUsername & "$ ")
    box.SelectionColor = Color.Black
    box.AppendText(txtSend)
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSend.Click

    ' Shell("net send " & txtcomputer.Text & " " & txtmessage.Text)


    Try
        If txtPCIPadd.Text = "" Or txtUsername.Text = "" Or txtSend.Text = "" Then
            MsgBox("wright a message!", "MsgBox")
        Else
            client = New TcpClient(txtPCIPadd.Text, 44444)
            Dim writer As New StreamWriter(client.GetStream())

            txttempmsg.Text = (txtSend.Text)
            writer.Write(txtUsername.Text + " @ " + txtSend.Text)
            AddMessage(txtUsername.Text, txttempmsg.Text + vbCrLf)
            'txtmsg.Text="You:" + txtmessage.Text)
            writer.Flush()
            txtSend.Text = ""
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

当您尝试在程序中使用某些内容时遇到问题。创建一个测试示例会更容易,然后您可以使用该示例来确定正在发生的事情,而无需处理较大程序中的许多其他变量。在你的情况下,我创建了一个VB Winforms应用程序,添加了一个RichTextBox,两个TextBox和一个Button。通过这样做,我能够证明该功能正在运行。

Public Class Form1
    Sub AddMessage(txtUsername As String, txtSend As String)
        box.SelectionColor = Color.Red
        box.AppendText(vbCrLf & txtUsername & " :") 'Note added colon
        box.SelectionColor = Color.Black
        box.AppendText(txtSend)  'Note changed variable name to parameter name
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        AddMessage(txtUsername.Text, txttempmsg.Text)
    End Sub

End Class

示例:

enter image description here