Unformatted text preview:

Windows FormsSeparation of UI and LogicWindows FormsC# ProgrammingJanuary 10Part IWindows FormsEvent-Driven Model•The System.Windows.Forms library contains controls forbuilding GUI applications•The actual window for a GUI application is an instance ofSystem.Windows.Forms.Form•Controls (such as buttons, text boxes, file menus) get addedto the form, where they sit and wait for events (such as theuser clicking them or mousing over them)•Each control responds to various user and other inputs (suchas the timer tick of an internal cloc k) by raising events•Event handlers can be attached to these events so that theyare executed when they are raised•Delegates are the means by which we can attach handlers toeventsDelegates•You can think of a delegate as a type-safe function pointer•Ie, a variable that holds a function of a particular return typeand parameter list•Delegates decouple the class that declares the delegate fromthe class that uses the delegate•After a delegate has been defined, it can be instantiated,assigned to, and then the contents of this instance (areference to a function) can b e invokedusing System;namespace FirstDelegate{public delegate void MyDel(int a, int b);class Program {private static MyDel someFunction;private static void PrintSum(int x, int y){ Console.WriteLine(x+y); }static void Main(string[] args) {someFunction = PrintSum;someFunction(2, 3);}}}Multicasting•Oftentimes you want to attach several functions to an event(sometimes said subscribe several functions to an event)•You c an attach (and detach) additional functions to aninstance of a delegate using +, -, +=, and -=static void Main(string[] args) {someFunction = PrintSum;someFunction += PrintSum;someFunction(2, 3);}Hello GUI World[demo]Adding Controls Programmatically•Although the Form Designer is great for rapid developmentand prototyping, organizing the UI solely through it canbecome messy•You c an, of course, explicitly write the code to create GUIelements and m odify their propertiesfor (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {Button b = new Button();b.Size = new Size(40, 40);b.Location = new Point(i * 40, j * 40);this.Controls.Add(b);}}Custom Controls•To customize the behavior a regular control, you can extendthat control class•To create a custom control, create a new Windows ControlLibrary project•By default, the class (with default name UserControl1) willbe declared to extend UserControl•Instead, choose the type of control you want to extend (eg,Button, TextBox, Label, etc)•You w ill then have to delete a line assigning to theAutoScaleMode property, which UserControl has but someother c ontrols do not•Add whatever custom event handlers and logic you need tothe class de finitionCustom Controls•Once you compile the custom control project, you can use itfrom a Windows Forms application•To be able to access the custom control class from yourproject, you need to add a reference to it•In the Solution Explorer, right-click the project name (or theReferences folder underneath it), and select Add Reference•Locate the custom control project folder, and then select the.dll file (this should be in the bin subdirectory•Once the reference has been added, the namespace of thecustom control project (and all its classes, etc.) will be inscopeCustom Controls•You c an also add a custom control to the Form Designer’sToolbox•Right-click anywhere in the Toolbox and se lect Choose Items•It takes a long time for this dialog box to appear•The first time you select Choose Items, it takes a really longtime, so be patient•Locate the .dll for your custom control•An entry for the custom control will be added to the Toolb ox,and you can drag instances of it onto a formPart IISeparation of UI and LogicPartial Classes•The Form Designer generates the plumbing code to set up thewindow and its controls•This can often grow large and clutter the rest of your code•To remedy this situation, partial classes were introduced inC# 2.0 to allow a class definition to span multiple files•For a form class Form1, the automatically generated coderesides in Form1.Designer.cs•Note: you should not modify this file!•The rest of your own logic can go in a separate file, likelyForm1.cs, apart from the messy GUI setup codeSeparation of UI and Logic•You m ust be careful to organize your own logic in anintelligent way•The Form Designer is convenient in that it automaticallygenerates stubs for event handlers•It is not a good style, however, to put complex logic in thesehandlers•If in the future you decide to reorganize your GUI and changethe way the user interacts with it, you would not want to do alot of copying and pasting of logic in the event handlers•Instead, you want event handlers to be fairly s traightforwardcalls t o functions that take care of the heavy duty workFinite State Diagrams•The com plexity of the state of a GUI application can growquickly, e ven with a seemingly small number of tasks anapplication needs to complete•The logic for maintaining state and appropriately respondingto e vents fired can easily become convoluted•It is a good idea to think carefully about all possible statesyour GUI application can be in and all the events that can beraised in each•Creating a finite state diagram before you begin coding willmake the developing and maintaining the code much easier•Getting into this good habit early will make development ofmedium and large scale projects much more efficient andeffectiveTraffic Light


View Full Document

Penn CIS 399 - Windows Forms

Download Windows Forms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Windows Forms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Windows Forms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?