3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

How to make a simple chat program in visual basic using the Microsoft Winsock Control

Step 5Coding the command buttons

Coding the command buttons
now it is time to write the code for the command buttons

lets start with the first one cmdHost

1. double click it, this will bring up the code window
2. it should look like this
Private Sub cmdHost_Click()

End Sub
3. enter this code

sckMain.LocalPort = txtPort.text
sckMain.listen

now what does that mean??

the first line makes the winsocks local port equal the number in the textbox (txtPort)

the second line tells the winsock to listen on that port for someone trying to connect

now close that and double click on cmdConnect

enter this code

sckMain.RemoteHost = txtIP.text
sckMain.RemotePort = txtPort.text
sckMain.Connect

the first two lines change the RemoteHost and the RemotePort of the winsock (sckMain) to the ip and port we want

the sckMain.Connect tells it to connect to someone listening on that port and ip

now for the close button

double click on cmdClose and type

sckMain.close
txtLog = txtLog.text & "Connection closed!" & vbCrlf

the sckMain.close tells the winsock to disconnect

the second line adds Connection closed to txtLog

double click on cmdSend and type

sckMain.senddata "[" & txtName.text & "]" & txtSend.text
txtLog = txtLog.text & "[" & txtName.text & "]" & txtSend.text & vbcrlf
txtSend.text = ""

the first two lines are wrong i have them right in the editor and it changes them when i save so look at the picture for the right code

first line sends the text in txtSend and your name in txtName
second line adds the message you sent and your name to txtLog
third line clears the text in txtSend
« Previous StepDownload PDFView All StepsNext Step »
23 comments
Feb 18, 2012. 9:09 AMMunRa says:
How to make the message sent to ListBox if use ENTER BUTTON on Keyboard?

Like chat aplication generally, example: Facebook chat.

Thanks
Aug 30, 2011. 6:37 PMmhimebaugh says:
Operator "&" is not defined for types "String" and "System.Windows.Forms.Textbox"
This message will appear when entering the code for cmdSend. This is the exact code I typed in
Private Sub cmdSend_Click()
sckMain.SendData("[" & txtName.Text & "]" & txtSend.Text)
txtLog = txtLog.Text & "[" & txtName & "]" & txtSend.Text & vbCrLf
txtSend.Text = ""
End Sub
Aug 30, 2011. 6:51 PMmhimebaugh says:
Wait Nevermind i used a diffrent code it works now :P
Sep 13, 2010. 4:36 PMgurgle528 says:
Do the same for cmdSend:
Dim proCedure As String
sckMain.SendData("[" & txtName.Text & "]" & txtSend.Text)
proCedure = txtLog.Text & "[" & txtName.Text & "]" & txtSend.Text & vbCrLf
proCedure = txtLog.Text
txtSend.Text = ""
Sep 13, 2010. 4:34 PMgurgle528 says:
Something I hate about VB. If you get an error where it can't convert String to System.Controls.RichTextBox or something the do the following:
Dim proCedure As String
sckMain.Close()
proCedure = txtLog.Text & "Connection closed!" & vbCrLf
proCedure = txtLog.Text
2 additional lines of code doing the same thing that gets rid of an error.
May 30, 2010. 7:00 AMnishantmendh says:
Whenever i click on  .exe file it is showing an error

Jun 9, 2010. 2:46 AMpdpantig says:
Mr. twenglish1 i tried your code but not working.. how do i know my local port please help me when i connect it seems NOT RESPONDING thanks
Jun 9, 2010. 3:27 AMpdpantig says:
Mr. twenglish1 its working now in my computer but only in my computer i tried to use in the LAN but it gives me error i used our local ip address but not working please hellppppp
Mar 1, 2010. 8:53 PMpee009 says:
Always have an error on this codes... somebody can help me?


Private Sub cmdSend_Click()

sckMain.SendData "[" & txtName.Text & "]" & txtSend.Text
txtLog = txtLog.Text & "[" & txtName.Text & "]" & txtSend.Text & vbCrLf
txtSend.Text = ""

End Sub
Oct 18, 2008. 2:49 AMyoungprogrammer says:
Can I change the second line in the cmd close to txtlog.text="Connection Closed"?
Mar 3, 2010. 12:33 AMhyker69 says:
sir when i run the
program it says that it has a run time error and it requires an object how can i fix that?
Jan 21, 2010. 6:55 AMvinodcb says:
sckMain.LocalPort = txtPort.text
 is error in my vb
Aug 11, 2009. 1:49 PMGhezzo says:
Could anyone help me? I get: operator '&' is not defined for types 'string' and 'system.windows.forms.textbox' With the send button.
Sep 22, 2009. 5:42 AMRoboticProgrammer says:
add .text behind it
Aug 4, 2009. 6:51 AMyaj126 says:
thanks,i would start working on that but now i have a new problem,i have tried re reading your script and re scripting the entire thing, but i still have the same problem. when i try to host,i get: run time error:13. mismatch.why? can u help me!
Aug 2, 2009. 5:36 PMyaj126 says:
great tutorial,i have managed to make it work.could you tell me how to stop or replace these error popups when ever something has not been compleated or filled in?
Jul 31, 2009. 4:44 AMblite777 says:
the code you gave us for Private Sub cmdSend_Click() ... wont work :( i copied ur code and the one u corrected and it workk.. i would really appreciate if u can plz help me :) thanks again ... i really love ur tutorial..its nice and clear..
Mar 19, 2009. 2:57 AMSRBAle says:
I got it working tnx, can u tell me how to enhance it? Like how to add to press entere insted of clicking on send evrytime,and how to add scroll bars and things like such.
Jul 30, 2009. 2:47 AMkhjui says:
(Naturally:) You could just press the Send button once, and then just press enter to auto click the button....naturally....
Jun 25, 2009. 3:26 AMPoptrop99 says:
I copied what it said in the picture but it still wont work? HELP ME!!!
Mar 19, 2009. 2:41 AMSRBAle says:
Whan i try to save it as exe. it seas compile error: Method or Date member not found. and it marks the "close" in the first line of
sckMain.close(this close)
txtLog = txtLog.text & "Connection closed!" & vbCrlf

what to do?
tnx

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
2
Author:twenglish1
i love working with electronics and computers, playing video games, and i love to play with fire, HACKING!!!!