A Demonstration: Passing Information between Windows

 

If you were creating a series of similar programs, it would seem foolish to create each succeeding program (once the first program is written) from scratch. It s much simpler to clone the first program under a different name and make the changes to the cloned program after separating the clone from the original.

We will accomplish this cloning task in this chapter.

Cloning

For demonstration purposes the original program is one that was created for you and is located in Visual Studio 2005\ Projects\ DemosSourceCode\Thumbnail1. Its name is Thumbnail1. To perform the clone operation, you will create a second project named Thumbnail2 in the IDE. For each form in the original project, you create a new form in the second project. Each form (including the main window form) has its own FormX.cs source file and FormX.Designer.cs window template file.

Once the basic cloned project has been created by the programmer, the only tasks that remain are to transfer selected files from the original project to the new project (overwriting the files in the cloned project with files from the original project) and rename the namespace in the FormX.cs and FormX.Designer.cs files (since they contain the namespace of the old, original project inside their files).

The tasks that follow are required to clone the Thumbnail1 project into a Thumbnail2 configuration. Thumbnail2 can be placed anywhere under the Projects directory in Visual Studio 2005, so we have elected to place it in C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Cloned Projects so it can be immediately identified as a cloned project.

Task 1: Create New Project

Open the IDE and create a new project using the name Thumbnail2. Place the new project in C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Cloned Projects.

The Cloned Projects subdirectory does not exist at this time, but the IDE will create it for you. As soon as the project opens in the IDE you must manually create the same number of forms as the original project contains. In this case, Thumbnail1 only has one form (the main window), but if your original project had three forms you would do the following:

  1. After the new project (Thumbnail2) has been created and the IDE screen is presented, click on Project on the main menu. Click Add Windows Form , and the window in Figure 4-1 appears.

    image from book
    Figure 4-1: Add New Item window

  2. Since the Visual C# Project Items category and the Windows Form template have been preselected by the compiler, all you have to do is click on the Add button at the lower right of the window to create a new form (Form2.cs in this case).

  3. The new Form2.cs and Form2.Designer.cs are created immediately. If you need a Form3, then you must click on the Project menu item again and click on Add Windows Form again. The number of forms in the new project must match the number of forms in the old project!

  4. As soon as this is done, move to File and Save All to save all elements of the new project. Then exit the project.

Task 2: Copy and Paste Files

Now you need to copy files from the old project and overwrite their counterparts in the new project. To do this, follow these steps:

  1. Move to My Documents\Visual Studio 2005\Projects\Cloned Projects .

  2. Open the Thumbnail2 subdirectory and click on the Thumbnail2 subdirectory that appears within the first Thumbnail2 directory. It is here, in this second Thumbnail2 subdirectory, that all file transfers will occur.

  3. Next, move to My Documents \ Visual Studio 2005 \ Projects \ DemosSourceCode . Project Thumbnail1 is located there.

  4. Click on Thumbnail1 , which displays a second Thumbnail1 subdirectory (and the Thumbnail1.sln file also). Click on the Thumbnail1 subdirectory. You now have two subdirectories open, so we suggest that you decrease the width of each and place them side by side for a series of file transfers. We always place the new project on the left side and the old project on the right side. At this time your screen should look like Figure 4-2.

    image from book
    Figure 4-2: Opening two subdirectories

    If, for example, your original project had three windows (three forms), then the list shown above would include four more files (two for form2 and two for form3).

  5. Copy each FormX.cs file and each FormX.Designer.cs file from the right list and paste them onto the left list. In each case, the operating system will ask you if you wish to overwrite the old file; answer Yes.

You are almost finished with the cloning process. You must now make a minor change inside each FormX.cs and FormX.Designer.cs file that has been transferred from Thumbnail1 to Thumbnail2, using WordPad.

Task 3: Rename, Compile, and Run Clone of Original Project

Bring each of the Thumbnail2 FormX.cs and FormX.Designer.cs files into WordPad and change the namespace at the top of each file from Thumbnail1 to Thumbnail2 . When these changes are complete, the cloning process is complete!

To verify that all is well, re-enter the compiler and bring up the Thumbnail2 project in the IDE. Compile it and run it. If you are an astute observer, you will note that the name Thumbnail1 remains in the title (at the top of the window), and you must click on the window template of Form1 to change the title from Thumbnail1 to Thumbnail2 within the Properties window.

Be aware that sometimes there are glitches in these file overwrites. The first time you attempt to compile the new cloned project you may see an error listing at the bottom of the IDE screen showing some sort of syntax error in either the FormX.cs or FormX.Designer.cs files. Double-click on that syntax error item to see the error. For some reason, it is fairly common for one or more of the semicolons in these files to be replaced with single quotes. You must fix the errors to proceed.

1.  

Why did we go through this process of creating a second project (Thumbnail2) with the same number of forms to create this clone? Couldn t we just copy all of project Thumbnail1, paste it in the Cloned Projects folder, and be done with it?

image from book

2.  

What if I add a form to a project and then find out that I don t need it. Can I delete the window?

image from book

Answers

1.  

The reason this simplified cloning process won t work is this: Thumbnail1 contains file paths in the supporting files that are okay for Thumbnail1 but are wrong for Thumbnail2. So the reason we create a copy of the project before transferring files is to set the proper file paths in the new (cloned) project.

IMPORTANT  

This cloning business also works when a project refuses to open properly in the IDE. If a project will not open, then exit the IDE and move to the Projects folder in Visual Studio 2005. Rename this folder as an old folder. For example, rename DragonSlayer to DragonSlayer(old). Then return to the IDE and open a new project named DragonSlayer. Be sure to create the same number of forms (windows) in this new project as there are in the old project. Save All and exit the new project. Now set up two screens. The one on the right should list all the files in DragonSlayer(old), and the one on the left should list all the files in DragonSlayer.

Copy and paste the same subdirectories and files that we listed above. In this case you will not have to open each FormX.cs and FormX.Designer.cs file to change its namespace because the original files already have the right namespace.

Enter the IDE, open project DragonSlayer, and immediately compile it and run. If all goes well, then the problem with the old project not opening in the IDE is solved . Once this is done, destroy folder DragonSlayer(old).

2.  

No, there is no provision to delete a form once it has been created in a project. But leaving the unused form in the project has no adverse affect on 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