Creating Multi-Window Applications

So far we've limited the Windows application to a single window, but there's no reason to limit yourself; you can use as many windows as you want in a Windows application. In this next example, ch07_06, which you can see at work in Figure 7.22, we'll take a look at multi-window applications. This example illustrates how to support the Multiple Document Interface (MDI), multiple and owned windows, and custom dialog boxes. In particular, this example uses an MDI parent window to enclose MDI child windows , as you see in Figure 7.22 (each MDI child window displays a rich text box to let the users enter and edit text).

Figure 7.22. Running the ch07_06 example.



Example ch07_06 is also designed to show how to work with menus in Windows forms. It uses a menu system to enable users to select various options, as you see in Figure 7.22. Creating a menu system in a form is easy in the IDE; you simply add a new MainMenu control (which appears in the component tray) from the toolbox to the form. When you do, boxes with the text "Type Here" appear in the upper-left corner of the form, as you see in Figure 7.23. This is where the menu will appear at runtime.

Figure 7.23. Creating a menu system.


To create menus and menu itemsand even submenusall you have to do is to enter text into the "Type Here" boxes. To add code to a menu item, you just double-click that item, opening its Click event handler in a code designer. For example, you can add this code to menuItem5 , which is the Exit item you see in Figure 7.23, to end the application:

 private void menuItem5_Click(object sender, System.EventArgs e) {  Application.Exit();  } 

MDI Applications

The ch07_06 example displays an MDI parent window, and each time you select its File, New MDI Window menu item, a new MDI child window will appear inside the MDI parent, as you see in Figure 7.22. To convert the standard form that is created when you create a Windows application into an MDI parent window, you select that form and set its IsMdiContainer property to true in the properties window.

We'll also need some MDI children, so select the IDE's Project, Add Windows Form menu item to open the Add New Item dialog box you see in Figure 7.24. You can add all kinds of items to your application this way; in this case, select the Windows Form icon and click Open , creating a new form class, Form2 , which is stored in Form2.cs.

Figure 7.24. Adding a new form to a Windows application.


To display a rich text box in each MDI child, double-click the Form2.cs entry in the IDE's Solution Explorer (the Solution Explorer is the top-right window in the IDE, as labeled in Chapter 1, Figure 1.2). The Solution Explorer opens Form2 in a form designer (you can click the two icons in the Solution Explorer under the word "Solution" to switch between a form's code designer and form designer). Add a rich text box from the toolbox to Form2 . To make the rich text box cover the entire client area of this new form, you dock it to the form. To do that, select the rich text box's Dock property in the properties window, and click the middle button in the button group that appears, as you see in Figure 7.25. You can dock a control to any edge of a form; in this case, we're docking the rich text to fill the form's entire client area.

Figure 7.25. Docking a rich text box.


Now the Form2 class is ready to be used. When the user clicks the File, New MDI Window menu item in the MDI parent window, we create a new Form2 object, named form , make it an MDI child of the MDI parent by setting the form object's MdiParent property to the MDI parent, and show the new MDI child form by calling its Show method:

 private void menuItem2_Click(object sender, System.EventArgs e) {  Form2 form = new Form2();   form.MdiParent = this;   form.Show();  } 

This code creates a new MDI child and adds it to the client area of the MDI parent, as you see in Figure 7.22. Every time you select the File, New MDI Window menu item in this example, a new MDI child window, covered with a rich text box, will appear in the MDI parent.


MDI children do not need to be all of the same class as they are in this example. MDI parents can display many kinds of MDI children at the same time. You can create your own form classes and make them MDI children of an MDI parent. All you have to do is to set their MdiParent properties to the MDI parent window.

Multiple Windows

You can also support multiple independent windows, not just MDI child windows, in a Windows application. For instance, when you select the File, New Window menu item in the ch07_06 example, a new window appears (see Figure 7.26). To show how communication between windows works, the MDI parent window has sent this new window the text "Multiple Windows!" , which the new window displays in a text box.

Figure 7.26. Displaying a new window.


To make this work, you add a new Windows form using the Project, Add Windows Form menu item as before to create the Form3 class. Add a Close button to Form3 to close the window:

 private void button1_Click(object sender, System.EventArgs e) {  this.Close();  } 

Give the new form a multiline text box in which to display text sent to it from the parent MDI form. How does the MDI parent communicate with this new form and send text to the text box? Say, for example, that you create a new object of the Form3 class: Form3 form = new Form3(); . In the code for the MDI parent, you can then refer to the new form as form , and show it with a call to the form.Show method. So how can you access the text in the text box in a Form3 object? Could you refer to it as form.textBox1.Text ? No, because the text box in Form3 is private by default. You can change the text box's declaration to public , in which case you can refer directly to form.textBox1.Text in the code in the MDI parent, but that's not good programming practice.


