Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showdialog and then close it

Status
Not open for further replies.

luisve

Programmer
Jun 24, 2005
29
US
hi!! i have a program with two forms, the program starts on form1, on the load event i show the form2, this works really good but i have a cancel button on the form2, if the user click this button i have to close all the program, i am ush=ing dialogevent.cancel after the user click button1 on form2 and then on form1 i take the eventdialog and if it is =cancel. i use me.close. but this give a error that says error system.forms handling window can someone help me?
 
You could take this approach:

On form2 add a public property as boolean. If the user hits the cancel button set the property to true. When form2 closes you check the property and go from there.

So, it would look something like this:

On form2, in the declaration section you would have...

Private blnCancel As Boolean

Public Property Cancel() As Boolean
Get
Return blnCancel
End Get
Set(ByVal Value As Boolean)
blnCancel = Value
End Set
End Property


Then in the button click even you would have...

blnCancel = True
Me.Close()

Lastly, in form1's load event you would do this...

Dim o As New Form2
o.ShowDialog()

If o.Cancel Then
Application.Exit()
End If


There may be a simpler way to do this, but this certainly works well. I think it's agood approach.

Dale
 
It sounds like your problem is that you eventually try to unload a form during the load event. I'd move your logic of showing Form2 from Form_Load to Form_Activated.

--
James
 
this is the error that i am getting, i changed the code to the activated event but same result.

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in system.windows.forms.dll

Additional information: Unable to get the window handle for the 'AxAtmTerminal' control. Windowless ActiveX controls are not supported.
 
Well it sounds to me like your trying to use a custom dialog box.

Try putting this in your Cancel button click event instead of Me.Close():
Me.DialogResult = DialogResult.Cancel

And for a successfully closing use another DialogResult like DialogResult.Ok.

This will go back to Form1 and allow you to use the DialogResult that was passed back.

That could be like this:
Code:
Dim o As New Form2
If o.ShowDialog() = DialogResult.Cancel Then
  End
End If

Hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top