Вы находитесь на странице: 1из 47

SharpDevelop

http://community.sharpdevelop.net/blogs/mattward/articles/FeatureTourVisuallyDesigningForms.aspx
Feature Tour

SharpDevelop is an Integrated Development Environment (IDE) for .NET Framework applications. It supports
the development of applications written in C#, Visual Basic.NET, Boo and IronPython. It is open source and
written in C#. It provides all of the features required from a modern Windows IDE, such as code completion,
project templates, an integrated debugger and a forms designer. It has good compatibility with Visual Studio
Express and Visual Studio 2008 by using the same project and solution file format.

Here is a quick tour showing you the main features of SharpDevelop.

 Creating Windows Applications

 Visually Designing Forms

 Code Completion

 Code Generation

 Refactoring

 XML Comments

 Code Navigation

 Debugging

 Converting Code between Languages

 Unit Testing

 Code Coverage

 Editing XML

 Creating Boo Applications


Creating a Windows Application

With SharpDevelop you can create and develop many different types of project:

 ASP.NET Web Page Project

 ASP.NET Web Service

 Class Library

 Console Application

 Direct 3D Application

 WPF Application

 WPF Navigation Application

 Windows Application

 Windows Application for the Compact Framework

 Windows Service

 Windows User Control Library

 WiX Setup Project

The following sections will walk you through creating a Windows Application for the C# language.

Creating a Windows Application


To create a new Windows Application, from the File menu, select New and then select Solution....

This opens the New Project dialog box.


From this dialog you can choose from many different project templates that are grouped into different categories.
Expand the C# category, select the Windows Applications category, and then select Windows Application from
the list of available templates. Type in a name for the project and choose where you would like the project to be
created. Click the Create button to generate the project. The project will then be opened by SharpDevelop ready
for you to start developing.

Click the above image to see a larger version with added notes explaining the various parts of the user interface.

Building the Application


From the Build menu select either Build Solution or Build <Your Project Name>.

Build messages will be displayed in the Output window as the build progresses.

When the build is completed any errors will be listed in the Errors window.

Running the Application


From the Debug menu select Run or Run without debugger.
Your application will then be started and its main form will be displayed.

Adding a New Form


SharpDevelop provides many different types of file templates which you can add to your project:

 Configuration classes

 Class

 Form

 Interface

 Module

 MSBuild File

 Setup Dialog (WiX)

 Setup Document (WiX)

 Singleton Class

 Struct

 Unit Test

 User Control

 Web Control

 Web Form

 Web Service

 WPF Flow Document

 WPF Page

 WPF User Control


 WPF Window

 XML File

To add a new form to your Windows Application, open the Project Explorer, if it is not already open, by
selecting Projects from the View menu.

In the Projects Explorer select the name of your project, right click, select Add and then New Item....

This opens the New File dialog box.


Expand the C# category, select the Windows Applications category, and then select Form from the list of
available templates. Type in the desired name of the file and then click the Create button to add the new form to
your project.

In the next section you will see how to visually design your forms.
Visually Designing Forms

SharpDevelop has a forms designer that allows you to visually design forms and user controls written in C#,
VB.NET or Boo.

Opening the Forms Designer


To open the forms designer click the Design tab at the bottom of the form's source code window.
Adding Controls
Controls can be added to the form from the Toolbox window. From the View menu select Tools.

This opens up the Toolbox window. The controls you can add to the form exist in tabs inside the toolbox:
Tab Contents
Displays a list of non-visual components that can be added to your form (e.g.
Component FileSystemWatcher, NotifyIcon and Timer). These are added to the non-visual design area at the
bottom of the forms designer.
Custom Displays a list of Windows Forms Controls that are in the current project, any referenced project
Components or any referenced assembly.
Displays a list of data objects that you can add to your form (e.g. DataGridView, DataSet and
Data
OleDbConnection).
Displays a list of the standard Windows Forms Controls (e.g. Button, ListView, TreeView and
Windows Forms
Label).

Click a tab to expand it and see what controls it contains. To add a control to the form you can either drag it or
draw it onto the form. To drag, click the control with the left mouse button and whilst holding down the left
mouse button move the mouse to your form, release the mouse button to drop the control onto your form. To
draw it onto your form, select the control in the toolbox window by left clicking it with the mouse, no need to
hold the button down at this point, then click where you want the top left corner of the control to be located on
the form, then whilst holding down the left mouse button drag to where the bottom right corner of the control
should be located. Once the control is placed on the form you can visually resize it, change its location and
modify its properties. In the screenshot below a Button, TextBox and Timer have been added to the form, with
the Timer shown in the non-visual component area of the forms designer.
Setting Properties
A control has properties that can be set at design time, changing how the control looks and reacts (e.g. text
displayed on a button, font used to display the text, and the background image on a button). These properties are
displayed in the Properties window. From the View menu select Properties to open the Properties window.
The properties are then displayed for the currently selected control. You can view the properties of another
control on the form by selecting it in the designer or by selecting its name in the drop down list at the top of the
Properties window.

