Just wanted to ask, Is there a form_closing event equivalent in VB6? (.bas). the reason I ask is that I wanted to prevent first closing the application if there's an open sub-window.
something like this.
Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If hasSubWindow Then
MessageBox.Show("You currently have active sub-window(s)")
e.Cancel = false
End If
End Sub
There are three events: Form_QueryUnload(Cancel As Integer, UnloadMode As Integer), Form_Unload(Cancel As Integer) and Form_Terminate().
Form_QueryUnload fires before the Form_Unload where setting Cancel to any value other than 0 cancels the unload and UnloadMode indicates the cause of the unload event.
See https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-basic-6/aa445536(v=vs.60)?redirectedfrom=MSDN
Form_Unload takes an integer that if set to any value other than 0, cancels the Unload.
The Unload event procedure is where programmers usually put cleanup code. The Unload event fires after the QueryUnload event. The Unload event procedure takes a single parameter, the Cancel parameter. Unload's Cancel parameter works the same as QueryUnload's Cancel parameter.
The Terminate event happens when the instance of the form has been completely unloaded and all the form's variables have been set to Nothing. The Terminate event happens only when you explicitly set the form to Nothing in your code during or after the Unload event procedure.
Source: https://www.freetutes.com/learn-vb6-advanced/lesson6/p5.html
In your case, you probably want to put code into Form_QueryUnload or Form_Unload that checks if there are any other open forms.
Private Sub Form_Unload(Cancel As Integer)
If hasSubWindow = True Then
MsgBox "You currently have active sub-window(s)", vbInformation, "Confirm"
Cancel = 1
End if
End Sub
Related
I have a local sub that allows the user to move a row of a datagridview, triggered by a button click. The sub works fine in debugger but when it exits control is transfered to the calling form, i.e. the current form is closed. This also happens when no row is moved, i.e. when one of the abort conditions on entrance are met. Simply: exiting this sub will close the form!?!
Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click
Dim rowToGo As DataGridViewRow
Dim rtgIndex As Integer = 0
If (dgvAuftrag.RowCount <= 1) or (dgvAuftrag.CurrentRow Is Nothing) Then
Beep()
Exit Sub
End If
rowToGo = dgvAuftrag.CurrentRow
rtgIndex = rowToGo.Index + 1
If (rtgIndex >= dgvAuftrag.RowCount) Then rtgIndex = 0
Try
dgvAuftrag.Rows.Remove(rowToGo)
dgvAuftrag.Rows.Insert(rtgIndex, rowToGo)
Catch ex As Exception
IssueErrorMessage(ex)
End Try
End Sub
All other local subs and functions work normal, just this one behaves strange. Any ideas how to fix/avoid this bug?
This is not a solution of the problem but a functioning workaround based on the sugestion of Hans. I have introduced a global boolean var named OKtoExit which is intialized to false.
private OKtoExit as boolean = false
Then I have a new FormClose event handler that checks that var. If OKtoExit is false then e.Cancel = true and the handler exits. The regular Exit functions (Save and Quit) set OKtoExit to true, any other code leaves the values unchanged.
Private Sub Current_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not exitOK Then
e.Cancel = True
Exit Sub
End If
End Sub
As I said, this is just a workaround that has the same effect as a normal functioning VB-Code. I would appreciate if somebody could present a real solution!
After many months I discovered the true reason for the problem and I must give all credits to Hans Passant: I had a button on one of the first forms that had the Dialog Result property set to Cancel. This was a really beautyful button so many other buttons in the appliaction were a copy of this first button where I just modified the label. Thus they all led to the unwanted behavior that a form was closed as soon as a user clicked one of them no matter what the label said... After months I discovered that just by chance. Thanks to Hans again, I obviously overlooked his last hint "And look at the button's DialogResult property."!
How to add event handler to index control in VB6
I have indexed control:
txtInput(0), txtInput(1), txtInput(2)
but when i create event to them like txtInput(0)_Click, the error showing...
Please tell me how to fix it. Thanks!
Just add your code to the click event for the control. Since it's an array, the index for the control being clicked will be an argument on the event handler:
Private Sub txtInput_Click(Index as Integer)
If Index = 0 then
'code for first control
ElseIf Index = 1 then
'code for second control
Else
' and so on ....
End If
End Sub
According to Microsoft:
"In Visual Basic 6.0, the Click event is raised when the CheckBox state is changed programmatically. "
and this is exactly what i do not want.
I want click event only raise when i click on the checkbox and not when the state is changed.
Any idea how to do it ?
Thank you
you can set a flag when you are populating the form from code to ignore changes. This can get messy if the code is not organized well.
Form Level:
Public IgnoreChange As Boolean
Form Load:
IgnoreChange = False
Event:
If IgnoreChange Then Exit Sub
Your code:
frmReference.IgnoreChange = True
frmReference.Checkbox1.Checked = True
frmReference.IgnoreChange = False
Code should only respond to user actions
Not sure if it's the best way, but one way would be to have a variable called something like IgnoreEvents and set that to true right before changed the state programmatically.
Then in the event handler if that variable is true, you just exit the event handler without doing anything.
Put your Click event code in the MouseDown event instead. You'll have to manually set the checkstate though:
Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Check1.Value = IIf(Check1.Value = vbChecked, vbUnchecked, vbChecked)
' run other necessary code here
End Sub
My issue is that I need to change the mouse pointer's position within the MouseMove event, which causes infinite recursion. I need to suppress the MouseMove event which Me.Cursor.Position = newpos generates. How can I do that?
I read about Me.EnableEvents = False but this is not valid for Visual Studio 2005 and I could not find an equivalent.
What exactly are you trying to do? Maybe there's a better way. But assuming this is what you want, you could unsubscribe the event handler in your MouseMove event before changing the cursor position using RemoveHandler. Just add it back when you're done:
Public Class MyForm
Private Sub MyForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseMove
UnsubscribeEvents()
' change mouse pointer's position here...
ResubscribeEvents()
End Sub
Private Sub UnsubscribeEvents()
RemoveHandler Me.MouseMove, AddressOf MyForm_MouseMove
End Sub
Private Sub ResubscribeEvents()
AddHandler Me.MouseMove, AddressOf MyForm_MouseMove
End Sub
End Class
I have an application written in VBA for Excel that takes in a live data feed. Whenever there is a change in the data, various events are triggered within VBA.
I also have some UserForms with ComboBoxes on them. My problem is that when I click the down arrow on the ComboBox and try to make a selection, the moment I get an update from the data feed, the ComboBox resets. What I would like to do is pause the events while I am making my selection in the ComboBox and then unpause it when I am done. How do I generate this functionality?
Try this to turn off:
application.enableevents = false
And this to turn back on:
application.enableevents = true
To pause and show a message and to keep working on something during the pause. finally press button
Public Ready As Boolean
Private Sub Command1_Click()
Ready = True
End Sub
Private Sub Form_Load()
Me.Show
Ready = False
Call Wait
Label1.Visible = True
End Sub
Public Function Wait()
Do While Ready = False
DoEvents
Loop
End Function
Maybe you can put a flag to bypass the update event on your combobox until a selection is made.
Private bLock as boolean ' declare at module level
' When a user clicks on the combobox
Private Sub DropDownArrow_Click() ' or cboComboBox_Click()
bLocked = True
End Sub
' This procedure is the one that does the updating from the data source.
' If the flag is set, do not touch the comboboxes.
Private Sub subUpdateComboBoxes()
If Not bLocked then
' Update the comboboxes
End If
End Sub
' When the selection is made, or the focus changes from the combobox.
' check if a selection is made and reset the flag.
Private Sub cboComboBox_AfterUpdate() ' Or LostFucus or something else
if Format(cboComboBox.Value) <> vbNullString Then
bLocked = False
End If
End Sub
Hope that helps.