The Form.Close method closes a window, but doesn't dispose of it, so even after a window has been closed, you can still access its properties. To actually dispose of a Form object, you call its Dispose method.

A better idea is to add a new public property to Form3 , giving you control over which values are accepted or returned. In this example, we'll call that property DisplayText , so add this code to the Form3 class now:

 public string DisplayText {     set     {  textBox1.Text = "The opening window sent this text: " + value;  } } 

Now when you assign the DisplayText property some text, the text box in a Form3 object will display that text. Here's how we might create a new Form3 object, send it some text, and display it:

 private void menuItem3_Click(object sender, System.EventArgs e) {  Form3 form = new Form3();   form.DisplayText = "Multiple windows!";   form.Show();  } 

This gives you the results you see in Figure 7.26, where we've been able to communicate between windows using a custom property. That's how windows in the same application usually communicate with each otherusing custom properties and methods .

Here's another multi-window topic. You can also make one form "own" another form. An owned form will close when you close its owner, and will be minimized and restored automatically when you minimize and restore its owner. To allow the MDI parent to own the Form3 object form , you just pass form to the MDI parent's AddOwnedForm method:

 private void menuItem3_Click(object sender, System.EventArgs e) {     Form3 form = new Form3();  this.AddOwnedForm(form);  form.DisplayText = "Multiple windows!";     form.Show(); } 

You can determine which forms a form owns by checking that form's OwnedForms property. That property returns an Form[] array or null if there are no owned forms.

Dialog Boxes

You can retrieve text input from the user with dialog boxes. The ch07_06 sample application is an example; when you select the File, Show Dialog menu item in the MDI parent, a new dialog box appears, asking you to enter your comments as you see in Figure 7.27.

Figure 7.27. Displaying a dialog box.


When you type some comments into the dialog box and click OK, those comments are retrieved by the MDI parent form and displayed in a message box, as you see in Figure 7.28.

Figure 7.28. Reading text from a dialog box.


To make this work, we've added a new form, Form4 , to our example application. We'll customize this new form to act as a dialog box so when the user selects the File, Show Dialog menu item in the MDI parent, you can use the ShowDialog method to display this form. That method will return a result from the DialogResult enumeration. If the user clicks the OK button in the dialog box, we'll display their comments, retrieving those comments from the Data property, which we'll add to the dialog box:

 private void menuItem4_Click(object sender, System.EventArgs e) {  Form4 dialog = new Form4();   if(dialog.ShowDialog() == DialogResult.OK)   {   MessageBox.Show("Your comments were: " + dialog.Data);   }  } 

The next step is to set up the dialog box, Form4 . To make this form as much like a dialog box as possible, you set its FormBorderStyle property to FormBorderStyle.FixedDialog , giving it a fixed (non-resizable) border. Although windows are usually represented by buttons in the Windows task bar, dialog boxes are not, so you set this form's ShowInTaskBar property to false . In addition, set the form's ControlBox property to false to remove the maximize, minimize, and close buttons in the title bar. As with any other form, you can set its title bar text with the Text property.


Dialog boxes can be modal , which means the user must dismiss them before working with the rest of the application, or non-modal, which means they can stay open while the user works with the other windows in the application. Using the ShowDialog method makes the dialog box modal; using the standard Form method Show (as you use with any other window you want to open from your code) makes the dialog box non-modal.

You can also add the controls you see in the dialog box in Figure 7.27a multiline text box, as well as OK and Cancel buttons, button1 and button2 . To make button1 into a true OK button, set the form's AcceptButton property to button1 ; similarly, to make button2 into a true Cancel button, set the form's CancelButton property to button2 . Now, for example, if you use the ShowDialog method to display the dialog box and the user clicks the Cancel button, the ShowDialog method will return DialogResult.Cancel .

You can also set the dialog box's result value by assigning a value to the DialogResult keyword. For example, to return a value of DialogResult.OK when the user clicks the OK button, you can use this code:

 private void button1_Click(object sender, System.EventArgs e) {  DialogResult = DialogResult.OK;  } 

The calling code is set up to retrieve the text in the text box using a property named Data , so you add that property to the code for the Form4 class:

 public string Data {  get   {   return textBox1.Text;   }  } 

That's all the code we need. Now when the dialog box is displayed using the ShowDialog method, it'll return DialogResult.OK if the user clicked the OK button and DialogResult.Cancel if the user clicked the Cancel button. If the user clicked the OK button, you can read the text they've entered with the dialog box's Data property. That finishes the dialog box example.

The examples in this chapter are meant to give you a solid foundation in Windows programming. We haven't covered all the details here, of course. There are other types of windows you can work with, such as those targeted to PDAs and other smart devices, and even non-rectangular windows. And there are other controls as wellwe haven't covered list views, toolbars , status bars, and some others. The objective here is to get a kick-start in the topic, and now we're up to speed. We're ready to press on to Web applications in the next chapter.

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: