One-Way Communication: Using Arguments

 

Default Event Handlers vs. Non-Default Event Handlers

Every control shown in the Toolbox has a default event handler. For example, the default for a button is buttonX_Click (the procedure that is activated when the user clicks on the button). For the text box, the default is textBoxX_TextChanged, the procedure used to check the characters being entered into the text box by the user. To invoke the default event handler for any control, the programmer highlights the control on the window template and double-clicks it. The IDE places the procedure in the FormX.cs source code file.

If a non-default event handler is required, the programmer must highlight the control in the template, move to the Properties window, click on the Events icon, and scroll up or down to find the proper event. Once the proper event is located in the left column, the programmer double-clicks that item to create the procedure in the FormX.cs source code file.

Creating the Necessary Event Handlers

In this project we will create default events handlers only. As each event handler is created, we will immediately enter a comment within the source code in the FormX.cs file to indicate which handler is which. With so many handlers it is easy to get them mixed up.

Note  

There are two ways to place comments in your Visual Studio C# code. Method one is used here: Enter two forward slashes; anything entered to the right of these slashes on the same line is ignored by the compiler. The compiler overlooks entries only on this line. The second method allows you to comment out multiple lines of code. At the start point you type /* and at the end point you type */. This is nothing new to C programmers; we have done this for decades.

The advantage of the /* -- */ sequence is that it can surround any // comments that you placed in the text earlier, and it does not damage any earlier comments you placed in the text.

None of the labels need event handlers. The two text boxes require event handlers only if we decide to do some text checking within each text box (similar to what was done in project Thumbnail1 or Thumbnail2). Since we are not going to check the text that the user types into the text boxes for correctness, the text boxes need no event handlers. However, the buttons definitely need default event handlers. We will create those handlers now.

1.  

What if you create a handler and discover later that you don t need it. How do you get rid of it?

2.  

Once I am in the event handler list (the Events window), how do I get out of it?

Answers

1.  

Highlight the control with the unwanted handler, move to the Properties window, click on the Events icon at the top of the window, find the unwanted handler in the list, and right-click on that entry. A pop-up menu appears. Choose the Reset subitem, and the entire handler goes away from the project s hidden connections. If the programmer has not typed any text into the procedure that the event handler created in the FormX.cs source code file, that procedure will be eliminated. However, if the programmer has entered any text into the procedure, the automatic system cannot erase the procedure and the programmer must do so manually.

2.  

Click on the Properties icon at the top of the window (just to the left of the bolt of lightning icon) to return to the Properties window.

  1. Highlight button1 on the template, double-click on it, and create a handler named button1_Click . Place the comment // Send Grandma Moses message. inside the source code (between the two curly brackets that define the procedure).

    After each of the following events, click on the F7 key to return the window template to the screen.

  2. Highlight button2 , double-click on it, and create a default handler named button2_Click . Place the comment // Send Grandma Juliette message. inside the source code (between the curly brackets that define the procedure).

  3. Highlight button3 , double-click on it, and create a default handler named button3_Click . Place the comment // Going to Grandma Moses house. inside the source code.

  4. Highlight button4 , double-click on it, and create a default handler named button4_Click . Place the comment // Going to Grandma Juliette s house. inside the source code.

  5. Highlight button5 , double-click on it, and create a default handler named button5_Click . Place the comment // Quit this project. inside the source code.

All that remains in the main window (Form1) is the placement of selected code in some of the event handlers.

  1. Move to the event handler for button 1, button1_Click . When the user clicks on this button to send a message to Grandma Moses, all that this handler must do is transfer the message from the temporary location textBox1.Text to a permanent, persistent, global variable that is declared at the top of Form1 (the main window): strGrandmaMoses. If you do not transfer the data in textBox1.Text to strGrandmaMoses (which is a public, global variable in Form1), the data cannot be transferred to Form2 later.

  2. Add these two lines of code in the button1_Click event handler:

     // Place this message into a public string, for transport to Form2. strGrandmaMoses = textBox1.Text; 
  3. Move to the event handler button2_Click and add these lines:

     // Place this message into a public string, for transport to Form3. strGrandmaJuliette = textBox2.Text; 
  4. Move to the event handler button3_Click and enter this code:

     // Create Form 2 (Grandma Moses' house). // 'this' in the next line refers to the main window's 'handle'. Form2 frm2 = new Form2(this); frm2.ShowDialog(); frm2.Close(); 
  5. Move to the event handler button4_Click and enter this code:

     // Create Form 3 (Grandma Juliette's house). // 'this' in the next line refers to the main window's 'handle'. Form3 frm3 = new Form3(this); frm3.ShowDialog(); frm3.Close(); 
  6. Move to the event handler button5_Click and enter:

     // Quit this project. Close(); 
  7. Move to the top of the Form1.cs file, to where the classes are declared, and add two comment lines and two declarations:

     partial class Form1 : Form                      (this line is already present) {                                               (this line is already present)   // These are two programmer-added items:      (add this line)   public string strGrandmaMoses;                (add this line)   public string strGrandmaJuliette;             (add this line)   //                                            (add this line) public Form1();                                 (this line is already present) 
    IMPORTANT  

    The statements above must appear precisely as shown (between the opening curly bracket and the Form1 line).

  8. Labels 6 and 7 have already been converted from private objects to public objects so the child windows can send information back to them.

This completes all code entries into Form1.

The next task is to create two new child windows (Form2 and Form3) in the project.

 


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