Sending data before winsock closes in Visual Basic 6? - vb6

I'm trying to send data to my server when my client closes the form or when the stop button is hit and for some reason it's not working.
Winsock.SendData "USERLEAVES" & txtUser.Text
Winsock.Close
It's like the winsock is closing before the data can be sent. How can I fix this?

have a look at the _SendComplete() event
for example, using a form level boolean :
Option Explicit
Private mblnClosing As Boolean
Private Sub Command1_Click()
Winsock1.SendData "USERLEAVES" & txtUser.Text
mblnClosing = True
End Sub
Private Sub Form_Load()
mblnClosing = False
End Sub
Private Sub Winsock1_SendComplete()
If mblnClosing Then
Winsock1.Close
End If
End Sub

Related

vb6 WebView2 get innerhtml and http cookies data

How do I Get the Login Cookies when am logged into a site?
I am searching for answers and now turned to StackOverflow pro members for your expertise and help to get this out of the way so I can move on to making my software.
i need to get the cookies login/visit /get/post anything how do i get it using the new webview2 browser kindly shed some light please anyone
Option Explicit
'Note, that this Demo requires the properly registered RC6-Binaries
'and in addition an installed "Chromium-Edge" (in its "evergreen" WebView2-incarnation)
'installable from its official MS-Download-URL: https://go.microsoft.com/fwlink/p/?LinkId=2124703
Private WithEvents WV As cWebView2 'declare a WebView-variable WithEvents
Private Sub Form_Load()
Visible = True '<- it's important, that the hosting TopLevel-Form is visible...
'...(and thus the Child-PicBox indirectly as well) - before we Bind the PicBox to the WebView
Set WV = New_c.WebView2 'create the instance
If WV.BindTo(picWV.hWnd) = 0 Then MsgBox "couldn't initialize WebView-Binding": Exit Sub
' Set WV = New_c.WebView2(picWV.hWnd) 'create the instance
' If WV Is Nothing Then MsgBox "couldn't initialize WebView-Binding": Exit Sub
End Sub
'*** VB-Command-Button-Handlers
Private Sub cmdNavigate_Click()
WV.Navigate "https://google.com" '<- alternatively WV.jsProp("location.href") = "https://google.com" would also work
'the call below, just to show that our initially added js-functions, remain "in place" - even when we re-navigate to something else
WV.jsRunAsync "test", 2, 3
End Sub
Private Sub WV_NavigationCompleted(ByVal IsSuccess As Boolean, ByVal WebErrorStatus As Long)
Debug.Print "NavigationCompleted welaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
End Sub
Private Sub picWV_Resize() 'when the hosting picBox got resized, we have to call a syncSize-method on the WebView
If Not WV Is Nothing Then WV.SyncSizeToHostWindow
End Sub
Private Sub picWV_GotFocus() 'same thing here... when the hosting picBox got the focus, we tell the WebView about it
If Not WV Is Nothing Then WV.SetFocus
End Sub
'*** the above two EventHandlers (of the hosting VB-PicBox-Container-Ctl) are basically all what's needed "GUI-Binding-wise"
'*** the rest of the EventHandlers below, are raised by the WebView-instance itself
Private Sub WV_InitComplete()
Debug.Print "WV_InitComplete"
End Sub
'Private Sub WV_NavigationCompleted(ByVal IsSuccess As Long, ByVal WebErrorStatus As Long)
'Debug.Print "WV_NavigationCompleted"
'End Sub
Private Sub WV_DocumentComplete()
Debug.Print "WV_DocumentComplete"
End Sub
Private Sub WV_GotFocus(ByVal Reason As eWebView2FocusReason)
Debug.Print "WV_GotFocus", Reason
End Sub
Private Sub WV_JSAsyncResult(Result As Variant, ByVal Token As Currency, ByVal ErrString As String)
Debug.Print "WV_JSAsyncResult "; Result, Token, ErrString
Text2.Text = Result
End Sub
Private Sub WV_JSMessage(ByVal sMsg As String, ByVal sMsgContent As String, oJSONContent As cCollection)
Debug.Print sMsg, sMsgContent
Select Case sMsg
Case "btn1_click": MsgBox "txt1.value: " & WV.jsProp("document.getElementById('txt1').value")
End Select
End Sub
Private Sub WV_LostFocus(ByVal Reason As eWebView2FocusReason)
Debug.Print "WV_LostFocus", Reason
End Sub
Private Sub WV_UserContextMenu(ByVal ScreenX As Long, ByVal SreenY As Long)
Debug.Print "WV_UserContextMenu", ScreenX, SreenY
End Sub

TextBox Value Disappears after closing and reopening form

