Two-Way Communication

 

A Demonstration: Passing Information between Windows

We will now proceed with a demonstration of how to pass information between a main window and its surrounding child windows, using approved object-oriented procedures. The demonstration includes:

  • How to create a typical Visual Studio C# project (main window plus two child windows).

  • How the main window talks to the child windows, and how the child windows talk back to the main window (two-way communication).

  • How to implement one-way communication (main window to child) using arguments. This method has been in vogue for 40 years in the computer industry.

The name of this project is OverTheRiverAndThroughTheWoods.

Do you recall the story of Little Red Riding Hood? She went to visit her grandmother in the woods, but her grandmother was replaced by a big bad wolf pretending to be her grandmother. In our story today Little Red Riding Hood (LRRH) decides to visit her two grandmothers, both of whom live a short distance away from her home in Bel-Air. Because LRRH s mother is concerned for her daughter s safety, she has constructed communications between home (the main window) and each of the grandmother s homes (first child window and second child window) so she and LRRH can send messages to each other.

Before LRRH departs home for Grandma #1 s house, she sits down at her computer and sends the lady a message announcing her departure . Then, when LRRH arrives at Grandma #1 s house she sends a message back home to her mother, announcing her arrival. This is an exercise in messaging.

  1. Open the Visual Studio C# IDE, and choose File on the main menu, followed by New and then Project . When the New Project window opens, name the project OverTheRiverAndThroughTheWoods and store the project at C:\Documents and Settings\Owner\ MyDocuments\Visual Studio 2005\Projects. Click on the OK button at the bottom-right edge of the window to create the new project. Highlight the window template so its properties will appear in the Properties window. Then scroll down to the Text entry and change the title bar from Form1 to Over the River and Through the Woods .

    IMPORTANT  

    You change the Text entry here, not the (Name) entry. The object s name always remains the same.

  2. Scroll down to Layout, then Size, and set the form s size to (550, 300) .

  3. Immediately below the Size entry, click on the Start Position entry and pick Center Screen .

  4. Click on the Toolbox tab at the left edge of the IDE screen to display the Toolbox. In Toolbox, click once on Label (not LinkLabel), then move the mouse cursor over the window template and click again to deposit the label on the template. This is label1. Since the label is highlighted on the template, its properties appear in the Properties window.

  5. Set the label s Location to (100, 8) . You cannot choose a size for a label in v2.0; that is done automatically. Set its Text as Over the River and Through the Woods . Set the label s Font Size to 14 and its Font Underline to true .

Note  

The window template, like the Windows screen, uses coordinates with a (0,0) point at the top left of the screen. The coordinates increase as you move down or right on the screen. The unit of measurement is the pixel . The most-coarse screen coordinates are 800 pixels wide by 600 pixels high. Any label or control that you place on a Windows screen takes its position coordinates from the top left of the screen. If, however, you place a group box onto the template, any control you place inside the group box takes its coordinates from the top left of the group box. You will notice the change in coordinate systems as we proceed through OverTheRiverAndThroughTheWoods.

This is going to be a busy project. You will place six more labels on the template, two text boxes, one group box, and five buttons . The finished main window template will look something like this:

image from book
Figure 5-1: OTRATTW window

Only in special cases does the order in which you place the labels/ controls on the template make a difference in the outcome. On this OTRATTW template we have a special case: The group box must be placed first, then the four labels (Moses:, label6, Juliette:, label7) must be placed on top of the group box.

  1. Create and place label2 (Send Note to Grandma Moses ->) at location (24, 52) . The font size is 10 with no underline, and the text is Send Note to Grandma Moses -> .

  2. Create and place label3 (Send Note to Grandma Juliette ->) at location (24, 86) . The font size is 10 with no underline, and the text is Send Note to Grandma Juliette -> .

  3. Create and place textBox1 at location (235, 50) with size (200, 23) . The font size is 10 with no underline. No new text is required here.

  4. Create and place textBox2 at location (235, 84) with size (200, 23) . The font size is 10 with no underline. No new text is required here.

  5. Create and place groupbox1 at location (28, 120) with size (488, 64) . Its text is Messages From Our Grandmother: . The font size is 10 with no underline.

These next four labels must be placed on groupbox1.

  1. Create and place label4 (Moses:) on top of groupbox1 with location (16, 20) . The text is Moses: and the font size is 10 with no underline.

  2. Create and place label5 (Juliette:) with location (16, 40) . The text is Juliette: and the font size is 10 with no underline.

  3. Create and place label6 at location (75, 20) . Font size is 10 with no underline.

  4. Create and place label7 at location (75, 40) . Font size is 10 with no underline.

    Note  

    We will erase the words label6 and label7 (fill them with blanks) now so the area inside groupbox1 will be empty except for the words Moses: and Juliette: . Highlight label6 and replace the label6 Text with blanks. Do the same thing for label7.

    Once you place blanks in a label, the template cannot show the programmer where the label is located, so we usually wait to blank out a label after all other work is complete on the form.

    IMPORTANT  

    All controls and forms are initially created by the IDE as private class objects. Since we will be writing to label6 and label7 from remote child windows, label6 and label7 must be converted from private to public classes. The programmer cannot accomplish this task manually. (Changing private to public in the Form1.Designer.cs file doesn t work, since the IDE changes them back to private immediately.)

    image from book
    Changing a Private Class to a Public Class

    To change label6 and label7 from private to public, proceed as follows :

    1. Get label6 into the Properties window. If label6 had any characters in it, it would be easy to click on that label in the window template and highlight it (so its properties appear in the Properties window). But in this case label6 has no characters in it, so it cannot be located on the template! What do you do? Click on any object to open the Properties window. At the top of the window the name of the object appears, and to the right of that name is a drop-down down arrow (directly below the black X that closes the window). Click on the down arrow to reveal the list of all objects.

    2. Click on label6 ; its properties will appear in the Properties window. Scroll down to Design , click on Modifiers , and choose Public (it was originally set at Private). Now the child windows can write to label6 (after the proper handle has been given by the main window to the child windows).

    3. Repeat this process for label7.

    image from book
     
  5. Create and place button1 at location (452, 50) , with size (60, 22) . The font size is 8.25 with no underline, and the text is Send .

  6. Create and place button2 at location (452, 84) , with size (60, 22) . The font size is 8.25 with no underline, and the text is Send .

  7. Create and place button3 at location (50, 195) , with size (200, 23) . The font size is 8.25 with no underline, and the text is Going to Grandma Moses House .

  8. Create and place button4 at location (300, 195) , with size (200, 23) . The font size is 8.25 with no underline, and the text is Going to Grandma Juliette s House .

  9. Create and place button5 at location (208, 226) , with size (125, 23) . The font size is 8.25 with no underline, and the text is Quit this Project .

This completes the placement of all the labels/controls on the main window template.

Next we will connect some of the controls to event handlers that the IDE will create for us automatically.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net