Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 Express Edition - From Novice To Professional (2006)

.pdf
Скачиваний:
387
Добавлен:
17.08.2013
Размер:
21.25 Mб
Скачать

240 C H A P T E R 9 W I N D O W S A N D D I A L O G S

Set the Name property of the CheckBox control to letFormCloseButton.

Click on the form itself, and then find the FormClosing event in the Properties window and event browser. You can see the event in Figure 9-10.

Figure 9-10. Just as you can with a control, you can browse the full list of events for a form in the Properties window.

Double-click the entry area next to the event to drop into the code editor. What I want you to do here is check to see whether the check box has been clicked before you allow the form to close down.

C H A P T E R 9 W I N D O W S A N D D I A L O G S

241

Take a look at the event handler code you currently have:

Private Sub Form1_FormClosing(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.FormClosingEventArgs) _

Handles MyBase.FormClosing

End Sub

The second parameter passed into the handler, e, is of type FormClosingEventArgs. This object has a Boolean property called Cancel. Usually Cancel is set to False, meaning, “Don’t cancel this event.” However, your code can decide to set it to True. If this happens, the form will stop unloading instantly, effectively cancelling the user’s request to close the form down.

Go ahead and add some code to do just that:

Private Sub Form1_FormClosing(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.FormClosingEventArgs) _

Handles MyBase.FormClosing

If Not letFormCloseButton.Checked Then e.Cancel = True

End If

End Sub

Run the program now. If you try to close the form by clicking its Close icon, it will steadfastly refuse. Click the check box and try to close the form, and it goes away nicely.

When you take a look at message boxes shortly, you’ll see how to actually question users about their decisions graphically.

242 C H A P T E R 9 W I N D O W S A N D D I A L O G S

MDI (Multiple Document Interface)

User interfaces tend to have fashions and phases much like anything else in life. In the mid-90s Multiple Document Interface (MDI) was extremely popular, but today the trend seems to be more toward Outlook-style user interfaces with a single window showing everything but subdivided into moving frames.

Despite that, MDI is still a useful user interface technique. An MDI application has a single parent form that “contains” inner forms. These inner forms typically each hold a document, or some specific piece of information, hence the name Multiple Document Interface. SQL Server’s Query Analyzer, for example, still uses MDI for its user interface (see Figure 9-11).

Figure 9-11. SQL Server is one of the few Microsoft products that still uses MDI for its user interface paradigm.

C H A P T E R 9 W I N D O W S A N D D I A L O G S

243

The great thing is, if you need to produce this kind of user interface, it’s very easy to do with .NET.

Try It Out: Creating an MDI Application

Start up a new WinForms project in Visual Basic 2005 Express. When the main form appears, click on it once and look in the Properties window for a property called IsMdiContainer (see Figure 9-12).

Figure 9-12. Setting a form’s IsMdiContainer property to True lets it contain other child forms.

Set this property to True. You’ll notice the form change style—its color will darken and the border will change slightly. This is to show you in the editor that this is now an MDI container form.

Prior to .NET you were limited in which controls you could drop onto an MDI container form. Typically you’d just want a menu bar, toolbar, and status control on the form. However, because we haven’t covered those controls just yet, and .NET does let you put other controls onto an MDI container, drop a button on the form, near the top. Call it newWindowButton (see Figure 9-13).

244 C H A P T E R 9 W I N D O W S A N D D I A L O G S

Figure 9-13. .NET lets you drop controls onto container forms, something that you couldn’t do very easily before.

Now, let’s add a child form to the project. Just as you did in the earlier example with more than one form, right-click on the project in the Solution Explorer and use the pop-up menu to add a new form to the project. Call it ChildForm.vb.

When that’s done, go back to the MDI form and double-click the button to drop into the code editor for its Click event. What you’re going to do is open a form just as you did before, but this time you’ll make it an MDI child form. You do that simply by setting a form’s MdiParent property to the form that is the parent. Confused? Go ahead and type this code in:

Private Sub newWindowButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _

Handles newWindowButton.Click

Dim newChild As New ChildForm() newChild.MdiParent = Me newChild.Show()

End Sub

C H A P T E R 9 W I N D O W S A N D D I A L O G S

245

Notice the second line of code there. Because your code is inside the MDI container form, you can just set the MdiParent property of the new form to Me. Because your code lives inside a form, the keyword Me is used to refer to that form.

Run the application now, and click the button in the MDI form a few times (see Figure 9-14).

Figure 9-14. MDI containers “contain” child forms.

Every new form that opens up is “contained” within the parent. You can’t drag the windows outside of the parent form at all. If you keep clicking the button, you’ll quickly find that the MDI container becomes a bit of a mess with new windows all over the place. Thankfully, it’s very easy to rearrange them through code.

Forms have a method called LayoutMdi() that does something useful only when the form in question is an MDI container. The method takes one property, an enum from MdiLayout that can be any of

