Zurück

.NET C# Windows Form Recommendations


Use your own Application Layer

Do not inherit your Forms and Controls directly from the .NET Framework. Create your own Library and inherit from this Application Layer and you can share your code, properties and visual representation among many instances. If you have to change for example the background color of all your controls, the only thing you have to do is to change the background color in the ancestor object in your own library. More Information can be found here.

Use Naming Conventions

A consistent naming pattern is one of the most important elements of predictability and discoverability in a managed class library. Widespread use and understanding of these naming guidelines should eliminate unclear code and make it easier for developers to understand shared code. More Information can be found here.

Use DataSet's instead of DataReader

DataSets are very similar to PowerBuilder's powerful Datawindow. They offer you an easy and efficient way to manipulate your data fetched from a database or XML-File. You don't have to worry about all the details if you decide to read the data row by row with a DataReader - therefore use DataReaders very sparingly.

Synchronize Master-Detail Relationships with DataBinding

A dataset can contain multiple tables, including tables that have an implicit relationship between them — tables with key fields in common. To take advantage of this implicit relationship, you use an ADO.NET DataRelation object. Do not use the SelectedIndexChanged or any other RowFocusChanged Event located on the master control to synchronize. Let the Form's BindingContext do it for you automatically. More Information can be found here.

Manage Row Position with the CurrencyManager

In a Windows application, navigation through records in a data source is managed by the data-binding layer. The CurrencyManager object associated with a table or view in a dataset supports a Position property. The Position property is used by the data-binding mechanism to make sure that individual controls all read and write data to the same record. Do not use your own code to navigate through records, if you get this task automatically by the Position Property. More Information can be found here.

Create Custom Controls

User controls provide a means by which custom graphical interfaces can be created and reused. A user control is essentially a component with a visual representation. As such, it might consist of one or more Windows Forms controls, components, or blocks of code that can extend functionality by validating user input, modifying display properties, or performing other tasks required by the author. User controls can be placed on Windows Forms in the same manner as other controls. Do not create repetitive controls over and over - use User controls. More Information can be found here.

Use Windows Forms Inheritance

On some occasions, you may decide that a project calls for a form similar to one that you have created in a previous project. Or you may want to create a basic form with settings such as a watermark or certain control layout that you will then use again within a project, with each iteration containing modifications to the original form template. Form inheritance enables you to create a base form and then inherit from it and make modifications while preserving whatever original settings you need. Do not create similar forms over and over - inherit from your own ancestor forms or dialogs. More Information can be found here.

Do Not Use SELECT * FROM ...

If somebody have to add a new column to the table, your code will fail if you use SELECT * FROM ..., always use SELECT col1, col2 ... FROM ...