Академический Документы
Профессиональный Документы
Культура Документы
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.
Code Completion
Code Generation
Refactoring
XML Comments
Code Navigation
Debugging
Unit Testing
Code Coverage
Editing XML
With SharpDevelop you can create and develop many different types of project:
Class Library
Console Application
Direct 3D Application
WPF Application
Windows Application
Windows Service
The following sections will walk you through creating a Windows Application for the C# language.
Click the above image to see a larger version with added notes explaining the various parts of the user interface.
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.
Configuration classes
Class
Form
Interface
Module
MSBuild File
Singleton Class
Struct
Unit Test
User Control
Web Control
Web Form
Web Service
WPF Page
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....
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.
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.
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.
Constructors
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.
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
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.
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.
To remove any unused import statements from the currently open file, select Remove unused import
statements from the Refactor menu.
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.
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.
As you type SharpDevelop provides XML element completion inside the comment.
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.
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....
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.
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.
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.
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.
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.
On typing the first character of the attribute value a list of possible values, as inferred from the schema, are
displayed.
Schema Annotation
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").
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.