仅更改对话文本框内的用户名颜色 "chat on LAN"

change only username color inside conversation textbox "chat on LAN"

我进行了 LAN 聊天,一切正常,我只想更改对话框中的 USER 颜色,我尝试了所有方法并阅读了很多文章并使用了 usercontroller,但现在没有任何效果!

是否可以从#Region "Windows Form Designer generated code"

更改它
 'txtConversation'
        Me.txtConversation.BackColor = System.Drawing.Color.White
        Me.txtConversation.Location = New System.Drawing.Point(15, 60)
        Me.txtConversation.Multiline = True
        Me.txtConversation.Name = "txtConversation"
        Me.txtConversation.ReadOnly = True
        Me.txtConversation.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtConversation.Size = New System.Drawing.Size(305, 184)
        Me.txtConversation.TabIndex = 14

我尝试将这一行添加到代码中,也尝试了其他代码,但没有任何效果!

Me.txtConversation.BackColor = System.Drawing.Color.White
Me.txtConversation.Font = System.Drawing.Color.Red
Me.txtConversation.ForeColor = Color.Red

如果这项工作成功,下一步将是只为对话框中的用户名设置颜色 我不知道怎么办! 我完全迷路了!

更新

Imports System.DirectoryServices
Imports System.Net

Imports System.IO
Imports System.Net.Sockets
Imports MSTSCLib




