Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Public Sub about_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles about.Click
MsgBox("This chat program was written to go with the tutorial at: [url]http://www.vbwm.com/forums/topic.asp?TOPIC_ID=1660[/url] and was written by Dunghill_Dave")
'as IFontDisp the user could forget my name :-)
End Sub
Private Sub cmdclear_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdclear.Click
Dim msgresult As String
msgresult = CStr(MsgBox("Are you sure you want to clear the chat box?", MsgBoxStyle.YesNoCancel))
If msgresult = CStr(MsgBoxResult.Yes) Then txtmain.Text = "" 'here we can clear the chat box if we have too much, just click it and everything goes!
'if they click anything else, we carry on...
End Sub
Private Sub cmdconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdconnect.Click
If txtIP.Text <> "" Then 'make sure the user has entered someting, IP or hostname
With Winsock 'what we are talking about
.RemoteHost = txtIP.Text 'what we want to connect to
.RemotePort = CInt(txtport.Text) 'port we are going to connect to
.Connect() 'connect to the server!
End With 'stop talking about winsock
Call areconnected() 'make the buttons look right
Else
MsgBox("You must enter an IP address!") 'you cant connect without the ip address
End If
End Sub
Private Sub cmddisconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmddisconnect.Click
Winsock.Close() 'close the winsock control
Call aredisconnected() 'get the buttons looking right
End Sub
Private Sub cmdhost_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdhost.Click
On Error Resume Next
Winsock.LocalPort = CInt(txtport.Text) 'tell the control port to use
Winsock.Listen() 'listen to the port above
Call areconnected() 'get the buttons looking right
MsgBox("Your IP address is: " & Winsock.LocalIP) 'just in case we want to tell the person we want to talk to what to connect to
txtIP.Text = Winsock.LocalIP 'and stick it in the ip box too
End Sub
Private Sub areconnected()
'get the buttons looking right
cmdhost.Enabled = False 'so we cant host while connected
cmdconnect.Visible = False 'so we cant try to connect again
cmdsend.Enabled = True 'make sure we can send data when we're connected
With cmddisconnect 'we dont really need with here, but
.Enabled = True 'it saves a lot of typing when you
.Visible = True 'have a lot of properties to set
End With 'end it so it doesnt think we are still refering to the button
'get the text boxes locked so info cant be changed:
txtport.ReadOnly = True 'lock the port box, this isnt really important as you cant change the port the winsock is using after you are connected anyway,
txtIP.ReadOnly = True 'lock ip box, (isnt really important, as above
txtname.ReadOnly = True 'stop people changing their usernames half way through chat, not important now, but if you have more users...
End Sub
Private Sub aredisconnected()
'get the buttons looking right, (opposite to areconnected)
cmdsend.Enabled = False 'we cant send if we're not connected, so dont even try!
cmdhost.Enabled = True 'we can host, so show that button
cmdconnect.Visible = True 'we could connect too!
With cmddisconnect 'we dont really need with here, but
.Enabled = False 'it saves a lot of typing when you
.Visible = False 'have a lot of properties to set
End With 'end it so it doesnt think we are still refering to the button
'get the text boxes locked so info cant be changed:
txtport.ReadOnly = False 'lock the port box, this isnt really important as you cant change the port the winsock is using after you are connected anyway,
txtIP.ReadOnly = False 'lock ip box, (isnt really important, as above
txtname.ReadOnly = False 'stop people changing their usernames half way through chat, not important now, but if you have more users...
End Sub
Private Sub cmdmore_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdmore.Click
MsgBox("We will use this button in a later tutorial: sending files")
'just in case you forget vbwm.com, and me of course!
End Sub
Private Sub cmdsend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdsend.Click
'UPGRADE_NOTE: State was upgraded to CtlState. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
If Winsock.CtlState = MSWinsockLib.StateConstants.sckConnected Then 'chack the connection is alive
Winsock.SendData((txtname).Text & ": " & (txtchat).Text) 'send your chat name, a ":" and the info you typed
'NOTE: there is a space after the : so that it will appear chatname: typedinfo and not: chatname:typedinfo
System.Windows.Forms.Application.DoEvents() 'this makes sure all the data gets their
txtmain.Text = txtmain.Text & vbCrLf & (txtname).Text & ": " & (txtchat).Text 'puts the data you typed into your txtmain chat box too
txtmain.SelectionStart = Len(txtmain.Text) 'takes you to the end of the chat session so far
txtchat.Text = "" 'empty the box so you can type some more
End If
'If the winsock controll is not alive,
'nothing will happen when you press the send button
End Sub
Public Sub elp_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles elp.Click
MsgBox("The tutorial which acompanies this is at: [url]http://www.vbwm.com/forums/topic.asp?TOPIC_ID=1660[/url] and was written by Dunghill_Dave. Please post any questions there.")
'just in case you forget vbwm.com, and me of course!
End Sub
Public Sub exit_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles exit_Renamed.Click
Me.Close()
End 'exit the program of course
End Sub
Private Sub Winsock_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.ConnectEvent
MsgBox("Connected to server!") 'just to let you know we are connected
End Sub
Private Sub Winsock_ConnectionRequest(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles Winsock.ConnectionRequest
'this part is only used if serving the chat.
Winsock.Close() 'close the control as it can only do one thing at a time
Winsock.Accept(eventArgs.requestID) 'accept the incoming connection
MsgBox("Connection recieved from: " & Winsock.RemoteHostIP) 'find out who connected (their ip address at least!)
End Sub
Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
Dim strrecieved As String
Winsock.GetData(strrecieved) 'the incoming data will be set in a variable called strrecieved
txtmain.Text = (txtmain).Text & vbCrLf & strrecieved 'add the data to the main chat box, the first bit makes sure the old data remains, the next bit vbCrLf put the new data on a new line and the new data is written in the text box
txtmain.SelectionStart = Len(txtmain.Text) 'takes you to the end of the chat session
End Sub
Private Sub Winsock_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Winsock.Error
'if we get an error, we can get told the problem, and the
'program will not close itself.
MsgBox("Error: " & eventArgs.Description & "Connection Closed")
Winsock.Close() 'close the connection to save confusion (sometimes the errors close the connection, sometimes not, best close them to make sure.
Call aredisconnected() 'make sure all the buttons are correct
End Sub
End Class