OK. The result should look like Figure 1.5.
NOTE
If you have previously edited a project, you can quickly reload it from the File menu's Recent Projects and Solutions submenu. You can also load a solution into the IDE by using File Explorer to double-click the solution's .sln file.
The rest of this lesson deals with the features available in Visual Studio, some of which are displayed in Figure 1.5. Before you launch into an inventory of useful features, however, open the Debug menu and select Start Debugging. Or if you're in a hurry, just press F5.
Your first program should look like Figure 1.6. Admittedly this first program isn't very fancy, but by the same token you didn't need to do much to build it. All you did was press Ctrl+Shift+N and then F5!
This first program may not seem terribly impressive, but there's a lot going on behind the scenes. C# has built a form with a bunch of useful features, including:
● A resizable border and a draggable title bar.
● Working minimize, maximize, and close buttons in the upper-right corner.
● A system menu in the upper-left corner that contains working Restore, Move, Size, Minimize, Maximize, and Close commands.
● An icon in the system taskbar that lets you minimize, restore, and close the program.
● The ability to use Alt+Tab and Flip3D (Win+Tab) to move between the application and others.
● Other standard window behaviors. For example, if you double-click the form's title bar it maximizes (or restores if it is already maximized), and if you press Alt+F4, the form closes.
Unless you're an absolute beginner to Windows, you probably take all of these features for granted, but providing them is actually a huge amount of work. Not too long ago you would have had to write around 100 lines of code to provide a subset of those features. Now Visual Studio automatically builds a form that handles most of the details for you.
You can still get in and change the way things work if you want to (for example, you can set a form's minimum and maximum allowed sizes), but usually you can ignore all of those issues and concentrate on your particular application instead of the Windows decorations.
A Suitable Executable
Whenever you run a program in the IDE, Visual Studio builds an executable program, normally in the project's bin\Debug subdirectory. You can run the executable by finding it in File Explorer and double-clicking it.
Unfortunately that doesn't mean the executable can run on any old computer! If you copy that file to another computer, it won't run unless the .NET Framework runtime libraries have been installed there. If that computer has Visual Studio installed, you're all set, but if it doesn't you'll need to install the redistributable yourself.
To install these libraries, go to Microsoft's download web page www.microsoft.com/downloads and search for “.NET Framework redistributable.” Pick the version that matches the one you're using (probably the most recent version if you just installed Visual Studio) and install it on the target computer.
Now you can copy C# executables onto the other computer and run them.
Copying Projects
Sometimes you may want to copy a project. For example, you might want to save the current version and then make a new one to try things out. Or you may want to give a copy of the project to a friend or your programming instructor so he or she can tell you why its New button makes the program exit.
You might look in Visual Studio's File menu and see the Copy As commands. Don't be tempted! Those commands copy single files, not the entire project. Later when you try to open one of those files, you'll discover that Visual Studio cannot find all of the other pieces that it needs and you'll be left with nothing usable.
To correctly copy a project, copy the entire solution or application folder and its directory hierarchy. Alternatively, you can compress the project directory and then copy the compressed file. Just be sure that whatever copying method you use brings along all of the project's files.
Note that you can delete the bin and obj subdirectories if you like to save space. Those directories contain files that Visual Studio creates when it loads and builds a program, and it will re-create them whenever it needs them later.
You can also delete the .vs directory, which contains user settings. Unfortunately that directory is hidden by default so it may be hard to find. To make File Explorer show you hidden files, open the Control Panel, click Appearance and Personalization, and select Folder Options. On the View tab, select Show Hidden Files and Folders, and then click OK. Now you can see the .vs directory to delete it.
NOTE
Compressing a project is very useful because it keeps all of its files together in a package. In particular, if you ever need to e-mail a project to someone (for example, if you e-mail me at [email protected] for help), you can remove the bin, obj, and .vs directories, compress the project folder, and e-mail the package as a single file.
If you're sending the project to your instructor as part of an assignment, rename the compressed file so it contains your name and the name of the assignment; for example, RodStephens6-1.zip.
Exploring the IDE
The Visual Studio IDE contains a huge number of menus, toolbars, windows, wizards, editors, and other components to help you build applications. Some of these, such as the Solution Explorer and the Properties window, you will use every time you work on a program. Others, such as the Breakpoints window and the Connect to Device dialog, are so specialized that it may be years before you need them.
Figure 1.7 shows the IDE with a simple project loaded and some of the IDE's most important pieces marked. The following list describes those pieces.
1. Menus– The menus provide all sorts of useful commands. Exactly which commands are available, which are enabled, and even which menus are visible depends on what kind of editor is open in the editing area (#4). Some particularly useful menus include File (opening old projects and creating new ones), View (finding windows), Project (adding new forms and other items to a project), Debug (build, run, and debug the project), and Format (arrange controls on a form).
2. Toolbars– The toolbars provide shortcuts for executing commands similar to those in the menus. Use the Tools menu's Customize command to determine which toolbars are visible.
3. Solution Explorer– The Solution Explorer lists the files in the project. One of the most important is Form1.cs, which defines the controls and code for the form named Form1. If you double-click a file in the Solution Explorer, the IDE opens it in the editing area.
4. Editing Area– The editing area displays files in appropriate editors. Most often you will use this area to design forms (place controls on them and set control properties) and write code for forms, but you can also use this area to edit other files such as text files, bitmaps, and icons.
5. Toolbox– The Toolbox contains