⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmtalker.vb

📁 Visual Basic.NET控件时尚编程百例
💻 VB
字号:
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Net.Sockets
Imports System.Windows.Forms


Class frmTalker
    Inherits Windows.Forms.Form
   

    Private talkerObj As Talker

    Public Sub New(ByVal talkerObj As Talker)
        InitializeComponent()
        Me.talkerObj = talkerObj '初始化talkerObj
        AddHandler talkerObj.Notifications, _
        AddressOf HandleTalkerNotifications
        '添加事件
    End Sub

    Protected Overrides Sub OnClosed(ByVal e As EventArgs)
        If Not (talkerObj Is Nothing) Then
            RemoveHandler talkerObj.Notifications, _
            AddressOf HandleTalkerNotifications
            '停止事件
            talkerObj.Dispose()
            '关闭应用程序时释放Talker对象
        End If
        MyBase.OnClosed(e)
    End Sub


    Private Sub HandleTalkerNotifications(ByVal notify As Talker.Notification, _
    ByVal data As Object)
        '处理TalkerObj的事件
        Select Case notify
            Case Talker.Notification.Initialized
            Case Talker.Notification.StatusChange
                ' 状态改变
                Dim statusObj As Talker.Status = CType(data, Talker.Status)
                Label1.Text = String.Format("目前状态: {0}", statusObj)
                '显示状态
                If statusObj = Talker.Status.已正常连接 Then
                    TxtSend.Enabled = True
                    '连接上时,显示文本框有效
                    TxtSend.Focus()
                End If
            Case Talker.Notification.ReceivedAppend
                '响应获取信息事件
                TxtRe.AppendText(data.ToString())
            Case Talker.Notification.ReceivedRefresh
                TxtRe.Text = data.ToString()
                TxtRe.SelectionStart = Int32.MaxValue
                TxtRe.ScrollToCaret()
                '响应更新
            Case Talker.Notification.ErrorNotify
                Close(data.ToString())
                '响应错误
            Case Talker.Notification.EndNotify
                '停止事件
                MessageBox.Show(Me, data.ToString(), "错误")
                Close()
            Case Else
                Close()
        End Select
    End Sub

    Private Overloads Sub Close(ByVal message As String)
        MessageBox.Show(message, "错误!")
        Close()
    End Sub

#Region "初始化Windows.Form的代码"
    Friend WithEvents TxtSend As System.Windows.Forms.TextBox
    Friend WithEvents TxtRe As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox

    Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Splitter1 = New System.Windows.Forms.Splitter()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.TxtSend = New System.Windows.Forms.TextBox()
        Me.TxtRe = New System.Windows.Forms.TextBox()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TxtRe})
        Me.GroupBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(368, 112)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "接收信息"
        '
        'Label1
        '
        Me.Label1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Label1.Location = New System.Drawing.Point(0, 270)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(368, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'Splitter1
        '
        Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Splitter1.Location = New System.Drawing.Point(0, 112)
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.Size = New System.Drawing.Size(368, 3)
        Me.Splitter1.TabIndex = 2
        Me.Splitter1.TabStop = False
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.TxtSend})
        Me.GroupBox2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GroupBox2.Location = New System.Drawing.Point(0, 115)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(368, 155)
        Me.GroupBox2.TabIndex = 3
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "发送信息"
        '
        'TxtSend
        '
        Me.TxtSend.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TxtSend.Enabled = False
        Me.TxtSend.Location = New System.Drawing.Point(3, 17)
        Me.TxtSend.Multiline = True
        Me.TxtSend.Name = "TxtSend"
        Me.TxtSend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TxtSend.Size = New System.Drawing.Size(362, 135)
        Me.TxtSend.TabIndex = 0
        Me.TxtSend.Text = ""
        '
        'TxtRe
        '
        Me.TxtRe.BackColor = System.Drawing.SystemColors.Info
        Me.TxtRe.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TxtRe.Location = New System.Drawing.Point(3, 17)
        Me.TxtRe.Multiline = True
        Me.TxtRe.Name = "TxtRe"
        Me.TxtRe.ReadOnly = True
        Me.TxtRe.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TxtRe.Size = New System.Drawing.Size(362, 92)
        Me.TxtRe.TabIndex = 0
        Me.TxtRe.Text = ""
        '
        'frmTalker
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(368, 293)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox2, Me.Splitter1, Me.Label1, Me.GroupBox1})
        Me.Name = "frmTalker"
        Me.Text = "点对点聊天"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub
#End Region


    Private Sub txtSend_KeyPress(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles TxtSend.KeyPress
        If e.KeyChar = Chr(13) Then
            If Not (talkerObj Is Nothing) Then
                talkerObj.SendTalk(CType(sender, TextBox).Text)
            End If
        End If
    End Sub
End Class



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -