Title: Developing Windows and Web Applications using Visual Studio'NET Session 4: Better Windows Forms
1Developing Windows and Web Applications using
Visual Studio.NETSession 4 Better Windows Forms
2Session Prerequisites
- The browser over promises
- Dont give the customer value
- Too slow
- The browser is a very weak and stupid program
3.NET Pet Shop 2.0
4.NET Pet Shop 2.0
5- so for the same reason I like Windows Forms as
there is less code to write
6Agenda
- Goals
- Why choose Windows Forms?
- How to do efficient Windows Forms
- Giving you a Toolkit
7Rule 1 Do you know why you choose Windows Forms
8Why Windows Forms
- Bandwidth Presentation layer
- Bandwidth - Compression
- Cache
- Vs. the browser button
- Faster Server
- Because of less requests and less work thanks to
processing power being used on client - Richer interface
- No buggy DHTML/JavaScript
- More responsive
- Faster to develop
- No cross-browser issues
- Build complex controls quickly
- More people are happy!
9Who Do I Please?
Browser Based Solution
Rich Client Solution
?
?
- Network Admins
- Developers
- End Users
- Accounts
?
?
?
?
?
?
Conclusion As .NET Framework becomes ubiquitous,
expect to see smart client applications
displacing browser-based apps, especially for
data-intensive purposes
10Why NOT Windows Forms?
- Not allowed in Standard Operating Environment
(SOE) - Environment not known
- Non-Windows OS
11Rule 2Do you design a mockup UI first?
12Designing a Mockup UI
- Database schema should be designed before the
mockup UI is started
13Rule 3Do you use code generators?
14Code Generation
- Code Generation should be set up when the mockup
is done - Have a _Regenerate.bat file to recreate data
access layer stored procs
15Rule 4Do you use red and yellow colors to
distinguish elements in the designer?
(1)
16Red Yellow colours
- Red for incomplete elements
- Yellow for deliberately invisible elements
17Rule 5Do your applications support XP themes?
- In Visual Studio .NET 2003
- Call Application.EnableVisualStyles() at the top
of the Main method of the application - Set the FlatStyle property of each control to
System - Bad Example Good Example
18Rule 5Do your applications support XP themes?
- In Visual Studio .NET 2005
- Simply set it in the project property page
-
19Rule 6Do you use inherited forms for consistent
behaviour?
(2)
20Inherited Forms For Every Form
- Common Behaviour
- Company Icon
- Remembering its size and location
- Adding itself to a global forms collection if SDI
(to find forms that are already open, or to close
all open forms) - Logging usage frequency and performance of forms
(load time) - No Controls!
21StartPosition
- CenterParent only for modal dialogs (to prevent
multi-monitor confusion) - CenterScreen only for the main form (MainForm),
or a splash screen - WindowsDefaultLocation for everything else (99
of forms) - prevents windows from appearing on
top of one another
22FormBorderStyle
- FixedDialog only for modal dialog boxes
- FixedSingle only for the the main form (MainForm)
- FixedSingle has an icon whereas FixedDialog
doesn't - None for splash screen
- Sizable for any form that has multi-line textbox,
grid, listbox or such
23Base Data Entry Form
3
4
1
2
24Rule 7Do you encapsulate (aka lock) values of
forms?
(1)
25Hiding Values
26Hiding Values
using System.ComponentModel Browsable(false),
EditorBrowsable(false) public new Font Font
get return base.Font set
base.Font value
Imports System.ComponentModelltBrowsable(False),
EditorBrowsable(false)gt _ Public Shadows
Property Font() As Font Get Return
MyBase.Font End Get Set(ByVal Value As
Font) 'MyBase.Font Value 'normal
property syntax MyBase.Font New
Font(Me.Font.FontFamily, 20) End SetEnd
Property
27Rule 8Do you know when to use User Controls?
(2)
28User Controls
- Pros
- You can use a user control more than once on the
same form eg. Mailing Address, Billing Address - You can reuse logic in the code behind the
controls e.g. Search control - User controls are less prone to visual
inheritance errors - Cons
- You lose the AcceptButton and CancelButton
properties from the Designer eg. OK, Cancel,
Apply. Therefore the OK, Cancel and Apply buttons
cannot be on User Controls.
29User Controls
- Controls only used once
- Bad!
30User Controls
31User Controls TabPages
32User Controls TabPages
- Possible Exception
- When a form has multiple tabs, and each tab has
numerous controls it can be easier to use User
Control in this case - Smaller designer generated code
- More than one person can be working on a
different tab
33Rule 9Do you know how to design a user friendly
search system?
(2)
34Search System
35Search System
- Good
- Separate from entry fields
- Advanced, field based, search criteria
36Rule 10Do you use validator controls?
(1)
37ErrorProvider
- Code intensive
- Must manually handle the Validating event of each
control you want to validate - Must be manually running the validation methods
when the OK or Apply button is clicked
38Validator Controls
- Good
- No code, all in the designer (integrates with the
ErrorProvider)
39Validator Controls
- One Final Thought
- Unfriendly Msgboxs Upset People
40Rule 11Do you use DataSets or create your own
business objects?
(2)
41Passing data through layers
- There are two kinds of people
- Those that use DataSets
- Those that want to use DataSets, but instead use
Business objects
42DataSets vs Business Objects
- DataSets
- Code Generation
- CRUD functionality
- Concurrency
- Data binding
- Business Objects
- Better performance
- Combine data storage with business logic
Developing Objects vs DataSets
43Rule 12Do you use a status bar to show load
time?
- Monitor Performance better
- Good user feedback
- Easy to do
44Rule 13Do you not cache lookup data in your
Windows Forms application?
- Leads to Synchronisation issues
- Exception When application can be taken offline
easily
45Rule 14Do you use the designer for all visual
elements?
(2)
46Exception
- Bad Non-visual elements in designer
47- Good Only visual elements in designer
48Rule 15Do you always use the Visual Studio
designer for data binding where possible?
(1)
49Bad write your own boring code
Private Sub OnLoad() OrderDetailsService.
Instance.GetAll(Me.OrderDetailsDataSet1)
Dim row As OrderDetailsDataSet.OrderDetailsRow
Me.OrderDetailsDataSet1.OrderDetails(0)
Me.TextBox1.Text row.UnitPrice.ToString("c")
End Sub Private Sub Save() Dim row
As OrderDetailsDataSet.OrderDetailsRow
Me.OrderDetailsDataSet1.OrderDetails(0)
row.UnitPrice Decimal.Parse(Me.TextBox1.Text,
System.Globalization.NumberStyles.Currency)
End Sub
50Using the Designer
- SimpleBinding to a single property
- ComplexBinding to a list
51Rule 16Do you avoid using MDI forms?
(2)
52MDI Forms
53MDI Forms
- Hangover from Access 2.0 and Windows 3.1
- Clutter the application
- Only one window on the taskbar
- No multiple monitor support
54Rule 17Do you have a correctly structured
common code assembly?
(2)
55Note Also clean up directory BAD Which file do
you use?
56Common Code Assembly
- Good Use across multiple projects
- Common (SSW.Framework.Common)
- Code which is not UI specific
- Example Code to convert a date into different
formats - CommonWindows (SSW.Framework.WindowsUI)
- Example Base forms which are the same for all
products, wizard frameworks - CommonWeb (SSW.Framework.WebUI)
- Example Generic XML-based navigation components
57Rule 18Are your Data Access Layers compatible
with Web Services?
- When running locally, use direct database
connection. - When running remotely, use Web Services for
better performance.
58How?
- Three ways to do it
- Bad example
- Lots of if statements (really messy - most people
try this first) - Better example
- Interfaces (Implements statement in VB)
- Best Example
- Factory pattern (best - most flexible and
extensible approach)
59Rule 19Do you include Exception Logging and
Handling?
(2)
60Handling Exceptions
- All unhandled exceptions should be logged
- EMAB
- Full source code provided by MS
- Fully extensible with custom logging target
extensions - Extended by SSW to log all exceptions
- Log4Net
- Open-source
- Log writing methods of different severities
- http//sourceforge.net/projects/log4net
61SSW ExceptionReportingService
- Web service which extends EMAB
- http//webservice.ssw.com.au/exceptionreportingser
vice/
62Rule 20Do you make a strongly-typed wrapper for
App.config?
63Not good
public static DatabaseAccessObjectFactory
CreateDataObjectFactory() return new
SqlDatabaseAccessObjectFactory(
ConfigurationSettings.AppSettings"ConnectionSt
ring")
public static DatabaseAccessObjectFactory
CreateDataObjectFactory() return new
SqlDatabaseAccessObjectFactory(
AssemblyConfiguration.ConnectionString)
64How to Do it
using System using System.Configuration namespa
ce SSW.Northwind.WindowsUI public sealed
class AssemblyConfiguration // Prevent
the class from being constructed private
AssemblyConfiguration() public static
string ConnectionString get
return ConfigurationSettings.AppSettings"Conne
ctionString".ToString()
65Rule 21Do you replace the standard .NET
DataGrid?
66Reasons
- Need sorting on column headings (when you bind to
data objects) - Combo box
- Hyperlinks
- Buttons
- Options
- Infragistics
- Janus Grid
67Whidbey DataGridView
68Rule 22Do you replace the standard .NET date
time picker?
69Rule 23Do you avoid 3rd party menus toolbars?
- We have tried several third party menu and
toolbar controls and all of them had serious bugs
70Menus Toolbars in Whidbey
- Office 2003 style menus and toolbars with the new
ToolStrip control - Easy to upgrade to
- Upgrading from 3rd party controls will be
difficult
71Rule 24Do your List Views support multiple
selection and copying?
72- Good Example Multiple select Copy
73Rule 25Do you use an image button for opening a
web page taking action?
- Note Simple Links are for navigation only (no
action taken)
74Bad Example 1
75Bad Example 2
76Bad Example 3
77Good Example
- In this example, the action is generating
reports, passing and processing values
78Rule 26Do your forms have Accept and Cancel
buttons?
79Rule 27Do you name your Accept button OK or
Next?
80- If wizard, it should be Next, otherwise OK.
81Rule 28Do you make common control with certain
width?
82Bad/Good Example 1
83Bad/Good Example 2
84Rule 29Do your Windows Forms applications
support URLs?
85URLs in Outlook
86URLs
- Q What does a browser have that a Windows
Application doesnt have? - A A URL
87Rule 27Do you include Back and Undo?
- Store the previous actions in a dataset in
memory. - Example Outlook Advanced toolbar
88Rule 28Do you use NUnit to write Unit Tests?
- Assumed
- But what about the GUI
89- http//www.windowforms.net
- http//www.ssw.com.au/SSW/Standards/
- Please plan for Whidbey
90SSW .NET Toolkit
- http//www.ssw.com.au/SSW/NetToolkit
912 things
JatinValabjee_at_ssw.com.au EricPhan_at_ssw.com.au
92Thank You!
93(No Transcript)