VB6 When closing the Parent form do unload queryunload or terminate events fire in Child Forms - events

I've asked this question before but I must not have been clear because the answer turned out not to be correct as far as I can tell .. so here goes again.
I have a VB6 application .. it has main menu that opens up Forms and dialogs (not MDI I don't think)
I want to save the position of any open forms or dialogs when the application closes so the next time I open the application the positions will be restored.
I hoped that when I close down the application that ANY of queryUnload, unload, terminate would fire in the 'child' windows or dialogs and I could save their position .. but nothing seems to fire.
I've put break points on all the above events, but when I close down the application nothing gets hit.
VB6 is not my usual gig .. so I'm probably thinking too much .net ..
Is there a way this can be done in VB6 ..
Edit: So it seems if I click the close cross in the top right corner I 'end' the application. Is there a way in VB6 to edit this behavior so I could instigate a graceful close down ?

All 3 events fire, but the Terminate event might not be fired when you expect it.
Create a test project consisting of 1 MDI form, 1 MDI child form, 1 normal form and add the following code:
MDI form:
'MDI form : name=MDIForm1
Option Explicit
Private Sub MDIForm_Click()
End
End Sub
Private Sub MDIForm_Load()
Form1.Show
Form2.Show vbModeless, Me
WindowState = vbMaximized
End Sub
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "MDI form QueryUnload event"
End Sub
Private Sub MDIForm_Terminate()
MsgBox "MDI form Terminate event"
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
MsgBox "MDI form Unload event"
End Sub
MDI child:
'1 form: name=Form1 MDIChild=true
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "MDI child QueryUnload event"
End Sub
Private Sub Form_Terminate()
MsgBox "MDI child Terminate event"
End Sub
Private Sub Form_Unload(Cancel As Integer)
MsgBox "MDI child Unload event"
End Sub
Normal form:
'1 form: name=Form2
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "Form2 QueryUnload event"
End Sub
Private Sub Form_Terminate()
MsgBox "Form2 Terminate event"
End Sub
Private Sub Form_Unload(Cancel As Integer)
MsgBox "Form2 Unload event"
End Sub
You will see the QueryUnload and Unload events are fired (in this order), but the Terminate event is fired when you close down the MDI form.
When you click the background of the MDI form, then End will be called and no event will be fired

"Child" forms do get the usual events. Notice that on Form_QueryUnload the UnloadMode parameter is vbFormOwner.
"Child" forms are shown with explcit owner form like this:
'--- using global references
Form2.Show vbModal, Form1
Form3.Show , Form1 '--- Form3 is modeless
'--- using instances
With New Form2
.Show vbModal, oOwnerForm
End With
With New Form3
.Show , oOwnerForm
End With

Related

KeyDown map to GUI form button downstate

Hi I would like to ask if it's possible to map the KeyDown for keyboards in Visual Basic 6 to turn the state of a graphical Command Button on the form to the "Down State" while keyboard key is pressed then back to raised when released? Thanks
I am aware of the problem here, because I did somewhat similar in the past and ended up by using an array of PictureBoxes instead of graphical CommandButtons.
Anyway, a simple workaround with CommandButtons is to keep the focus away by adding to the Form another control which can act as focus target. Remember: when a Form goes activated, it will place the focus to the first focusable control inside itself.
As You haven't specified in Your question what kind of keyboard state You need, below is a simple example with the a s d f keys. You will need less than 5 minutes to get it up and running.
Step 0:
Copy and paste following declarations to Your VB Form:
Option Explicit
Option Base 0
Const BM_SETSTATE = &HF3
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Target(254) As Long
Step 1:
Add to Your Form an array of CommandButtons called, for instance, Button(0), Button(1), Button(2), Button(3) and so on.
Set the properties which You need (Picture, DownPicture, etc.) and set also:
TabStop: False
Double-click one of this CommandButtons. You can see, You have just one entry point for the whole array of Controls. Choose GotFocus from the event drop-down and put this piece of code:
Private Sub Button_GotFocus(Index As Integer)
PicFocus.SetFocus
End Sub
Step 2:
On Your VB Form, set this property:
KeyPreview: True
Double-click the Form, choose Load from the event drop-down and set Your desired mapping between a KeyCode and the corresponding CommandButton:
Private Sub Form_Load()
Target(65) = Button(0).hwnd ' 65: KeyCode for "a"
Target(83) = Button(1).hwnd ' 83: KeyCode for "s"
Target(68) = Button(2).hwnd ' 68: KeyCode for "d"
Target(70) = Button(3).hwnd ' 70: KeyCode for "f"
End Sub
Choose KeyDown and KeyUp from the event drop-down and put inside the two global keyboard event handlers this piece of code - respectively -1 for the down-state and 0 for the up-state:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print KeyCode
Call PostMessage(Target(KeyCode), BM_SETSTATE, -1&, 0&)
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Call PostMessage(Target(KeyCode), BM_SETSTATE, 0&, 0&)
End Sub
Step 3:
Lastly, add to the same VB Form the PictureBox mentioned above and set following properties:
Name: PicFocus
Appearance: 0-Flat
BorderStyle: 0-None
HasDC: False
TabIndex: 0
TabStop: False
Width: 255
Left: -1000
Press Ctrl+F5 and test if this is what You need.
The CommandButton control has mouse and keyboard down and up events:
Private Sub Command1_Click()
Debug.Print "click"
End Sub
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "keydown"
End Sub
Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "keyup"
End Sub
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "mousedown"
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "mouseup"
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

Sending data before winsock closes in Visual Basic 6?

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

passing values present in one user control to another using VB

i have a user control with a text box and i need to access the value entered in this text box in a label present in another user control. How do i do that in vb.Thanks in advance.
create a shared event in the first user control (UserControl1):
Friend Shared Event GetTextBoxText(ByVal myString As String)
you can then raise this event with a button on (UserControl1)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'raise the event with the text from the text box
RaiseEvent GetTextBoxText(TextBox1.Text)
End Sub
on your second user control (UserControl2) add a handler to the event in your Constructor :
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'this will let us handle the event from (UserControl1)
AddHandler UserControl1.GetTextBoxText, AddressOf SetLabelText
End Sub
Private Sub SetLabelText(ByVal myString As String)
Label1.Text = myString
End Sub
now whenever you click the button on (UserControl1) the text will be displayed in the label on UserControl2
you could also add the event handler on any control and respond to GetTextBoxText event

Visual basic 6 events

How can i restrict an event to occur? Suppose i don't want textbox change event to occur when i press backspace.
Setting KeyAscii=0 in the KeyPress event will cause the keypress to be ignored.
Private Sub myTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyBack Then KeyAscii = 0
End Sub
Since the Change event doesn't pass you the code of the last key pressed, you'll have to store that in the KeyPress event, then you can immediately exit the Change event whenever the backspace key is pressed.
Private keyCode As Integer
Private Sub Text1_Change()
If (keyCode = vbKeyBack) Then
Exit Sub
Else
// do whatever it is you want to do in this event
// P.S.: I know this is the wrong comment syntax,
// but this code prettifier has a problem with
// VB6 comments
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
keyCode = KeyAscii
End Sub

Resources