Print | Contents | Close |

Working with IDE, Code-editing, and Forms Enhancements

Learning objective

After completing this topic, you should be able to import IDE settings, add a code snippet, and perform some new Windows Forms creation tasks in Visual Studio 2005, given a scenario.

Exercise overview

In this exercise, you're required to manage Visual Studio 2005 Integrated Development Environment (IDE) settings, use Windows Forms controls, insert a code snippet in code, and use the BackgroundWorker class.

This involves the following tasks:

Task 1: Importing IDE settings

Suppose you want to restore an exported version of Visual Studio 2005 IDE settings for a new application.

Step 1 of 2

To import IDE settings, you first need to launch the Import and Export Settings Wizard and specify what you want to use it to do.

How do you launch the wizard and specify that you want to import settings?

Options:

  1. Select Tools - Import and Export Settings and select the Import selected environment settings radio button
  2. Select View - Import and Export Settings and select the Import selected environment settings radio button
  3. Select Tools - Import and Export Settings and select the Reset all settings radio button

Result

To launch the Import and Export Settings Wizard and specify that you want to import settings, you select Tools - Import and Export Settings and then select the Import selected environment settings radio button.

Alternatively, to launch the wizard, you press Alt+T, I.

Step 2 of 2

You've specified that you want to import IDE settings, and now need to save your existing IDE settings and complete the import process.

How do you complete the steps to import IDE settings from the file named Exported-2006-04-06.vssettings?

Options:

  1. Click Next, accept the default selection of the Yes, save my current settings radio button, click Next, select Exported-2006-04-06.vssettings, and click Finish
  2. Click Next, accept the default selection of the Yes, save my current settings radio button, and click Finish
  3. Click Next, accept the default selection of the Yes, save my current settings radio button, click Next button, select Exported-2006-04-06.vssettings, and click Close

Result

To import the IDE settings from the Exported-2006-04-06.vssettings file, you click Next, accept the default selection of the Yes, save my current settings radio button, click Next, select Exported-2006-04-06.vssettings, and click Finish.

Task 2: Adding Windows Forms controls

Once you've imported required IDE settings, you need to begin designing a form.

Step 1 of 3

You decide to add a MenuStrip control to the form using the Toolbox.

How do you open the Toolbox and add the control to the form?

Options:

  1. Select View - Toolbox and double-click MenuStrip
  2. Select Tools - Toolbox and click MenuStrip
  3. Select View - Toolbox and click MenuStrip

Result

To open the Toolbox and add a MenuStrip control to the form, you select View - Toolbox and double-click MenuStrip.

Step 2 of 3

The form requires standard menu items.

How do you add standard items to the MenuStrip control?

Options:

  1. Click the arrow in the top right corner of the control and click Insert Standard Items
  2. Click the arrow in the top right corner of the control and click Edit Items
  3. Double-click the MenuStrip control and click Insert Standard Items

Result

To add the standard items to the control, you click the arrow in the top right corner of the control and click Insert Standard Items.

Step 3 of 3

You decide to add a ToolStripContainer control to the form, and to disable its left and right panels.

How do you do this?

Options:

  1. Double-click ToolStripContainer in the Toolbox, and deselect the Left and Right checkboxes
  2. Click ToolStripContainer in the Toolbox, click the arrow at the top right corner of the control, and deselect the Left and Right checkboxes
  3. Double-click ToolStrip in the Toolbox and deselect the Left and Right checkboxes

Result

To add the ToolStripContainer control and disable its left and right panels, you double-click ToolStripContainer in the Toolbox, and deselect the Left and Right checkboxes.

Task 3: Adding a code snippet

Suppose you're creating a loan payment calculator form. You've placed a button labeled "Calculate" on the form. Now you need to add the code snippet that calculates the payment to the ButtonClick method.

Step 1 of 2

Suppose you want to use the code snippet named "if", which is located in the Visual C# category of code snippets. To do this, you first need to check the code shortcut you must use to insert the snippet.

How do you locate and select the required code snippet to determine its code shortcut?

Options:

  1. Select Tools - Code Snippets Manager, expand the Visual C# node, and select if
  2. Select Tools - Add-in Manager, expand the Visual C# node, and select if
  3. Select Tools - Code Snippets, expand the Visual C# node, and select if

Result

To locate and select the required code snippet, you select Tools - Code Snippets Manager, expand the Visual C# node, and select if.

Step 2 of 2

You've determined that the shortcut for the required code snippet is if. You now need to add the code snippet to your code.

How do you add the code snippet to private void Button1_Click?

Options:

  1. Type if in the code inside private void Button1_Click and press the Tab key
  2. Type if.Button1_Click and press the Enter key
  3. Type if in the code inside private void Button1_Click and press the Enter key

Result

To add the code snippet, you type if in the code inside private void Button1_Click and press the Tab key.

Task 4: Coding BackgroundWorker

You now need to use the BackgroundWorker class to perform multithreading in your application. You decide to do all the coding without using IntelliSense menus.

Step 1 of 3

Suppose you want to use the BackgroundWorker class to execute the process of iterating through a dictionary.

Complete the code to initialize the object worker as a new instance of the BackgroundWorker class.



using System.ComponentModel;
public class Form1
{
private BackgroundWorker worker;
private void Form1_Load(object sender, System.EventArgs e)
{
worker = MISSING CODE;
worker.WorkerReportsProgress = true;
}
}

Result

The complete code to initialize the object worker as a new instance of the BackgroundWorker class is

worker = new BackgroundWorker();

Step 2 of 3

Next you need to ensure that the BackgroundWorker class produces progress reports for the background process.

Complete the code to do this.



using System.ComponentModel;
public class Form1
{
private BackgroundWorker worker;
private void Form1_Load(object sender, System.EventArgs e)
{
worker = new BackgroundWorker();
worker.MISSING CODE;
worker.DoWork += new DoWorkEventHandler(OnWork);
worker.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged);
}
}

Result

The complete code to produce progress reports is

WorkerReportsProgress = True;

Step 3 of 3

Last, you need to assign the process to the BackgroundWorker class.

Complete the code to perform the RunWorkerAsync method on the variable worker.


  
private delegate void ChangeProgressBarHandler(int percentage);
private void ChangeProgressBar(int percentage)
{
ProgressBar1.Value = percentage;
}
private void Button1_Click(object sender, System.EventArgs e)
{
MISSING CODE;
}

Result

The complete code to perform the RunWorkerAsync method on the variable worker is

worker.RunWorkerAsync();

IDE settings have now been restored, Windows Forms controls have been added to a form, and a code snippet has been added to a method. The BackgroundWorker class has also been used in code.