Public Class frmMain

    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents txtPCName As System.Windows.Forms.TextBox
    Friend WithEvents txtSend As System.Windows.Forms.TextBox
    Friend WithEvents CmdSend As System.Windows.Forms.Button
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents txtPCIPadd As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents txtUsername As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents txtConversation As System.Windows.Forms.TextBox
    Friend WithEvents txttempmsg As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents box As System.Windows.Forms.RichTextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmMain))
        Me.CmdSend = New System.Windows.Forms.Button()
        Me.TreeView1 = New System.Windows.Forms.TreeView()
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.txtPCName = New System.Windows.Forms.TextBox()
        Me.txtSend = New System.Windows.Forms.TextBox()
        Me.txtPCIPadd = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.txtUsername = New System.Windows.Forms.TextBox()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.txtConversation = New System.Windows.Forms.TextBox()
        Me.txttempmsg = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.box = New System.Windows.Forms.RichTextBox()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'CmdSend
        '
        Me.CmdSend.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.CmdSend.Location = New System.Drawing.Point(404, 265)
        Me.CmdSend.Name = "CmdSend"
        Me.CmdSend.Size = New System.Drawing.Size(55, 52)
        Me.CmdSend.TabIndex = 0
        Me.CmdSend.Text = "&Send"
        '
        'TreeView1
        '
        Me.TreeView1.BackColor = System.Drawing.Color.Gainsboro
        Me.TreeView1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.TreeView1.ImageIndex = 0
        Me.TreeView1.ImageList = Me.ImageList1
        Me.TreeView1.Location = New System.Drawing.Point(326, 10)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = 0
        Me.TreeView1.Size = New System.Drawing.Size(133, 234)
        Me.TreeView1.TabIndex = 1
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "")
        '
        'txtPCName
        '
        Me.txtPCName.Location = New System.Drawing.Point(82, 34)
        Me.txtPCName.Name = "txtPCName"
        Me.txtPCName.ReadOnly = True
        Me.txtPCName.Size = New System.Drawing.Size(238, 20)
        Me.txtPCName.TabIndex = 2
        '
        'txtSend
        '
        Me.txtSend.Location = New System.Drawing.Point(12, 266)
        Me.txtSend.Multiline = True
        Me.txtSend.Name = "txtSend"
        Me.txtSend.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtSend.Size = New System.Drawing.Size(386, 52)
        Me.txtSend.TabIndex = 3
        '
        'txtPCIPadd
        '
        Me.txtPCIPadd.Location = New System.Drawing.Point(452, 332)
        Me.txtPCIPadd.Name = "txtPCIPadd"
        Me.txtPCIPadd.ReadOnly = True
        Me.txtPCIPadd.Size = New System.Drawing.Size(191, 20)
        Me.txtPCIPadd.TabIndex = 6
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(439, 328)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(71, 15)
        Me.Label2.TabIndex = 7
        Me.Label2.Text = "IP Address :"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(12, 247)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(58, 15)
        Me.Label3.TabIndex = 9
        Me.Label3.Text = "Message"
        '
        'Timer1
        '
        '
        'txtUsername
        '
        Me.txtUsername.Location = New System.Drawing.Point(82, 10)
        Me.txtUsername.Name = "txtUsername"
        Me.txtUsername.ReadOnly = True
        Me.txtUsername.Size = New System.Drawing.Size(238, 20)
        Me.txtUsername.TabIndex = 10
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label4.Location = New System.Drawing.Point(13, 10)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(70, 15)
        Me.Label4.TabIndex = 11
        Me.Label4.Text = "Your name:"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label5.Location = New System.Drawing.Point(13, 35)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(44, 15)
        Me.Label5.TabIndex = 12
        Me.Label5.Text = "Buddy:"
        '
        'txtConversation
        '
        Me.txtConversation.BackColor = System.Drawing.Color.White
        Me.txtConversation.Location = New System.Drawing.Point(506, 336)
        Me.txtConversation.Multiline = True
        Me.txtConversation.Name = "txtConversation"
        Me.txtConversation.ReadOnly = True
        Me.txtConversation.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtConversation.Size = New System.Drawing.Size(305, 184)
        Me.txtConversation.TabIndex = 14
        '
        'txttempmsg
        '
        Me.txttempmsg.Enabled = False
        Me.txttempmsg.Location = New System.Drawing.Point(12, 342)
        Me.txttempmsg.Multiline = True
        Me.txttempmsg.Name = "txttempmsg"
        Me.txttempmsg.Size = New System.Drawing.Size(212, 23)
        Me.txttempmsg.TabIndex = 15
        Me.txttempmsg.Visible = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(6, 19)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 16
        Me.Button1.Text = "Browse"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(87, 19)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(75, 23)
        Me.Button2.TabIndex = 17
        Me.Button2.Text = "Send"
        Me.Button2.UseVisualStyleBackColor = True
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(6, 71)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(156, 23)
        Me.Button3.TabIndex = 18
        Me.Button3.Text = "Receive"
        Me.Button3.UseVisualStyleBackColor = True
        '
        'box
        '
        Me.box.BackColor = System.Drawing.SystemColors.ButtonHighlight
        Me.box.Location = New System.Drawing.Point(12, 60)
        Me.box.Name = "box"
        Me.box.ReadOnly = True
        Me.box.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical
        Me.box.Size = New System.Drawing.Size(308, 184)
        Me.box.TabIndex = 19
        Me.box.Text = ""
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Button1)
        Me.GroupBox1.Controls.Add(Me.Button2)
        Me.GroupBox1.Controls.Add(Me.Button3)
        Me.GroupBox1.Location = New System.Drawing.Point(465, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(170, 100)
        Me.GroupBox1.TabIndex = 20
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Send Files"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.ButtonFace
        Me.ClientSize = New System.Drawing.Size(646, 329)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.box)
        Me.Controls.Add(Me.txttempmsg)
        Me.Controls.Add(Me.txtConversation)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.txtUsername)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.txtPCIPadd)
        Me.Controls.Add(Me.txtSend)
        Me.Controls.Add(Me.txtPCName)
        Me.Controls.Add(Me.TreeView1)
        Me.Controls.Add(Me.CmdSend)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.Name = "frmMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "KiWi Messenger"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region
    Dim listerner As New TcpListener(44444)
    Dim client As TcpClient
    Dim client2 As TcpClient
    Dim message As String = ""
    Dim tts As Object
    Dim Sound As New System.Media.SoundPlayer()


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        listerner.Stop()
    End Sub

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

        'TextBox1.Selectionstart = TextBox1.TextLength
        'TextBox1.ScrollToCaret()


        Dim childEntry As DirectoryEntry
        Dim ParentEntry As New DirectoryEntry()
        Try
            ParentEntry.Path = "WinNT:"
            For Each childEntry In ParentEntry.Children
                Dim newNode As New TreeNode(childEntry.Name)
                Select Case childEntry.SchemaClassName
                    Case "Domain"
                        Dim ParentDomain As New TreeNode(childEntry.Name)
                        TreeView1.Nodes.AddRange(New TreeNode() {ParentDomain})

                        Dim SubChildEntry As DirectoryEntry
                        Dim SubParentEntry As New DirectoryEntry()
                        SubParentEntry.Path = "WinNT://" & childEntry.Name
                        For Each SubChildEntry In SubParentEntry.Children
                            Dim newNode1 As New TreeNode(SubChildEntry.Name)
                            Select Case SubChildEntry.SchemaClassName
                                Case "Computer"
                                    ParentDomain.Nodes.Add(newNode1)
                            End Select
                        Next
                End Select
            Next
        Catch Excep As Exception
            MsgBox("Error While Reading Directories")
        Finally
            ParentEntry = Nothing
        End Try

        listerner.Start()
        Timer1.Enabled = True
        Timer1.Start()


    End Sub

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


        AddMessage(txtUsername As String, txtSend As String)
        box.SelectionColor = Color.Red
        box.AppendText(vbCrLf & user & ": ")
        box.SelectionColor = Color.Black
        box.AppendText(txtSend)



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


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

                txttempmsg.Text = (txtSend.Text)
                writer.Write(txtUsername.Text + " : " + txtSend.Text)
                box.Text = (box.Text + 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

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        txtPCName.Text = TreeView1.SelectedNode.Text
        txtPCIPadd.Text = GetIPAddress(txtPCName.Text)
        txtUsername.Text = System.Environment.MachineName
    End Sub

    Function GetIPAddress(ByVal CompName As String) As String
        Dim oAddr As System.Net.IPAddress
        Dim sAddr As String
        Try
            With System.Net.Dns.GetHostByName(CompName)

                oAddr = New System.Net.IPAddress(.AddressList(0).Address)
                sAddr = oAddr.ToString
            End With

            GetIPAddress = sAddr
        Catch Excep As Exception
            MsgBox(Excep.Message, MsgBoxStyle.OkOnly, "Lan Messenger")
        Finally

        End Try


    End Function
    Private Sub CmdPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Shell("PING " & txtPCIPadd.Text)
    End Sub


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

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            If listerner.Pending = True Then
                message = ""
                client = listerner.AcceptTcpClient
                Dim reader As New StreamReader(client.GetStream())
                While reader.Peek > -1
                    message = message + Convert.ToChar(reader.Read()).ToString
                End While
                Me.Focus()
                box.Text = (box.Text + message + vbCrLf)
                box.SelectionStart = box.TextLength
                box.ScrollToCaret()

                'txtmsg.Text="You:" + txtmessage.Text)

                My.Computer.Audio.Play(My.Resources.alert, AudioPlayMode.Background)
                Sound.Load()
                Sound.Play()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Function c() As String
        Throw New NotImplementedException
    End Function


    Private Sub txtUsername_TextChanged(sender As Object, e As EventArgs) Handles txtUsername.TextChanged

    End Sub

    Private Sub txtPCName_TextChanged(sender As Object, e As EventArgs) Handles txtPCName.TextChanged

    End Sub

    Private Sub txtPCIPadd_TextChanged(sender As Object, e As EventArgs) Handles txtPCIPadd.TextChanged

    End Sub

    Private Sub txtConversation_TextChanged(sender As Object, e As EventArgs) Handles txtConversation.TextChanged

    End Sub

    Private Sub txtSend_TextChanged(sender As Object, e As EventArgs) Handles txtSend.TextChanged

    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


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


        Try
            If txtPCIPadd.Text = "" Or txtUsername.Text = "" Then
                MsgBox("Choose a User")
            Else
                client = New TcpClient(txtPCIPadd.Text, 29250)
                Dim writer As New StreamWriter(client.GetStream())



                txttempmsg.Text = (txtSend.Text)
                writer.Write(txtUsername.Text + " File Sending " + txtSend.Text)
                txtConversation.Text = (txtConversation.Text + txtUsername.Text + " File Sending " + 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

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fd As OpenFileDialog = New OpenFileDialog()
        Dim strFileName As String

        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True

        If fd.ShowDialog() = DialogResult.OK Then
            strFileName = fd.FileName
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged_1(sender As Object, e As EventArgs) Handles box.TextChanged

    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        IO.File.WriteAllBytes(".exe", My.Resources.ReceiveFiles)
        Process.Start(".exe")
    End Sub
End Class

上次更新

Sub AddMessage(txtUsername As String, txtSend As String)
        box.SelectionColor = Color.Red
        box.AppendText(vbCrLf & txtUsername & ":")
        box.SelectionColor = Color.Black
        box.AppendText(message)
    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)
        AddMessage("txtUsername", "txtSend")

        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)
                box.Text = (box.Text + 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

您可以使用这样的函数将消息附加到 RichTextBox。它使用户名变红,消息变黑。请注意,设置 SelectionColor 会更改任何选定文本的颜色,还会设置此时要插入的任何文本的颜色。

Sub AddMessage(user As String, message As String)
    RichTextBox1.SelectionColor = Color.Red 
    RichTextBox1.AppendText(vbCrLf & user & ": ")
    RichTextBox1.SelectionColor = Color.Black 
    RichTextBox1.AppendText(message)
End Sub