15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1144 ❘ ChaPTer 39 windOws fOrms<br />

That’s the simple way. The one drawback to this is that there isn’t any notification back to the calling code<br />

that myForm is finished <strong>and</strong> has been exited (unless you hook up the Closing or Closed events on the<br />

form). Sometimes this isn’t a big deal, <strong>and</strong> the Show method will work fine. If you do need some type<br />

of notification, ShowDialog is a better option.<br />

When the Show method is called, the code that follows the Show method is executed immediately. When<br />

ShowDialog is called, the calling code is blocked <strong>and</strong> will wait until the form that ShowDialog called is<br />

closed. Not only will the calling code be blocked, but the form will optionally return a DialogResult<br />

value. The DialogResult enumeration is a list of identifiers that describe the reason the dialog is closed.<br />

These include OK, Cancel, Yes, No, <strong>and</strong> several others. In order for the form to return a DialogResult,<br />

the form’s DialogResult property must be set or the DialogResult property on one of the form’s buttons<br />

must be set.<br />

For example, suppose that part of application asks for the phone number of a client. The form has a text<br />

box for the phone number <strong>and</strong> two buttons; one is labeled OK <strong>and</strong> the other is labeled Cancel. If you set<br />

the DialogResult of the OK button to DialogResult.OK <strong>and</strong> the DialogResult property on the Cancel<br />

button to DialogResult.Cancel, then when either of these buttons is selected, the form becomes invisible<br />

<strong>and</strong> returns to the calling form the appropriate DialogResult value. Now notice that the form is not<br />

destroyed; the Visible property is just set to false. That’s because you still must get values from the<br />

form. For this example, you need to get a phone number. By creating a property on the form for the phone<br />

number, the parent form can now get the value <strong>and</strong> call the Close method on the form. This is what the<br />

code for the child form looks like:<br />

namespace FormsSample.DialogSample<br />

{<br />

partial class Phone: Form<br />

{<br />

public Phone()<br />

{<br />

InitializeComponent();<br />

btnOK.DialogResult = DialogResult.OK;<br />

btnCancel.DialogResult = DialogResult.Cancel;<br />

}<br />

public string PhoneNumber<br />

{<br />

get { return textBox1.Text; }<br />

set { textBox1.Text = value; }<br />

}<br />

}<br />

}<br />

The first thing to notice is that there is no code to h<strong>and</strong>le the click events of the buttons. Because the<br />

DialogResult property is set for each of the buttons, the form disappears after either the OK or Cancel<br />

button is clicked. The only property added is the PhoneNumber property. The following code shows the<br />

method in the parent form that calls the Phone dialog:<br />

Phone frm = new Phone();<br />

frm.ShowDialog();<br />

if (frm.DialogResult == DialogResult.OK)<br />

{<br />

label1.Text = "Phone number is " + frm.PhoneNumber;<br />

}<br />

else if (frm.DialogResult == DialogResult.Cancel)<br />

{<br />

label1.Text = "Form was canceled. ";<br />

}<br />

frm.Close();<br />

This looks simple enough. Create the new Phone object (frm). When the frm.ShowDialog() method is<br />

called, the code in this method will stop <strong>and</strong> wait for the Phone form to return. You can then check the<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!