Windows Desktop Applications WINDOWS FORMS Windows Forms June

  • Slides: 40
Download presentation
Windows Desktop Applications WINDOWS FORMS Windows Forms June 8, 2021 1

Windows Desktop Applications WINDOWS FORMS Windows Forms June 8, 2021 1

Windows Desktop Applications are implemented in C# using Windows Forms There are. NET classes

Windows Desktop Applications are implemented in C# using Windows Forms There are. NET classes that represent windows, buttons, text boxes, menus, list boxes, and so forth With experience, they are very easy to use: Drag and Drop from the “toolbox” Set properties Handle events Windows Forms June 8, 2021 2

Event Driven Programming Console applications are essentially sequential code executed in the order specified

Event Driven Programming Console applications are essentially sequential code executed in the order specified by Main and whatever it calls Windows programs are event-driven Main instantiates a main window Subsequently, the order of code execution is primarily determined by events, many of which are triggered from outside the code �User clicks a mouse �User presses a key, producing keystroke �User scrolls �Timer expires �I/O operation concludes �And so forth Windows Forms June 8, 2021 3

Events During the execution of a Windows program, thousands of events typically occur Every

Events During the execution of a Windows program, thousands of events typically occur Every time the mouse is moved by one pixel over the window, a Mouse. Move event occurs, for example Each time an event related to the program occurs, the program is notified by the system. It may �Take some action �Ignore the event and let Windows deal with it �Windows ignores most events but not all �It typically doesn’t ignore a CTRL-ALT-DELETE event, for example Windows Forms June 8, 2021 4

Dealing with Events Programs typically ignore most events The events to which a program

Dealing with Events Programs typically ignore most events The events to which a program responds and the action it takes give the program its behavior The program may provide one or more methods that execute actions to “do something” in response to the event Such a method is called an event handler An event handler is a method with a signature similar to this private void Handler. Name (Object sender, Event. Args e) Windows Forms June 8, 2021 5

Delegates While it is not necessary to understand delegates to understand event-driven programming, the

Delegates While it is not necessary to understand delegates to understand event-driven programming, the term delegate occurs frequently in the literature The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate As indicated on the previous slide an Event. Handler delegate has the signature: public delegate void Event. Handler(Object sender, Event. Args e); Any method with the same signature can serve as an Event. Handler Windows Forms June 8, 2021 6

Event Handlers Registering an event-handler in C# is analogous to Adding a Listener in

Event Handlers Registering an event-handler in C# is analogous to Adding a Listener in Java Event handlers are “registered” for events to be handled Delegate this. Load += new System. Event. Handler (Form_Load); Load is the event name Event Handler method name which must have same signature as the Event. Handler delegate Visual Studio generates the code to register most event handlers so that the programmer need not do so in most cases Windows Forms June 8, 2021 7

Windows Form Application Create a Windows Forms Application … … with an appropriate name

Windows Form Application Create a Windows Forms Application … … with an appropriate name Windows Forms June 8, 2021 8

Results The Window Driver Program Rename the Form class Visual Editor for the Form

Results The Window Driver Program Rename the Form class Visual Editor for the Form – Form can be resized, etc. Windows Forms Rename Driver June 8, 2021 9

Main Program No changes or additions to Main program are required for simple Windows

Main Program No changes or additions to Main program are required for simple Windows Form applications Windows Forms June 8, 2021 10

Design and Code Views Code View Windows derived from Form class like Java GUI

Design and Code Views Code View Windows derived from Form class like Java GUI window derived from JFrame Design View Easily switch between the views Windows Forms June 8, 2021 11

Design View and Toolbox Controls in the Toolbox are represented by. NET classes Dragging

Design View and Toolbox Controls in the Toolbox are represented by. NET classes Dragging and dropping a control onto the form creates an object of the class in the code Standard controls can be placed on a form The control may be placed at a desired location on the form and sized to suit your needs Windows Forms June 8, 2021 12

Each Form has Two Code Files Windows Forms June 8, 2021 13

Each Form has Two Code Files Windows Forms June 8, 2021 13

Each Form Has 3 Files These represent the C# code, the designer code file,

Each Form Has 3 Files These represent the C# code, the designer code file, and the visual components of the form In you plan to copy a form and paste it into another project, be sure to copy all 3 of the files Windows Forms June 8, 2021 14

Designer. cs The designer code file contains VS-generated code that defines the controls dropped

Designer. cs The designer code file contains VS-generated code that defines the controls dropped on the form and their properties, including their event handler registrations Note the generated code for registering this event handler Windows Forms June 8, 2021 15

Properties of Controls Each control has an extensive list of properties that allows the

Properties of Controls Each control has an extensive list of properties that allows the designer/programmer to manage many of the features of the control such as Colors Fonts Size Style Object name Caption (Text) Many more Windows Forms June 8, 2021 16

Setting Properties of Controls Properties can be set or retrieved At design time in

Setting Properties of Controls Properties can be set or retrieved At design time in the Design View �Changes show up both in the visual editor and in the code files �Changes in the visual editor change the properties and change the code �Changes in the code may change the properties and the visual appearance in the editor (depending on whether the code is “definition” code or to be executed at run time) At run time – via program code Windows Forms June 8, 2021 17

Design Time: Setting Properties of Controls Object name in code Button caption Background color

Design Time: Setting Properties of Controls Object name in code Button caption Background color of Button Text Alignment Font Color Is button visible? Font Windows Forms June 8, 2021 18

