Reputation: 79
I'm using the following code to dynamically create a WinSock control, and connect to a server using it:
Licenses.Add "MSWinsock.WinSock.1"
Controls.Add "MSWinsock.WinSock.1", "s1"
s1.RemoteHost = "irc.netsplit.de"
s1.RemotePort = "6667"
s1.Connect
However I get "Error 424: Object required.", highlighting "s1.RemoteHost...", how can I fix this? The control should be added from previous lines?
Thanks.
Upvotes: 1
Views: 3208
Reputation: 11
Sorry for my bad english...
You can make array of WinSock controls. To do this you need to add one control to form and assign it's index property to 0. Then you can use statement like this:
Load WinSock1(5)
where WinSock1 is name of the control(previously added to form), and 5 is index in the array for the new control(it could be a variable). This means that you have multiple 'copies' of same control and you can manipulate with each individually. After that you can manipulate with this control like this:
WinSock1(5).LocalPort = 80
Winsock1(5).Listen
and respond to events like this:
Private Sub WinSock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
WinSock1(Index).Close
WinSock1(Index).Accept(requestID)
End Sub
In this situation Index is control's index in the array, so you don't need to track controls yourself.
Hope this helps... Have fun! :)
Upvotes: 1
Reputation: 175766
The simplest way is to just dump a socket control to the form to negate the need for importing the license & get strict typing, then;
Controls.Add "MSWinsock.WinSock.1", "s1"
Dim s1 As Winsock: Set s1 = Controls("s1")
s1.RemoteHost = "irc.netsplit.de"
Or perhaps an array of sockets is what your after?
Upvotes: 2
Reputation: 545588
You have no variable declared s1
. You merely added a control to the form having that name. You can retrieve it using:
Dim s1 As Object
s1 = Controls("s1")
My VB6 is rusty though so I’m not sure that, this being an Object
, you can meaningfully work with it yet.
Upvotes: 0