开发者

VB6 Dynamically Adding Winsock Control

开发者 https://www.devze.com 2023-03-15 13:40 出处:网络
I\'m using the following code to dynamically create a WinSo开发者_Python百科ck control, and connect to a server using it:

I'm using the following code to dynamically create a WinSo开发者_Python百科ck 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.


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?


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! :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号