Editing Properties: Example Windows Forms June 8, 2021 19

Editing Properties: Example Windows Forms June 8, 2021 19

Properties in Code After changing the properties, VS has added code to the Designer

Properties in Code After changing the properties, VS has added code to the Designer file to make the changes Windows Forms June 8, 2021 20

Making Simultaneous Changes to Multiple Controls Drag a rectangle around desired controls Change properties

Making Simultaneous Changes to Multiple Controls Drag a rectangle around desired controls Change properties that are in common to the selected controls Can see the results All included controls are selected Windows Forms The one with white handles is the “master” June 8, 2021 21

Controls Toolbar Send to Front or Back Windows Forms June 8, 2021 22

Controls Toolbar Send to Front or Back Windows Forms June 8, 2021 22

Align and Size Controls With all buttons selected, use the toolbar to align and

Align and Size Controls With all buttons selected, use the toolbar to align and resize them appropriately Made same size, aligned vertically, centered horizontally The entire form also has properties to set Windows Forms June 8, 2021 23

Form Properties Windows Forms June 8, 2021 24

Form Properties Windows Forms June 8, 2021 24

Form Properties Designate a button to be activated by pressing the <ENTER> key Designate

Form Properties Designate a button to be activated by pressing the <ENTER> key Designate a button to be activated by pressing the <ESC> key Windows Forms June 8, 2021 25

Form Properties Specify where window appears on screen Center the window on the screen

Form Properties Specify where window appears on screen Center the window on the screen Neither maximized nor minimized originally Windows Forms June 8, 2021 26

Complete Form with More Controls Textboxes – all set to rightaligned text because they

Complete Form with More Controls Textboxes – all set to rightaligned text because they are for numeric input; last is read-only Label controls Windows Forms Align groups of controls to make the form look professional June 8, 2021 27

Tab Order For controls such as Text. Boxes and Buttons that can have the

Tab Order For controls such as Text. Boxes and Buttons that can have the keyboard focus, there is a tab order than one can set to control the order in which the controls receive focus as the tab key is pressed repeatedly Windows Forms June 8, 2021 28

Tab Order Click in the order you want focus to move. The Label for

Tab Order Click in the order you want focus to move. The Label for a Text. Box should be clicked just before the Text. Box Not clicked since focus cannot be on a read-only control Windows Forms June 8, 2021 29

Tab Order Run the application and this window will appear Cursor is here, rightjustified

Tab Order Run the application and this window will appear Cursor is here, rightjustified initially 2 3 Windows Forms 4 5 If tab is pressed multiple times focus will move in the order of the numbers June 8, 2021 30

Current Status At this point, the program runs successfully but does very little We

Current Status At this point, the program runs successfully but does very little We can see the form We can type into the two input textboxes We can click buttons Nothing happens if we do these things This is all of the “code behind” the form at this point Windows Forms June 8, 2021 31

Handling Some Events Three primary events we need to handle are the click events

Handling Some Events Three primary events we need to handle are the click events for the 3 buttons In the Design View, we double click each button A skeleton of the Event Handler for each button is written in the “code behind”. cs file Each Event Handler is registered in the designer. cs file Windows Forms June 8, 2021 32

Event Handler Skeletons 3 event handler skeletons added Event Handler registered Windows Forms June

Event Handler Skeletons 3 event handler skeletons added Event Handler registered Windows Forms June 8, 2021 33

Add Event Handler Code For the Quit button, we want to exit the program

Add Event Handler Code For the Quit button, we want to exit the program For the Clear button, we want to make the text boxes empty and set the focus to the first text box Run the program and observe how the Quit and Clear buttons work Windows Forms June 8, 2021 34

The Calculate Button Handler When Calculate is pressed, we want to get the points

The Calculate Button Handler When Calculate is pressed, we want to get the points and hours values from the form and divide to calculate GPA Convert to decimal if possible Else, show error message, reset form, and exit Windows Forms If OK so far, display 35 June 8, 2021 result

Errors The Try. Parse method takes care of some of the input errors a

Errors The Try. Parse method takes care of some of the input errors a user may make Empty input fields Non-numeric input It does not handle others such as Entering 0 for the number of Hours Earned (divide by 0 error) Producing a GPA of 57. 2 or other invalid value We must add code to handle the other anomalies Windows Forms June 8, 2021 36

Edited Handler Windows Forms June 8, 2021 37

Edited Handler Windows Forms June 8, 2021 37

Other Events One may wish to display a “Goodbye” message when the application ends

Other Events One may wish to display a “Goodbye” message when the application ends Can be done in the btn. Quit_Click handler for that approach to quitting the application but there are … Other ways to quit �Red X �System Menu Close �Alt-F 4 Use Form. Closing event to catch them all at once Windows Forms June 8, 2021 38

Closing Event In the Design View, click on the lightning bolt in Properties to

Closing Event In the Design View, click on the lightning bolt in Properties to get a list of all Events for the form Double-Click Form. Closing Events Windows Forms June 8, 2021 39

Load Event Just before the window becomes visible for the first time, the Load

Load Event Just before the window becomes visible for the first time, the Load event is recognized It gives the programmer a chance to do some things at run time just before the window is shown For example, we might want to do something like: Display date and time Display info about the application such as title, author, version, etc. Give initial values to some controls Windows Forms Set window caption Get current date and format it for display as 8, 2021 “Thursday, June 5, 2014” 40