我有一个名为channel的表单窗口,用户将从该窗口登录并连接到neptune.smokeynet.org server.And工作正常。 用户登录后,它将在表单的右侧显示在线用户。
所以我能够输入消息并在公共窗口中发送它的工作正常以供公众查看但是当我点击在线用户并键入消息发送给他时我收到错误 " NullReferenceException未处理" 在此表单中,对象引用未设置为对象的实例,名为
此表单负责与在线用户进行私密聊天。如何在用户成功登录后立即向在线用户发送消息。 privatechat.vb
Public Class privatechat
Public irc As New channel
Public Sub Button1_Click(sender As Object, e As System.EventArgs) Handles prvmsgtxt.Click
If prvmsg.Text = "" Then
MsgBox("Please Enter a Message", MsgBoxStyle.OkOnly, "Enter Messages")
Else
irc.irc.SendRAW("PRIVMSG" & Space(1) & pvtlabel.Text & Space(1) & prvmsg.Text)
prvtextbox.AppendText("<" & irc.irc.Nick & "> " & prvmsg.Text & vbNewLine)
prvmsg.Clear()
prvmsg.Focus()
End If
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles prvtextbox.TextChanged
End Sub
Private Sub prvmsg_TextChanged(sender As Object, e As EventArgs) Handles prvmsg.TextChanged
End Sub
Private Sub private_messageincome(sender As Object, e As EventArgs) Handles prvtextbox.TextChanged
End Sub
Private Sub pvtlabel_Click(sender As Object, e As EventArgs) Handles pvtlabel.Click
End Sub
Private Sub prvnametxt_TextChanged(sender As Object, e As EventArgs)
End Sub
Public Sub privatechat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackColor = login.BackColor
ForeColor = login.ForeColor
End Sub
End Class
连接的另一种主要形式,工作正常 的 Channel.vb
Imports TechLifeForum
Public Class channel
Public WithEvents irc As IrcClient
Public txtnick As String
Dim chatfrm(100) As privatechat
Dim z As Integer = 1
Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs)
irc.Connect()
End Sub
Public Sub channel_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
irc = New IrcClient("neptune.smokeynet.org", 6667)
irc.Nick = login.txtnick.Text
lstUsers.Enabled = False
txtSend.Enabled = False
btnSend.Enabled = False
irc.Connect()
rtbOutput.SelectionColor = Color.Brown
rtbOutput.AppendText("* * * Attempting to connect to the chat server * * * " & vbNewLine)
Connectbtn.Enabled = False
BackColor = login.BackColor
ForeColor = login.ForeColor
End Sub
Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
If txtSend.Text = "" Then
MsgBox("Please Enter a Message")
Else
irc.Nick = login.txtnick.Text
'irc.SendRAW("PRIVMSG #nepal " & txtSend.Text)
irc.SendMessage("#nepal", txtSend.Text)
rtbOutput.AppendText("<" & irc.Nick & "> " & txtSend.Text & vbNewLine)
txtSend.Clear()
txtSend.Focus()
End If
End Sub
Private Sub txtSend_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtSend.KeyDown
If e.KeyCode = Keys.Enter Then
btnSend.PerformClick()
End If
End Sub
Public Sub irc_ChannelMessage(Channel As String, User As String, Message As String) Handles irc.ChannelMessage
rtbOutput.AppendText("<" & User & "> " & Message & vbNewLine)
rtbOutput.ScrollToCaret()
End Sub
Private Sub irc_userjoin(channel As String, User As String) Handles irc.UserJoined
rtbOutput.SelectionColor = Color.Green
rtbOutput.AppendText("*** " & User & " has joined the chatroom" & vbNewLine)
End Sub
Private Sub irc_userleft(channel As String, User As String) Handles irc.UserLeft
rtbOutput.SelectionColor = Color.Red
rtbOutput.AppendText("*** " & User & " has left the chatroom" & vbNewLine)
End Sub
Sub UserNickChangeEventDelegate(oldUser As String, newUser As String) Handles irc.UserNickChange
rtbOutput.SelectionColor = Color.Blue
rtbOutput.AppendText("*** " & oldUser & " is now knows as " & newUser & vbNewLine)
lstUsers.Update()
End Sub
Private Sub irc_PrivateMessage(User As String, message As String) Handles irc.PrivateMessage
Dim t As Integer = 1
Dim found As Integer = 0
Dim i As Integer = 1
If z = 1 Then
For i = 1 To 100
chatfrm(i) = New privatechat
Next
z += 1
End If
For i = 1 To 100
If Trim(chatfrm(i).pvtlabel.Text) = User Then
found = 1
t = i
Exit For
End If
Next
If found = 0 Then
For i = 1 To 100
If chatfrm(i).pvtlabel.Text = " " Then
t = i
chatfrm(t).Show()
chatfrm(t).Focus()
chatfrm(t).pvtlabel.Text = User
chatfrm(t).prvtextbox.AppendText("<" & User & ">" & message & vbNewLine)
Exit For
End If
Next
ElseIf found = 1 Then
chatfrm(t).prvtextbox.AppendText("<" & User & ">" & message & vbNewLine)
chatfrm(t).Show()
chatfrm(t).Focus()
End If
End Sub
Private Sub irc_ExceptionThrown(ex As System.Exception) Handles irc.ExceptionThrown
MessageBox.Show(ex.Message)
End Sub
Public Sub irc_OnConnect() Handles irc.OnConnect
irc.SendRAW("mode" & Space(1) & irc.Nick & Space(1) & "-B")
If login.txtpass.Text = "" Then
Else
irc.SendRAW("PRIVMSG nickserv identify" & Space(1) & login.txtpass.Text)
End If
lstUsers.Enabled = True
irc.JoinChannel("#nepal")
btnSend.Enabled = True
txtSend.Enabled = True
End Sub
Private Sub irc_ServerMessage(message As String) Handles irc.ServerMessage
'rtbOutput.SelectionColor = Color.Brown
'rtbOutput.AppendText(message & vbNewLine)
End Sub
Public Sub irc_noticemessage(User As String, message As String) Handles irc.NoticeMessage
If User = "NickServ" And message = "Password incorrect." Then
password.Show()
Else
End If
If login.txtpass.Text = "" And User = "NickServ" And message = "please choose a different nick." Then
password.Show()
Else
End If
End Sub
Public Sub irc_UpdateUsers(Channel As String, userlist() As String) Handles irc.UpdateUsers
lstUsers.Items.Clear()
lstUsers.Items.AddRange(userlist)
totaluser.Text = "Total Users: " & lstUsers.Items.Count
End Sub
Private Sub rtbOutput_TextChanged(sender As Object, e As EventArgs) Handles rtbOutput.TextChanged
End Sub
Public Sub nickttaken(Nick As String) Handles irc.NickTaken
changenick.Show()
End Sub
Private Sub lstUsers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstUsers.SelectedIndexChanged
End Sub
Private Sub lstUsers_MouseDoubleClick(ByVal sender As Object, e As EventArgs) Handles lstUsers.MouseDoubleClick
Dim tmpnick, flet As String
tmpnick = lstUsers.SelectedItem
flet = Microsoft.VisualBasic.Left(lstUsers.SelectedItem, 1)
If Trim(flet) = "@" Or Trim(flet) = "%" Or Trim(flet) = "+" Or Trim(flet) = "~" Or Trim(flet) = "&" Then
tmpnick = Mid(tmpnick, 2, Len(tmpnick) - 1)
End If
Dim t As Integer = 1
Dim found As Integer = 0
Dim i As Integer = 1
If z = 1 Then
For i = 1 To 100
chatfrm(i) = New privatechat
Next
z += 1
End If
For i = 1 To 100
If Trim(chatfrm(i).pvtlabel.Text) = tmpnick Then
found = 1
t = i
Exit For
End If
Next
If found = 0 Then
For i = 1 To 100
If chatfrm(i).pvtlabel.Text = " " Then
t = i
chatfrm(t).Show()
chatfrm(t).Focus()
chatfrm(t).pvtlabel.Text = tmpnick
Exit For
End If
Next
ElseIf found = 1 Then
chatfrm(t).Show()
chatfrm(t).Focus()
End If
End Sub
Private Sub lstUsers_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstUsers.MouseUp
End Sub
Private Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
'-- etc
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ConnectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Connectbtn.Click
irc = New IrcClient("127.0.0.1", 6667)
rtbOutput.SelectionColor = Color.Brown
rtbOutput.AppendText("* * * Attempting to connect to the chat server * * * " & vbNewLine)
irc.Nick = login.txtnick.Text
irc.Connect()
If irc.Connected = True Then
Disconnectbtn.Enabled = True
Else
Disconnectbtn.Enabled = True
Connectbtn.Enabled = False
End If
rtbOutput.Clear()
End Sub
Private Sub FIleToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FileToolStripMenuItem.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
irc.Disconnect()
rtbOutput.Clear()
End Sub
Private Sub txtSend_TextChanged(sender As Object, e As EventArgs) Handles txtSend.TextChanged
End Sub
Private Sub DisconnectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Disconnectbtn.Click
If irc.Connected = True Then
Disconnectbtn.Enabled = False
Connectbtn.Enabled = True
Else
Disconnectbtn.Enabled = True
Connectbtn.Enabled = False
End If
If irc.Connected = True Then
rtbOutput.SelectionColor = Color.Red
irc.SendRAW("quit Using SmokeyNet Messenger Beta Version 1.0")
Else
End If
lstUsers.Items.Clear()
rtbOutput.SelectionColor = Color.Purple
rtbOutput.AppendText("* * * Disconnected * * * " & vbNewLine)
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Dim exitmsn As System.Windows.Forms.DialogResult
exitmsn = MsgBox("Do you really want to Quit?", MsgBoxStyle.YesNo, "Exit Messenger")
If exitmsn = MsgBoxResult.Yes And irc.Connected = True Then
rtbOutput.SelectionColor = Color.Red
irc.SendRAW("quit Using SmokeyNet Messenger Beta Version 1.0")
End
Else
End
End If
End Sub
Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
End Sub
Private Sub rtbOutput_TextChanged_1(sender As Object, e As EventArgs) Handles rtbOutput.TextChanged
End Sub
Private Sub AboutMessengerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutMessengerToolStripMenuItem.Click
About.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub ClearChatTextToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearChatTextToolStripMenuItem.Click
rtbOutput.Clear()
End Sub
Private Sub RefreshToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RefreshToolStripMenuItem.Click
End Sub
Private Sub ChangeNicknameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChangeNicknameToolStripMenuItem.Click
changenick.Show()
End Sub
Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
End Sub
Private Sub EnterPasswordToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EnterPasswordToolStripMenuItem.Click
password.Show()
End Sub
Public Sub NickTakenEventDelegate(nick As String)
changenick.Show()
End Sub
Private Sub RegisterNickNameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RegisterNickNameToolStripMenuItem.Click
registernick.Show()
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles totaluser.Click
End Sub
Private Sub btnSend_DoubleClick(sender As Object, e As EventArgs) Handles btnSend.DoubleClick
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
End Sub
Private Sub LightBlueToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LightBlueToolStripMenuItem.Click
BackColor = Color.LightBlue
End Sub
Private Sub CornFlowerBlueToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CornFlowerBlueToolStripMenuItem.Click
BackColor = Color.CornflowerBlue
End Sub
Private Sub HotPinkToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HotPinkToolStripMenuItem.Click
BackColor = Color.HotPink
End Sub
Private Sub BlackToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BlackToolStripMenuItem.Click
BackColor = Color.Black
ForeColor = Color.Black
End Sub
Private Sub AliceBlueToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AliceBlueToolStripMenuItem.Click
BackColor = Color.AliceBlue
End Sub
Private Sub MediumPurpleToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MediumPurpleToolStripMenuItem.Click
BackColor = Color.MediumPurple
End Sub
Private Sub FloralWhiteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FloralWhiteToolStripMenuItem.Click
BackColor = Color.FloralWhite
End Sub
Private Sub WhoisToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WhoisToolStripMenuItem.Click
Dim tmpnick, flet As String
tmpnick = lstUsers.SelectedItem
flet = Microsoft.VisualBasic.Left(lstUsers.SelectedItem, 1)
If Trim(flet) = "@" Or Trim(flet) = "%" Or Trim(flet) = "+" Or Trim(flet) = "~" Or Trim(flet) = "&" Then
tmpnick = Mid(tmpnick, 2, Len(tmpnick) - 1)
End If
irc.SendRAW("WHOIS" & Space(1) & tmpnick)
End Sub
End Class