Events
Each Windows Form control has events that you can catch and respond to. To see the events a control exposes,
select the control in the designer and select the Events tab in the Properties window.
To generate an event handler, select the event in the Properties window, enter a name, or leave it blank to accept
the default name, and then double click the property or press the enter key. The forms designer will then create an
empty event handler method and switch to the form's source code ready for you to type in your code. The
screenshot below shows the code generated for the button's Click event.
Code Completion

SharpDevelop provides code completion as you type. This helps you quickly find an object's method, property or
event and then insert the code into the text editor.

Typing the dot character "." after an object's name will automatically bring up a list of methods, properties and
events that are available for that object.

Typing the first few characters of the method, property or event will select the matching item. Pressing the Tab
key will complete the code by inserting the currently selected item into the text editor.

Whilst you are typing in attributes you will see code completion.
Or when typing the space character after the new keyword when creating a new instance of a class.

Or when typing in a using statement.

Or writing code to connect an event handler.

Typing in the opening parenthesis of a method will show you its parameters and allow you to see any overloaded
methods it has. The up and down cursor keys can be used to view the different overloaded methods.

Typing in the open parenthesis of a catch statement will show you a list of exceptions.
Code Generation

If you have an interface that you want to implement it can be time consuming to write all the class methods just
to get your code to compile, especially if the interface has a large number of methods. SharpDevelop can save
you time by generating this code for you.

SharpDevelop can generate code for the following:

 Abstract base class methods

 Constructors

 Equals and GetHashCode methods

 Property getters and setters

 Interfaces

 Overridable methods

 Overridable properties

 ToString methods

Whilst editing the code in the Text Editor select Auto code generation from the Tools menu, or use the keyboard
shortcut Alt+Insert.

This opens the Code Generation dialog box.


Use the cursor keys to select Interface implementation, press the Tab key to move to the list of interfaces, press
the Space Bar to select the ICollection interface, then press the Enter key to generate the code.

Code Templates
SharpDevelop also has code templates that can be inserted into your code. To view the code templates, from the
Tools menu select Options..., in the options dialog box expand the Coding category and select Code Templates.
SharpDevelop ships with several templates. These templates can be customised or you can create new ones.

To use a template in your source code, move the cursor to where you want the code to be inserted, then type in
the template name, for example tryf, then press the space bar to insert the code.

Alternatively you can insert a code template using the code template dialog. Move the cursor to where you want
the code to be inserted, then press Ctrl+J to open the Code Template dialog box.
The code template can be selected directly and inserted or, just as with code completion, you can type in the first
few characters of the template name and press the Tab key or Enter key to insert the template.

Surround With
The Code Template dialog box can also be used to surround selected code with a template. For example, if you
wanted to put a try catch around some code, first select the code in the text editor.

Then press Ctrl+J to open the Code Template dialog box and then use the mouse or cursor keys to select the
template.

Pressing the Enter or Tab key will insert the selected template.
Refactoring

"Refactoring is the process of changing a software system in such a way that it does not alter the external
behavior of the code yet improves its internal structure."

Martin Fowler

SharpDevelop supports the following refactorings.

 Rename - you can rename a variable, class, method, field or property and all references to it are updated.

 Remove unused import statements - removes all the unused using or import statements from the current file.

 Extract method - creates a new method from the selected lines of code.

Given the code:

You decide that the Count property is badly named and you want to change it, but it is used in several other
classes. Right click the Count property and under its name in the menu select Rename.

This opens the Rename dialog box.


Type in a new name for the property and click the OK button. This renames the property and updates all the code
that uses this property.

To remove any unused import statements from the currently open file, select Remove unused import
statements from the Refactor menu.

Given the code:

You decide that the code inside the constructor should be extracted into its own method. Highlight the lines of
code you want to extract.
From the Refactor menu select Extract Method.

In the Extract Method dialog give the method a name and click the OK button.

Then a new method will be generated as shown below.


XML Comments

SharpDevelop has support for adding and previewing XML documentation comments in C# and VB.NET code.
It also ships with Microsoft's Sandcastle so you can generate your help file directly from the development
environment.

Adding XML Comments


In a C# source file, typing in /// before a method will start an XML comment and SharpDevelop will generate a
basic comment based on the method parameters and return value.

