Where do I control the behavior of the "X" close button in the upper right of a winform? - vb.net

I'm venturing into making my VB.NET application a little better to use by making some of the forms modeless.
I think I've figured out how to use dlg.Show() and dlg.Hide() instead of calling dlg.ShowDialog(). I have an instance of my modeless dialog in my main application form:
Public theModelessDialog As New dlgModeless
To fire up the modeless dialog I call
theModelessDialog.Show()
and within the OK and Cancel button handlers in dlgModeless I have
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Hide()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Hide()
End Sub
and that seems to work fine.
The "X" button in the upper right is getting me, though. When I close the form with that button, then try to reopen the form, I get
ObjectDisposedException was unhandled. Cannot access a disposed object.
I feel like I'm most of the way there but I can't figure out how to do either of the following:
Hide that "X" button
Catch the event so I don't dispose of the object (just treat it like I hit Cancel)
Any ideas?
The class of this dialog is System.Windows.Forms.Form.

Catch the FormClosing event and, if the reason is UserClosing, set Cancel on the event to true.
Something like the following:
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles Form1.FormClosing
if e.CloseReason = CloseReason.UserClosing then
e.Cancel = true
Me.Hide()
end if
End Sub

Use Me.Close() to hide the form. To open it, use the following snippet:
If theModelessDialog.IsDisposed Then
theModelessDialog = New dlgModeless
End If
dlgModeless.Show()
If this is saving data, then you'll need to figure some way of storing it (perhaps in a static variable/s in the form). This is the proper way to do what you are trying to achieve though.
You'll also have to forgive me if my VB is off, it's been a while.

the formclosing event allows me to do a managed exit of the form so I have included a question to confirm to exit. I also have a form flag bterminate to force the cancel where i want it to and therefore not ask the question. Thanks your suggestion helped me as well :)
Dim msgboxresponse As MsgBoxResult
If e.CloseReason = CloseReason.UserClosing Then
If Not Me.bTerminate Then
msgboxresponse = MsgBox("Are you sure you want to cancel adding?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
If msgboxresponse <> MsgBoxResult.Yes Then
e.Cancel = True
Return
End If
End If
End If

#John was Hiding the form in his code and the answers above provide a solution to that case. Often, though, you are not planning to use the form again, so you really do want the form to be Disposed. All Close related activities will be in one place if you Handle the FormClosing event using Me.FormClosing by adding it to anyCancel/Close/Exit code that you already have. e.g. in #John's case:
Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Cancel_Button.Click, Me.FormClosing
....More code
Me.Dispose
End Sub
Note the use of the Me.Dispose instead of any existing Me.Close. If you leave the Me.Close you'll create an infinite loop. See this for the subtle differences between Close and Dispose.

Agree with handling the FormClosing event. Or change the properties on the form to hide the system X control.

I've tried everything and it didn't work
if you just want to close, without showing an messagebox, you will just need:
Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
>e.Cancel = False
>FORM2.Show() (if you want to show another form)
End Sub
Hope this helps you...!

Related

calling event programmatically

I have a datagridview and i want to delete a row selecting the row, pressing "delete" button.
and clicking my button "cmdsave" to do so. works great.
However the code below doesn't delete the record.
what arguments do i put in cmdsave_Click(SENDER???, e???)
private Sub datagridview_UserDeletingRow(sender As Object, e As DataGridViewRowCancelEventArgs) Handles datagridvieuw.UserDeletingRow
If MsgBox("are you sure?", MsgBoxStyle.YesNo, "delete this record") = MsgBoxResult.Yes Then
cmdsave_Click(SENDER???, e???)
Else
e.Cancel = True
End If
End Sub
Private Sub cmdsave_Click(sender As Object, e As EventArgs) Handles cmdsave.Click
Me.myadapter1.Update(Me.dataset.mydatatable)
end sub
To answer the specific question you asked:
what arguments do i put in cmdsave_Click(SENDER???, e???
Anything you like; you never make use of either of the variables so you can put anything at all and it won't matter
Nothing is probably simplest
-
As you have figured out, you only call one method in the save click anyway, so it's probably simplest to just call that same method in both places

How to share events between forms

So i have a tray icon that should behave the same way between 3 forms. I then created this code:
Private Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
TrayForm.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
Which is used to handle the tray icon. How can i do to share this event between the forms so i don't have to place this same code on every form?
How are event handlers working exactly? I looked online and on MSDN and it is not clear to me.
Thanks
Are you sure that you want to share the event, and not juste the code that will handle the event?
If you don't want to copy and paste your code, which you need to handle the events of more than one form, here's a way to do it:
Declare the sub which contains the code needed to handle the event as a public shared sub. Like this:
Public Shared Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs)
So, now you have a Sub which can handle the event you want to handle from all three forms.
Now, when you initialize those forms, add a line to make the shared Sub handle the event you want it to handle:
AddHandler NotifyIcon1.MouseClick, AddressOf ProjectName.FileName.TrayForm_MouseClick
ProjectName.FileName is meant here to be the path to refer to the shares Sub inside the file where you put it. I usually name it like ProjectNameUtils.vb or something like that.
If you just want to avoid copy and pasting your Sub so you don't have to modify it at several places every time you change something, this could be a way to achieve that.
As Stipulated by Hans Passant:
Sub Eclass_EventHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
Me.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
On the Trayform.VB just did the trick.
But about the shared event. i Have one that would have to be:
Private Sub FormClosingEVENT(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromMenu Then e.Cancel = True
Me.WindowState = FormWindowState.Minimized
'Application.Exit()
End Sub
How should i handle this?

vb.net: Distinguish between me.close() and x-button

how is it possible to distinguish between the event, that the user explicitly pressed to x-button (or pressed ALT F4) to close a form and all other methodes of closing the form programatically (me.close(), etc).
I already figured out, that this could be done using sender objects but I don't really get it. Could somebody explain it for me with an example?
Thank you all very much in advance.
The class FormClosingEventArgs event args has an enumeration to tell what the reason the form is closing.
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason(v=vs.110).aspx
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.Closing
If Not appClosing AndAlso e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then
' DO WHATEVER CODE YOU WANT IN HERE
' LIKE SETTING E.CANCEL TO TRUE
End If
End Sub
' You can also set a close appClosing local variable at the class level as a boolean, whenever you call closing in code, assign the variable.
appClosing = True
Me.Close();
It's as simple as that. :)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
e.Cancel = True
End Sub
This will prevent the X button to close the form and also the :) .The issue is that if you do this you cannot close your form using Me.Close too , To fix this,just remove the EventHandler :)
RemoveHandler Me.Closing, AddressOf Form1_FormClosing
'Now close the form
Me.Close()

