Newbie ShowDialog query...how to close the modal dialog

G

Guest

Sorry for another query, but I'm have trouble handling a simple modal form.
The form (Form2) has only one textbox and two labels on it and I can show it
modally by including the following code in a button click event:

Private Sub Button1_Click(ByVal....)
Dim testDialog as New Form2
testDialog.Textbox2.Text = "Some text"
testDialog.ShowDialog(Me)
testDialog.Dispose
End Sub

When the Form shows, the text "Some text" appears in the textbox and I can
overwrite it using the keyboard. My problem is, how can I get the form to
close using either the Enter key, in which case the new textbox contents can
be passed back to the owner form (I guess this corresponds to the
DialogResult.OK response), or the Esc key, in which case the form closes
without passing the textbox contents?

I have tried using:
If testDialog.ShowDialog(Me) = System.Windows.Forms.Dialogresult.OK Then
..
ElseIf testDialog.ShowDialog(Me) =
System.Windows.Forms.Dialogresult.Cancel Then
..
End If
in place of the simple 'testDialog.ShowDialog(Me)' statement but the modal
form remains on-screen no matter what key is pressed.

Any comments would be gratefully received, as I need to use model forms
(some with and some without buttons on in addition to the labels and
textboxes) to maintain consistency with the VB4 version of our application.

P.S. Many thanks to Moshazu and Phill W. for responding to my RaiseEvent
query earlier today.
 
C

Cor Ligthert [MVP]

LCA,

I would not use old DOS or Access habits in modern windows applications.

(This is not the first time your question is answered like this and not only
by me)

A user of modern windows/webform programs is today used to certain
behaviour, don't do it in a way that confuses him.

Just my idea

Cor
 
O

Oenone

LCAdeveloper said:
My problem is, how can I get
the form to close using either the Enter key, in which case the new
textbox contents can be passed back to the owner form (I guess this
corresponds to the DialogResult.OK response), or the Esc key, in
which case the form closes without passing the textbox contents?

Add two buttons to your dialog form and set their (Name) properties to btnOK
and btnCancel.

For btnOK, set its Text property to "OK" and its DialogResult property to
OK.

For btnCancel, set its Text property to "Cancel" and its DialogResult
property to Cancel.

Then edit the properties of the dialog form itself, and set the AcceptButton
property to btnOK, and the CancelButton property to btnCancel.

This sets up your two buttons so that when they are clicked, they
automatically close the form and return a DialogResult value appropriate to
their function. The changes to the Form properties set the form up so that
pressing Enter will automatically click the OK button, and pressing Esc will
click the Cancel button.

Finally, modify your calling function to read as follows:

\\\
Dim testDialog as New Form2
testDialog.Textbox2.Text = "Some text"
If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
MsgBox("Clicked OK, text value = " & testDialog.Textbox2.Text)
Else
MsgBox("The dialog was cancelled.")
End If
testDialog.Dispose()
///

Hopefully that will do the trick.
 
H

Herfried K. Wagner [MVP]

LCAdeveloper said:
Sorry for another query, but I'm have trouble handling a simple modal
form.
The form (Form2) has only one textbox and two labels on it and I can show
it
modally by including the following code in a button click event:

Private Sub Button1_Click(ByVal....)
Dim testDialog as New Form2
testDialog.Textbox2.Text = "Some text"
testDialog.ShowDialog(Me)
testDialog.Dispose
End Sub

When the Form shows, the text "Some text" appears in the textbox and I can
overwrite it using the keyboard. My problem is, how can I get the form to
close using either the Enter key

Add a button control to the form and assign it to the button's
'AcceptButton' property. Then enter this code in the button's 'Click' event
handler:

\\\
Me.DialogResult = DialogResult.OK
Me.Close()
///
 
G

Guest

Thank you to all who replied, especially Oenone (and Herfried who posted just
as I was trying Oenone's suggestion): your reply Oenone was a model of
clarity - Thank You! I don't suppose you have any suggestions as to how to
implement a RadioButton double-click event....?!!! :)

Cor Ligthert, I accept your point, however I'm finding it hard not to try to
adapt old ways instead of relearning from scratch. In an ideal world I would
try to do things in the modern windows/webform way, but my work/time
constraints do not allow this, hence adapting my old VB4 code to work in a
new vb.NET environment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top