In VB.NET an XML comment starts with '''.

As you type SharpDevelop provides XML element completion inside the comment.

Previewing the XML Comment


With the cursor inside the comment, from the Tools menu select Quick XML doc, or alternatively use the
keyboard shortcut Ctrl+Q. This opens up a window showing you a preview of the XML comment.
Generating Documentation
By default the compiler does not generate XML comment documentation, to turn this on, select the name of the
project in the Project Explorer, right click and select Properties. The project options are then opened. Select the
Compiling tab and enable the XML comment generation by ticking the XML documentation check box.

XML comment documentation will then be generated from the code when the project is compiled. Once
compilation is complete you can use Sandcastle to generate help documentation. From the Project menu, select
Build documentation from XML comments.
This runs the Sandcastle application where you can generate various types of documentation, such as MSDN-
style HTML Help (.chm), Visual Studio .NET Help (HTML Help 2), and MSDN style web pages.
Code Navigation

SharpDevelop provides several features that help you quickly navigate to different parts of your code.

Quick Class Browser


At the top of the text editor is the Quick Class Browser.

On the left is a list of the types defined in the currently open source file. On the right is a list of all the members
(methods, properties, fields and events) of the selected type. Selecting a list item will cause the text editor to
jump to the corresponding code.

Go to Line
From the Search menu select Goto Line Number....

This opens the Go to Line dialog box.


You can type in a line number, class name or filename and then click the OK button to get the text editor to jump
to the corresponding code.

Go to Definition
In your code select a method call, an object or a variable, right click it, select the name of the object and then
Goto Definition, or alternatively use the keyboard shortcut Ctrl+Enter, and the text editor will jump to the
definition.

Class View
From the View menu select Classes.
The Class View window is then displayed.

The Class View shows you all the types defined in your project and allows you to quickly navigate to the code of
a particular type or type member. To jump to a class member simply double click it in the Class View.

If you have a project containing a lot of types you can search for a particular type using the Class View search
text box. As you type into the search text box the Class View updates to show only those types that match.

Find References
You can find all references to a particular type or type member by right clicking it in the text editor, selecting its
name and then selecting Find references.

This can also be done from the Class View.


The references found are then displayed in the Search Results window. This search is more comprehensive than
doing a string search since it will only find references to the type and not strings that happen to have the same
name.

From the Search Results window, double click a list item to jump to the corresponding code in the text editor.
Debugging

With SharpDevelop you can debug your code without leaving the development environment.

Setting Breakpoints
Position the cursor on the line where you want your breakpoint to be located, then click in the left hand margin of
the text editor to create the breakpoint. This puts a breakpoint icon in the margin and highlights the line in red.

Alternatively you can select Toggle Breakpoint from the Debug menu or use the keyboard shortcut F7.

To remove the breakpoint simply click the breakpoint icon or select Toggle Breakpoint from the Debug menu.

Running the Debugger


To run your program under the debugger select Run from the Debug menu.

Your program will then be started and SharpDevelop will change the window layout of the development
environment as it switches to the Debug Layout.

To stop the debugger either let your program exit or select Stop process from the Debug menu.

Debugging
When a breakpoint is hit your program will be paused and the next line to be executed is highlighted in yellow.

Whilst the program is paused you can execute the code line by line using the Debug menu items.
Step into will call the method on the current line and stop at the first line of code inside the method.

Step over will execute the entire method on the current line but it will not step into it.

Step out will finish executing the current method and returns to its parent method.

Continue debugging will continue the program execution so it is running just as it was before it was paused.

You may also want to skip over lines of code whilst you are debugging or even to re-execute the same lines of
code again. In the text editor select the line of code you want to execute next, right click and select Set current
statement.

The selected line will then be highlighted in yellow and will be the next line of code executed.

Whilst the program is paused you can view the current state of variables and objects in order to diagnose
problems. This is covered in the following sections.

Data Tips
If you move the mouse over a variable SharpDevelop will display a special tooltip called a data tip.
Data tips show more information than ordinary tooltips, allowing you to explore an object's members. To see its
members expand the data tip by clicking the plus icon .

Local Variables
The Local Variables window allows you to browse variables defined in the function currently being executed,
arguments passed into the current function, fields and properties of the class where the function is defined.

From the View menu select Debug and then Local variables.
The Local Variables window is then displayed.

Callstack
The Callstack window shows you the method calls currently on the stack.

From the View menu select Debug and then Callstack.

The Callstack window will then be displayed.


Code Conversion

A lot of code examples are only available in one language, usually C#, and often you need the code in another
language. SharpDevelop can convert the code between the three languages C#, VB.NET and Boo. It can convert
individual files or an entire project to another language. Currently converting from Boo to the other languages is
not supported.

To convert an open project, from the Project menu select Convert and choose the desired language.

To convert a single file, from the Tools menu select Convert code to and choose the desired language.
Unit Testing

SharpDevelop integrates with NUnit allowing you to run and debug your unit tests inside the development
environment.

First you will need a project to hold your tests. From the File menu, select New and then select Solution... to
open up the New Project dialog box.

Expand the C# category and select Class Library from the list of available templates. Choose the project's name
and location and then click the Create button.

Now create a simple test class. Select the name of the project in the Project Explorer, right click, select Add and
then select New Item.... Select the Unit Test template from the C# category, give it a name and then select the
Create button. Adding this template to the project will automatically add the required reference to
NUnit.Framework. The code shown below is an example of a very basic test class which you can copy into the
file just created.

using NUnit.Framework;
using System;

namespace Simple.Tests
{
[TestFixture]
public class MyTest
{
[Test]
public void FailingTest()
{
Assert.Fail("This test fails.");
}
}
}

From the View menu select Tools and then Unit Tests.
This opens up the Unit Tests window.

To run the test from the Unit Tests window click the Run tests toolbar button , or right click a test, and select
Run tests.

After a test run, unit tests that pass are shown in green, those that fail in red and those that have not been run in
yellow.
As the tests are run messages are displayed in the Output window and after the tests have completed failing tests
are added to the Errors window.

Tests can be run from the source code editor by right clicking the test method or test class and selecting one of
the Unit Testing menu options.

The menu options allow you to run the test and show the result in the Unit Tests window, or run the test with the
debugger so you can step through the code, or run the test and check for code coverage. The last two menu items
are also available when running tests from the Unit Tests window.
Code Coverage

How much of your code is being actually being tested? SharpDevelop can use PartCover (an open source code
profiler) to profile your code whilst the unit tests are being run and show you what parts of your code are
covered.

Checking code coverage whilst running your tests can be done from the Unit Tests window by right clicking a
test class or method and selecting Run with code coverage. This menu option is also available when you right
click a test method or test class in the source code editor.

Whilst running the tests with code coverage any output from PartCover will be displayed in the Output window.
After the test run is completed any failing tests will be displayed in the Errors window.

To see a summary of the code coverage results, from the View menu select Tools and then select Code
Coverage.

This opens the Code Coverage window.


The Code Coverage window shows the percentage code coverage for classes and methods. Double clicking a
class or method will open up the corresponding source file. Double clicking a row in the list view will open up
the corresponding source file and move the cursor to the appropriate line. The toolbar button in the top left
corner of the Code Coverage window allows you to enable or disable the highlighting of the covered source
code in the editor. This option is also available when you right click inside the text editor.

With this option enabled, the source code editor will highlight the lines of code that have been covered in green,
and uncovered lines in red.
Editing XML

Whilst editing XML files SharpDevelop provides namespace, element, attribute and attribute value completion
based on its schema.

Namespace Completion

On typing the = character, after the xmlns attribute, a list of namespaces will be shown. A namespace in the list
can be selected in a similar way to code completion by using the up and down cursor keys, or by typing in the
first few characters of the namespace, and then pressing the Tab key to insert the namespace.

Element Completion

On typing an opening tag a list of elements that can be added at the current location will be displayed.

Attribute Completion
On typing a space character after an element name a list of attributes for that element will be shown. Inserting the
attribute will also insert the attribute value quotes ready for you to type in its value.

Attribute Value Completion

On typing the first character of the attribute value a list of possible values, as inferred from the schema, are
displayed.

Schema Annotation

If the schema contains any annotation then this will be displayed.

XML Validation
To validate an xml document against its schema, first associate a namespace with it using the xmlns attribute,
then from the XML menu select Validate XML.

Errors will be shown in the Output window and the Errors window.
Clicking an entry in the Errors window will move the cursor to the position of the error.

Configuring Schemas
The XML schemas that SharpDevelop is aware of can be viewed and configured from the options dialog box.
From the Tools menu select Options..., expand the Text Editor category and select XML Schemas.

In the Schemas group box schemas that have been added by you are highlighted in black text. System schemas,
those that are installed with the XML Editor, are displayed in grey text. Only schemas that have been added by
you can be removed. Clicking the Add button opens up the open file dialog allowing you to browse to an .xsd
file and add it.
The File Extensions group box shows default associations between a file extension and a schema. For example,
.build files could be associated with a NAnt schema, then whenever a .build file is opened it automatically gets
XML completion for that schema without having to specify an xmlns attribute. Specifying an xmlns attribute will
override the default schema association. To associate a schema with an xml file extension click the browse button
and select a schema from the list.

The Prefix text box is used to specify the default namespace prefix associated with an xml element. This prefix
will be used in the completion list (eg. "xs:schema").

Creating Boo Applications

Boo is a .NET programming language, created by Rodrigo Barreto de Oliveira, which has a similar syntax to
Python. More information about Boo can be obtained from its home page http://boo.codehaus.org/.
SharpDevelop has support for Boo by providing project and file templates, code completion, source code syntax
highlighting, and the ability to build Windows Forms applications or console applications written in Boo.

Вам также может понравиться