I have two forms: Form 1 and Form 2.
Form 1 has two Buttons and Form 2 has one textbox.
On Button 1 Click event I am writing "My Text" in my Form 2 TextBox and on button 2 I am showing Form 2.
What is happening is when I close my Form 2 using close [X] button and reopen it value in my Form 2 Textbox Disappears.
Please Help how can I resolve this
Form 1 Code:
Private Sub Command1_Click()
Form2.Text1.Text = "Parth"
End Sub
Private Sub Command2_Click()
Form2.Show
End Sub
Form 2 Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Me.Hide
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Cancel=true
Cancel=UnloadMode<>vbFormCode
Me.Hide
End Sub
Form1:
Option Explicit
Private Sub Command1_Click()
Form2.Text1.Text = "Parth"
End Sub
Private Sub Command2_Click()
Form2.Show vbModal, Me 'Since we are the owner, Form2 unloads when we do.
End Sub
If Form1 was not made the owner you'd need to explicitly unload Form2 within Form1's Unload event handler to avoid hanging the program.
You need to be careful not to test for the wrong UnloadMode value.
Form2:
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = True
Hide
End If
End Sub

vb6 - sending message into winsock server chat

I've a simple winsock server chat and this is the code:
Private Sub Form_Load()
Winsock1.LocalPort = 5100
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData
Text1.Text = Text1.Text & sData
End Sub
I receive the message from an ios app but I can't send message with a "sendData".
How can I send message to all the clients? I need to use a client?
Thanks.
If you want to send messages to more than one client, then the best approach would be instead of closing your listening winsock1, and using it to accept the request, to create a new winsock control that will accept the request. This way you can accept connections from more than one source.
Example:
1st change winsock1's property Index to 0, to create a control array. Now all events's signature change to include the Index parameter.
Dim NumSockets As Integer
Private Sub Form_Load()
Winsock1(0).LocalPort = 5100
Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
NumSockets = NumSockets + 1
Load Winsock1(NumSockets) 'create a new winsock control
Winsock1(NumSockets).Accept requestID 'use that one to accept the request
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim vtData As String
Winsock1(Index).GetData vtData, vbString
Print vtData
End Sub

VBA Custom Event Not Found when Raised in UserForm

I was following this MSDN guide on creating custom events. I feel like I understand the process now, but I cannot figure out why I am getting a Compile Error: Event Not Found for RaiseEvent ItemAdded. The weird thing is, the ItemAdded event is recognized by the IDE (I can type it in all lowercase and it is then automatically formatted properly), so I know that it is recognized by VB.
DataComboBox Class Module Code:
Public Event ItemAdded(sItem As String, fCancel As Boolean)
Private pComboBox As Control
Public Property Set oComboBox(cControl As Control)
Set pComboBox = cControl
End Property
Public Property Get oComboBox() As Control
oComboBox = pComboBox
End Property
Private Sub Class_Initialize()
End Sub
Private Sub Class_Terminate()
End Sub
The UserForm contains two controls - a CommandButton named btnAdd and a ComboBox named cboData.
UserForm Code:
Private WithEvents mdcbCombo As DataComboBox
Private Sub UserForm_Initialize()
Set mdcbCombo = New DataComboBox
Set mdcbCombo.oComboBox = Me.cboData
End Sub
Private Sub mdcbCombo_ItemAdded(sItem As String, fCancel As Boolean)
Dim iItem As Long
If LenB(sItem) = 0 Then
fCancel = True
Exit Sub
End If
For iItem = 1 To Me.cboData.ListCount
If Me.cboData.List(iItem) = sItem Then
fCancel = True
Exit Sub
End If
Next iItem
End Sub
Private Sub btnAdd_Click()
Dim sItem As String
sItem = Me.cboData.Text
AddDataItem sItem
End Sub
Private Sub AddDataItem(sItem As String)
Dim fCancel As Boolean
fCancel = False
RaiseEvent ItemAdded(sItem, fCancel)
If Not fCancel Then Me.cboData.AddItem (sItem)
End Sub
You cannot raise an event outside the classes file level.
Add a routine like this inside "DataComboBox1" to allow you to raise the event externally.
Public Sub OnItemAdded(sItem As String, fCancel As Boolean)
RaiseEvent ItemAdded(sItem, fCancel)
End Sub
Then call the OnItemAdded with the current object.
Example...
Private WithEvents mdcbCombo As DataComboBox
...
mdcbCombo.OnItemAdded(sItem, fCancel)

VB6: get onreadystate value from browser control

I'm trying to get the onreadystate value from the browser control do detect when the page has finished loading. Unfortunately, the event's .returnValue gets returned empty.
Here's my code:
Dim WithEvents m_doc As HTMLDocument
Private Sub Form_Load()
Set m_doc = WebBrowser1.Document
End Sub
Private Sub m_doc_onreadystatechange()
Dim m_event As IHTMLEventObj
Set m_event = m_doc.parentWindow.event
m_value = "'" & m_event.returnValue & "'"
MsgBox "onreadystatechange: " & m_value
End Sub
Any ideas on what's wrong?
If you want to use the HTMLDocument's events try
m_doc.createDocumentFromUrl "http://www.microsoft.com", ""
Otherwise you can use the WebBrowser control's event to detect when a document is completely loaded or call the Navigate or Navigate2 method and immediately loop while polling the WebBrowser.ReadyState
WebBrowse1.Navigate2 "http://www.microsoft.com"
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Of course don't forget to add an error handler.

Resources