MdiLayout.ArrangeIcons, MdiLayout.Cascade, MdiLayout.TileHorizontal, or

MdiLayout.TileVertical. Let’s add a couple more buttons to your MDI container to see LayoutMdi() at work.

Drop two more buttons onto the MDI container, and name them cascadeButton and tileButton (see Figure 9-15).

246 C H A P T E R 9 W I N D O W S A N D D I A L O G S

Figure 9-15. Drop two more buttons onto the MDI container form.

Double-click the Cascade button and add a line of code to call LayoutMdi():

Private Sub cascadeButton_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles cascadeButton.Click

Me.LayoutMdi(MdiLayout.Cascade)

End Sub

Now go back to the editor and double-click the Tile button to drop into its click event handler and add a similar line of code:

Private Sub tileButton_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles tileButton.Click

Me.LayoutMdi(MdiLayout.TileHorizontal)

End Sub

In both cases, all you need to do is call LayoutMdi() and pass in the appropriate value. Run the application now, create a few child windows, and try out the two new layout buttons (see Figure 9-16).

C H A P T E R 9 W I N D O W S A N D D I A L O G S

247

Figure 9-16. Arranging MDI child windows is as easy as calling this.LayoutMdi().

Dialogs

There is another kind of window that you haven’t explored yet, but you have used it in a number of examples: the dialog box. A dialog is a special window that pops up to provide information to, or get information from, the user. It’s special in that it’s modal: when a dialog is on-screen, you can’t interact with any other window in the application until the dialog has gone away. You’ve used a few of them in the examples, courtesy of the

MessageBox class.

Using a Simple Message Box

A MessageBox control is perhaps the simplest of all possible dialogs in a Windows application, and it’s provided for you through the .NET MessageBox class. As you’ve already seen a few times, all you need to do is type

MessageBox.Show("Your message goes here")

and a message box dialog will appear on-screen at runtime with whatever text you specify. There are actually quite a few overloads of the MessageBox.Show() method call that allow

248 C H A P T E R 9 W I N D O W S A N D D I A L O G S

you to specify not just the message to display, but also the caption of the dialog, what buttons to display inside it, and what icon to display next to the text. MessageBox.Show() will even return a value to you that you can check to see exactly which button the user clicked to make the dialog go away.

Probably the most common overload of the MessageBox.Show() method looks like this:

MessageBox.Show(<message>,<dialog caption>, <buttons>, <icon>)

The first two parameters are both strings. The first is the actual message that you want to show in the message box, and the second is the text to display in the title bar of the dialog.

The buttons parameter is another enumerated value, from MessageBoxButtons. It can be one of the following values:

MessageBoxButtons.AbortRetryIgnore

MessageBoxButtons.Ok

MessageBoxButtons.OkCancel

MessageBoxButtons.RetryCancel

MessageBoxButtons.YesNo

MessageBoxButtons.YesNoCancel

Obviously, these all refer to the number of buttons to display and the text within them. Similarly, the icon is specified as an enumerated value from MessageBoxIcon, and can be

one of MessageBoxIcon.Asterisk, MessageBoxIcon.Error, MessageBoxIcon.Exclamation,

MessageBoxIcon.Hand, MessageBoxIcon.Information, MessageBoxIcon.None,

MessageBoxIcon._Question, MessageBoxIcon.Stop, or MessageBoxIcon.Warning.

As if all those enumerated values aren’t enough to make your head spin, the Show() method will also return an enumerated value from DialogResult. The values here correspond to the name of the button that was pressed—for example, DialogResult.OK,

DialogResult.Retry, DialogResult.Cancel, and so on.

C H A P T E R 9 W I N D O W S A N D D I A L O G S

249

Try It Out: Using a Message Box Properly

Earlier in the chapter, I mentioned using a message box to cancel a form closing down. That’s exactly what you are going to do now.

Create a new WinForms project and drop into the code editor for the form’s FormClosing event handler. What I’d like you to do is put up a message box that asks the user whether she really wants to quit, and that displays Yes and No buttons. This is quite easy to do. Go ahead and add some code:

Private Sub Form1_FormClosing(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.FormClosingEventArgs) _

Handles MyBase.FormClosing

MessageBox.Show("Are you sure you want to close the form down?", _ "Really quit?", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question)

End Sub

The call here just displays the question, sets the title of the message box to “Really quit?” and specifies that you want Yes and No buttons, and a Question icon in the message box.

The next thing you need to do, though, is figure out what the user actually pressed. MessageBox.Show() returns a value of type DialogResult, so you’ll need a variable here of the same type to catch the return value:

Private Sub Form1_FormClosing(ByVal sender As System.Object,_

ByVal e As System.Windows.Forms.FormClosingEventArgs)_

Handles MyBase.FormClosing

Dim result As DialogResult result = MessageBox.Show(_

"Are you sure you want to close the form down?",_

"Really quit?", MessageBoxButtons.Yesno,_

End Sub