Change Close button in window form - vb.net

Is there a way to change the command of the (x) Button?
I want to show another form and hide the current form instead of closing the program.
where should I put this? :
Me.Hide()
Form.Show()
I tried puttin it on the Form Closing/Closed Event, but nothing happen, am I missing something?

Sure, the FormClosing event is made to do that. You'll need to pay attention to the reason the form is being closed, something like this:
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
MyBase.OnFormClosing(e)
If Not e.Cancel AndAlso e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
Dim frm = New Form2()
frm.Show()
Me.Hide()
End If
End Sub
Do note that you have to give a way for your user to terminate the app.

Related

How can I use the .Show() method in such a way the user can't click other forms already opened in background?

Now i'm using a'jobdone' flag and the following behaviour (that it looks quite horrible to me...):
Dim NewLoginForm As New LoginClass
LoginClass.jobdone = False
NewLoginForm.Show()
While (LoginClass.jobdone = False)
Application.DoEvents()
End While
NewLoginForm.Close()
If I try to use ShowDialog() the behaviour is better but it's a problem to manage all the opening and closing windows (if the forms are many) and I notice that all the background already opened forms close themselves either if one ShowDialog() form is closed...
Thanks
Ran a quick test and this worked perfectly:
Public Sub ForceOpen(ByRef frm As Form)
frm.Show()
For Each otherForm As Form In Application.OpenForms
If otherForm Is frm Then
AddHandler frm.FormClosing, AddressOf BlockFormClosing
Else
otherForm.Enabled = False
End If
Next
End Sub
Public Sub BlockFormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
e.Cancel = True
End Sub
Public Sub EnableOpenForms()
For Each frm As Form In Application.OpenForms
frm.Enabled = True
RemoveHandler frm.FormClosing, AddressOf BlockFormClosing
Next
End Sub
The calling form will open the stay-open form with ForceOpen(FormStayOpen). When the stay-open form's conditions for allowing the user to close it have been satisfied, have it call EnableOpenForms().

Start a process in a VB form without clicking a button

Here is my situation.
I have a windows from application in VB.NET. In the my first form there are two checkboxes. If the one is checked it goes to Form2, if only the second one is checked it goes to Form3. If both are checked then i want first to go on Form2 and when this process is finished go to Form3. All good until this point.
However, I want Form3 to start executing its code (which is inside a background worker) without pressing any button (which is the case when only the second checkbox is checked). Is this possible?
Thanks In advance!
Extra info....
To navigate between the Forms I use a Show/hide/Close scheme. For example:
If CheckBoxTrain.CheckState = CheckState.Checked Then
Me.Hide()
Form2.Show()
ElseIf CheckBoxTrain.CheckState = CheckState.Unchecked And CheckBoxEvaluate.CheckState = CheckState.Checked Then
Me.Hide()
Form3.Show()
At the end of the code in form2 i check if the second checkbox is also checked:
If Form1.CheckBoxTrain.CheckState = CheckState.Checked And Form1.CheckBoxEvaluate.CheckState = CheckState.Checked Then
Form3.Show()
Me.Hide()
End If
Then in Form3 there is a button in which after pressing it i have all the code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To n_monte
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
Next
End Sub
All i want is for this last piece of code is to start running without pressing any buttons (only for the case in which both checkboxes are checked)
Declare a Boolean property in Form3 and set that property in Form1 based on whether or not both CheckBoxes are checked. In the Load event handler of Form3, start the BackgroundWorker or don't based on the value of that property.

How can we show the parent form after showing the report Form?

I have a problem in showing the parent form and report form at the same time.When user click on the parent form for print it should pop up with yes or no button when user click on yes button it should print the form screen shot image,if we click "No" button it should show the crystal report.
When we click 'No' button it should show the crystal report.So to show the messagebox i have done like this
me.hide()
if MsgBox('Do you want to print screen shot image?') then
'Print screen shot image
me.Show()
else
'Show CxReport
me.Show()
end if
When I Did like this the parent form is strucking and unable to perform operations.
It's not standard practice to hide a form when showing a dialog. Completely remove the me.hide and me.show lines and try again.
I know that this question is an old one and you probably have figured it out by now, but I thought I would add an answer for future reference. All Forms have a FormClosing and FormClosed Event that you can attach a handler to in your creating Form. Here is a simple example of what I am trying to say.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
If MsgBox("Open Report?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim frm2 As Form2 = New Form2
AddHandler frm2.FormClosed, AddressOf ReportClosing
frm2.Show(Me)
Else
Me.Show()
'Do your work for printing form here
End If
End Sub
Private Sub ReportClosing(sender As Object, e As FormClosedEventArgs)
'Remove handler to prevent memory leaks
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf ReportClosing
Me.Show()
End Sub
End Class
Atlast I Found the way to get the access the parent Page hiddenField Value as given below
function getParentPageHField() {
var parentPageHField = window.opener.document.getElementById('hSelectedStandard').value;
document.getElementById('hStandard').value = parentPageHField;
}
Thanks for your inputs :-)

What is the best way to detect all ways of closing a form?

I have a form that i close using End several times in my code. I want to do a command to save settings when I do this called VarsToIni() which takes some public variables and saves them in an INI file. I have tried putting it in the main window's FormClosing (which stays open throughout) and this only works when you close from pressing the X button not from my End statement.
Add a new Sub and replace calls to End with calls to your new Sub:
Sub EndMe()
VarsToIni()
Application.Exit()
End Sub
Edit:
As Dan points out, End() is a bad way to close the application, Application.Exit() is preferred.
Consider using Application.Exit() instead of End. This allows FormClosing to be called no matter what (and you can handle it based on how it is being closed).
Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
' This is the result of Application.Exit()
e.Cancel = False
Case CloseReason.UserClosing
' This is the result of clicking the red X
Select Case MessageBox.Show("Are you sure you wish to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case DialogResult.Yes
e.Cancel = False
Case DialogResult.No
e.Cancel = True
End Select
Case Else
e.Cancel = False
End Select
If Not e.Cancel Then
VarsToIni()
End If
End Sub
I am quite confused by your question, nonetheless:
This will close all your forms
For each x as Form in My.Application.OpenForms
'You can then put your VarsToIni() here
x.Close()
Next
Note: Add this to your import
Imports System.Windows.Forms

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

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...!