A function is called twice in VB.NET

I have a function that is called twice and I don't know what to do.
This is the code that is called when I press an input button on a WebBrowser:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
Private Sub Document_Click(sender As Object, e As HtmlElementEventArgs)
Select Case Document.ActiveElement.Id.ToLower
Case "global" : prueba()
Case Else
End Select
End Sub
If you want to see the function called prueba() here it is: http://pastebin.com/Fi5LLX2N
I have a video where I show it, but the annotations are in Spanish: http://www.youtube.com/watch?v=OCJXk3qJwVA
Well I have another problem with my function, as you can see, on the bottom I have put this:
Else
MsgBox("Este ModPack ya lo tienes instalado!")
End If
But it doesn't work. :(
Try this:
PS: It's written on the fly maybe can have some syntax error.
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
try : removehandler Document.Click, addressof(Document_Click): catch : end try
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
My immediate reaction is a sticky mouse button, but really it's likely due to the web page you are loading having multiple pages being loaded, thus adding the duplicate event handler. Put a breakpoint on this line of code:
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
You'll likely see it being hit twice. Make sure to only wire up one HtmlElementEventHandler to avoid the double firing of the click event handler. You can check e.Url for a match before wiring up as a possible solution.
in vb.net there is no need to define onclick in html for button because it is handled automatically.so if you are doing this then the click event will fire twise.

Ok button in VB.net

I have a form that contain a combobox, I'm inserting an error check to check if the user hit the ok button before selecting a valid value from the combobox it mask the error and keep the form on focus, till the user select a proper value, but in my case after raise the error the application still close after i press Ok, would you help me identify my mistake
here is my code
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Try
If SymbolComboBox.SelectedValue Is Nothing Then
Throw New Exception()
Else
ErrorProvider1.SetError(SymbolComboBox, String.Empty)
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End If
Catch ex As Exception
ErrorProvider1.SetError(SymbolComboBox, "Error")
End Try
End Sub
Thank you in advance
Jp
Is your OK button the default accept button for the form ? If so, I beleive it will always return System.Windows.Forms.DialogResult.OK no matter the outcome, unless you specifically cancel the action. I do not remember how to cancel the action though, but if it's the case, I sugesst you remove that AcceptButton property of your form.
OR,
Is it possible that SymbolComboBox.SelectedValue is never 'Nothing' even when nothing is selected (like an empty string) ? I would use selectedIndex instead and check if it's equal to -1.
Hope that helps.
I'm not sure, but I do know that using exceptions like this to control program flow is a bad technique, and in this case unnecessary:
Private Sub OK_Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles OK_Button.Click
If SymbolComboBox.SelectedValue Is Not Nothing Then
ErrorProvider1.SetError(SymbolComboBox, String.Empty)
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
Else
ErrorProvider1.SetError(SymbolComboBox, "Error")
End If
End Sub
Why not look at using a required field validator to do this? Are you doing Web forms or Win Forms development? Also what is the value of SymbolComboBox.SelectedValue when you get this issue?