Project Cleanup

 

Creating Form2 and Form3

  1. On the main IDE window, click on the main menu item Project , then Add Windows Form . The Add New Item window appears and the new item, Form2.cs, appears in the Name text box. Click on the Open button at the bottom of the window to create Form2.

  2. Repeat this procedure to create Form3.

Figures 5-2 and 5-3 show what Form2 and Form3 should look like eventually (after you have added several controls to each one):

image from book
Figure 5-2: Child window #1 (Form2)
image from book
Figure 5-3: Child window #2 (Form3)
  1. Move to the Form2 window template and set its size to (400, 200) . Sets its title bar text to Grandma Moses House . Set its Start Position to Center Screen .

  2. Create and place label1 (Message From Home:) at location (16, 16) . The font size is 10 and the message is underlined . The text is Message From Home: .

  3. Create and place label2 at location (16, 40) . The font size is 10 . There is no new text.

  4. Create and place label3 at location (16, 72) . The font size is 10 and the message is underlined. The text is Send Message to Home: .

  5. Create and place textBox1 at location ( 24, 94) with size (340, 23) . The font size is 10 . No new text is required here.

  6. Create and place button1 at location (137, 128) , with size (125, 23) . The font size is 8.25 , and the text is Return To Home .

This completes all the labels/controls on Form2.

It is time to create the event handlers for Form2. The only control that requires a handler is button1, the Return To Home button. When the user clicks on this button we will place the text in textBox1 into a persistent location in Form1 (the main window).

However, Form2 requires that items be entered on the form before any control is actuated (as soon as the window is created), so we must create the general event handler that is a part of the entire form window. Highlight the window template, move to the Properties window, and click on the Events icon. Under Behavior you will find a handler named Load. Double-click on Load to create an event handler named Form2_Load. This completes all formation of event handlers.

Note  

The easiest way to initialize data in a newly formed window is to place the event FormX_Load into its source code since this function is executed before the window opens on one s PC. The Load event is located in the list of events in every window template.

Each of the two event handlers that have been created within Form2 require code:

  1. In Form2_Load place this code:

     // Place the main window's message into 'label2'. label2.Text = frm1Parent.strGrandmaMoses; 
  2. In button1_Click place this code:

     // Place the return message into persistent storage in Form1: frm1Parent.label6.Text = textBox1.Text; Close(); 

This completes all code entries into Form2.

One last item remains in Form2: informing Form2 that it should expect data from Form1 (and vice versa). This is accomplished at the top of the class declaration. You will add six lines of code and modify one:

 partial class Form2 : Form                  (no change) {                                           (no change)   // This is a programmer-added item:       (add this line) ******   public Form1 frm1Parent;                  (add this line) ******   //                                        (add this line) ******   // This next line is programmer-amended:  (add this line) ******   public Form2(Form1 frm1)             (modify this line with an argument Form1 frm1) ******   {                                         (no change)     // This next line is programmer-added:  (add this line) ******     frm1Parent = frm1;                      (add this line) ******     InitializeComponent();                  (no change) 

This completes the rework of Form2. At the end of this section we will discuss how the communication system between the main window and child windows works.

Repeat the process for Form3 (three labels, one text box, and one button), keeping in mind that Form3 is Grandma Juliette s House, not Grandma Moses House.

 public class Form3 : Form                   (no change) {                                           (no change)   // This is a programmer-added item:       (add this line) ******   public Form1 frm1Parent;                  (add this line) ******   //                                        (add this line) ******   // This next line is programmer-amended:  (add this line) ******   public Form3(Form1 frm1)             (modify this line with an argument Form1 frm1) ******   {                                         (no change)     // This next line is programmer-added:  (add this line) ******     frm1Parent = frm1;                      (add this line) ******     InitializeComponent();                  (no change) 

The event handlers are the same, but there are minor changes in the code you must enter in the handlers:

1. In the Form3_Load event handler, place this code:

 // Place the main window's message into 'label2'. label2.Text = frm1Parent.strGrandmaJuliette; 

2. In the button1_Click handler, place this code:

 // Place the return message into persistent storage in Form1: frm1Parent.label7.Text = textBox1.Text; Close(); 

This completes all code entries into Form3.

Build and run this project. You should be able to write messages to the grandmas houses before you depart home (the main window) for their houses. (Don t forget to send the messages by clicking on the Send button after you type text in the text boxes.) Once LRRH is at the grandmothers houses , you should be able to send messages back home.

If a problem persists, the complete, working project may be found in Projects\DemosSourceCode\OverTheRiverAndThroughTheWoods.

 


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