Sie sind auf Seite 1von 26

C# Programming

Windows Form Application Design and Coding

Visual Studio Options

Create a new project


The project template determines the initial files, assembly references, code, and property settings that are added to the project If the create directory for solution box is checked, visual studio creates folder for the solution and a subfolder for the project. Otherwise, these folders are stored in the same folder If the save new projects when created option is on, the project is saved right after its created. If you want to target a version of the .net framework other than version 4, you can select the version from the drop down menu

Designing a Form

3 ways to add a control to a form


Select the control from the toolbox, then click and drag to the workspace Double click the control on the toolbox, then the control is placed on the upper left corner of the form Clicking the control and clicking the form to place the control

Working with controls


To move a control, drag it To size a selected control, drag one of its handles To select multiple controls, hold down the shift or ctrl key as you click on each control To align, size, or space a group of selected controls, click on the control to make it a primary control

Control Properties

The properties window displays the for the currently selected control To change the property, enter the value into the textbox of the property To change the properties for two or more controls of the same time , select the controls then select the common properties A brief explanation of the property appears on the bottom of the window Properties can be sorted alphabetically and by category

Common Property

Name Text Acceptbutton Cancelbutton Startposition Enabled Readonly Tabindex Tabstop Textalign

Introduction to coding
Object oriented programming Instantiation Classes Properties defines characteristics and data Methods determines the operations Events and members

Classes and objects concepts


Object-self contained unit that combines code and data A class is the code that defines the characteristics of an object An object is an instance of a class

Property, method, and event concepts


Properties define the characteristics of an object Methods are the operations that an object can perform Events are signals sent by an object to the application telling it that something has happened that can be responded to Properties, methods, and events can be referred to as members of an object Instantiating 2 or more objects, they have same members

Class member syntax


Classname.membername Objectname.membername Properties txtTotal.Text = 10 txtTotal.ReadOnly = true Methods This.close() Event btnExit.Click

Common Events
Control click double click enter leave Form Load Closing Closed

Event Concepts
Windows forms applications work by responding to events that occur on objects To indicate how an application should respond to an event , you code an event handler, which a special type of method that handles the event To connect the event handler to the event, visual studio automatically generates a statement that wires the event to the event handler An event can be an action thats initiated by the user like the click event , or it can be an action initiated by program code like the closed event

How to create an event handler


1.

2.

3.

In the form designer, double click the control. The code window opens and generates the declaration for the method that handles the event, and places the cursor within its declaration Type the C# code between the { } You can return to the designer by clicking the view designer button

Load Event

The method declaration for the event handler thats generated when you double click on an object in the form designer includes a method name that consists of the object name, an underscore , and the event name The event handler is stored in the cs file in the form Most of the code thats generated when you design a form, including the statement that wires the event to the event handler, is stored in the designer.cs file in the form

IntelliSense
Lists keywords , data types , variables, objects, and classes as you type so you can enter them correctly When you highlight an item in the completion list , a tooltip is displayed with the information about the item Pressing the ctrl key will open the code behind the list

Coding rules
Use spaces to separate the words in each statement Use exact capitalization for all keywords, class names, object names, variable names, End each statement with a semicolon Each block of code must be enclosed in {}

Syntax Errors
Visual studio checks the syntax of your C# code as you enter it A wavy underline is highlighted in a syntax error All errors are listed in the error list

Readable style coding


Use indentation and extra spaces to align or group codes Use spaces to separate words, operators , and values in each statement Use blank lines before and after groups of related statements

Comments
Used to document a part of a program Can be single line(//) or multiline(/* */)

Text editor toolbar


To comment or uncomment several lines of code, select the lines and click the comment or uncomment button from the text editor toolbar To collapse or expand regions of code, use the plus(+) or minus(-) sign The occurrences of a highlighted codes in the editor is shown

Snippets
To insert a code snippet, right click in the code editor and select the insert snippet command from the resulting menu. Then, double-click the folder that contains the code snippet you want to insert, select the code snippet and then press the tab or enter key You can also insert a code snippet by selecting an item from a completion list that has a code snippet and then pressing the tab key twice To surround existing code with a code snippet, select code, right-click on it, and select the surround with command from the resulting menu, then select the appropriate snippet Code snippets manager displays a dialog box that list the available snippets

Running the project


To run a project, press f5 key Syntax errors are detected Double clicking the error in the error list brings to the area of the specific error

Testing the project


Test the user interface Test valid input data Test invalid data or unexpected user actions

Runtime errors
Fixing errors are called debugging When errors are encountered, it enters break mode The exception assistant suggests solutions

Das könnte Ihnen auch gefallen