The Ok button of MessageBox closes the form - vb.net

I have a main form formA and empty form formB, formA opens formB, on the load event of formB it add a user control using Activator.CreateInstance(ControlType) .. the user control has Submit and Cancel buttons.
this is the btnOk.click event
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Try
If OnSubmit() = cSUCCESS Then
OnSubmitRaiseEvent(e)
Else
'
' what should I write here to Not close the form if onSubmit failed ?
'
End If
Catch ex As Exception
WriteToEventLog(ex)
End Try
End Sub
the OnSubmit() method checks for some data if valid or not, if not, it shows a message Box with OK button. The problem is that OK button on the message box closes the formB that is hosting the user control.
I want Message box to close and user can resume working on the form
How can I solve this?

Related

VB.net How to hide dialogue without close the application

I have problem about close() or dispose() function with my barcode reader (Windows Embedded Compact 7). In this case I can only hide() form.
I tried to show Form2 as dialogue but after I clicked the close button (to hide this form and go back to Form1) It made all of my application close
In Form1 (Main):
Public Sub showForm2()
Dim secForm As New Form2
secForm.ShowDialog()
End Sub
In Form2:
'close button
Private Sub closebt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles closebt.Click
Me.Hide()
End Sub
Form can't be hidden if it is shown as Dialog. If you want to hide the form then use form.show() rather than form.ShowDialog(). Also here is a link
http://www.vbforums.com/showthread.php?759061-How-can-i-hide-my-second-form-dialog-without-bliking-form-not-closing-my-first-form
Go to properties page of the project. In the Application tab, there is a setting:
Shutdown mode
When startup form closes
When last form closes
Choose "When last form closes" to prevent closing the application when your main form closes.
in the index form add in form closing the fallow code:
Form1.Dispose()

Load new form from button and from timer are handled differently

When i login into my application and i get to the menu form, after the form triggers the form.shown after 1 second timer i start an update procedure.
This procedure calls another form which is on top the menu form, and does certain things.
This update form can also be called from a button that is on the menu form.
The problem here is when i call this form from the button the taskbar is hidden and also the menu form which is at the back is unclickable(I set my application to be full screen so taskbar is not visible). When the update form opens from the timer the taskbar appears, and the menu form which is at the back is clickable, and the user can click buttons from the parent form.
I tried setting focus on the update form after is loaded from the processes of windows but it didn't change anything.
I dont want to show the taskbar nor have clickable parent form.
Any idea why is this happening and how can i fix it?
Here is how i call my form from timer and from the button:
Private Sub TimerDelayedStart_Tick(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
TimerDelayedStart.Stop()
LoadUpdateDatabase()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
LoadUpdateDatabase()
End Sub
Private Sub LoadUpdateDatabase()
Try
fUpdateDatabase = New frmUpdateDatabase
With fUpdateDatabase
.ShowDialog()
Cursor = Cursors.Default
End With
Catch ex As Exception
End Try
End Sub

Terminate program when Close button is Clicked after Prompt - Visual Basic

I have two forms in my WindowsFormApplication named as Form1 and Form2. The idea is when the Program is being closed, it shows up a Dialog Box to confirm it.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You are About to cancel the Setup.","Cancel Setup?",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End Sub
End Class
Until here, my code worked fine but the problem is when I click Button1, the Message Box appears to confirm the closure of Form1.
I don't want this to happen so then I tried changing Me.Close() to Me.Hide. I was successful for preventing the message box to appear but then I got another Problem. As the Form hides, it stays active in the background and I also don't want this to happen.
Another thing I added in Form1_FormClosing is Me. Close and Form2.Close. This enables to close both the forms once the program's active Form is being closed. But Again, there's a problem. As soon as I click the close button, the Message Boxes fill up the screen and not listening to my Command. Anyone got a solution for this?
So..
If the user clicks close in Form1, then the program should terminate with no messagebox.
If the user clocks close in Form2, Form3 or Form4, the program would Terminate when the user clicks Yes in the MessageBox or else nothing would get affected (Especially the Data).
The way this works is when user clicks Close in Form1, the program terminates with no MessageBox and if the user clicks Close in other Forms, A MessageBox would appear asking whether you are sure to close the Program. If the user clicks Yes, the program will Terminate because the DialogResult is Yes and Form1.Closing Cancel goes to False and Closes Form1(As Closure type was set as First Form Closes in Program Properties, this will terminate the program) Else the Form1.Closing Cancel goes to True which will prevent the current Form from closing and losing any Data.
This code goes to Form1:
Imports System.ComponentModel
Public Class Form1
Friend closeProgramAlreadyRequested As Boolean = False
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
closeProgramAlreadyRequested = False
End Sub
End Class
This code goes to Form2 & applies to other Forms as well but except for Form1:
Imports System.ComponentModel
Public Class Form2
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Form1.closeProgramAlreadyRequested = False Then
Dim result As DialogResult = MessageBox.Show("You are About to cancel the Setup.", "Cancel Setup?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
If result = DialogResult.No Then
e.Cancel = True
Else
e.Cancel = False
Form1.Close()
End If
End If
End Sub
End Class

How to hide a Dialog without hide the Main Form in vb.net?

In the main form "frmMain.vb" I have a button that when it is clicked runs the following code:
Dim myform as New frmMyDialog
myform.ShowDialog()
On the "frmMyDialog.vb" form the user can start a public function of the main form and I need to hide the dialog until the function ends, so I wrote this code:
Private Sub btnStartProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnStartProcess.Click
'hide the dialog
Me.Hide()
'start the sub of the main form
frmMain.TestSub()
'close the dialog
Me.Close()
End Sub
However, when the dialog becomes hidden, the main form is minimized. How can I hide the dialog without also hiding the main form?
(The "formBorderStyle" property of "frmMyDialog.vb" is "FixedDialog", I don't know if it can help.)

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 :-)