8 winforms applications
play

8. WinForms Applications Writing native Windows programs Overview - PowerPoint PPT Presentation

8. WinForms Applications Writing native Windows programs Overview Windows Applications Events and event handlers Layered (tiered) model of software Focus Form designer and controls Dialog boxes and forms Delegates


  1. 8. WinForms Applications Writing native Windows programs

  2. Overview � Windows Applications � Events and event handlers � Layered (tiered) model of software � Focus � Form designer and controls � Dialog boxes and forms � Delegates � Visual Inheritance

  3. Windows vs Console apps � A console application has a very limited user- interface � Keyboard for input � Console window (text only) for output � Compared to this a Windows program provides a much richer range of interaction methods � On-screen buttons, scroll bars, lists etc. � Multiple windows to categorize and simplify output and input � The penalty for this is a more complex application structure

  4. Windows and Events � Console programs follow an Input Process Output Input � Process � Output model � Easy to program � By contrast, a Windows program can have many different forms of input � Programmer must orchestrate the various sources Software � Each source of input fires ‘events’ the program must react to

  5. Event handlers � Each source of events can kick off the execution of a block of code called an event handler � An event handler is simply a Sub that is called automatically by Windows in response to some external interaction � Typing into a text control etc. � ‘Clicking’ on a control � Selecting an item in a list � Often, the event incorporates some additional information – which key was pressed, the exact location of the mouse cursor etc. � This information is passed in the form of parameters to the event handler

  6. Layers of software � Because of the wide range of stimuli (events) a program has to react to, the structure of a Windows application is more complex � One strategy for simplifying the structure is to try to keep application specific code and user-interface code separate � Application specific code is usually the set of classes that do the actual job required of the program, often called the ‘business classes’ � User-interface code accepts user input and commands and directs the business classes according to these � These two separate parts of a system can be thought of a ‘tiers’ or ‘layers’ in a system � User-interface tier comes between the user and the business tier and is responsible for interpreting user interactions and effecting the appropriate changes in the business tier

  7. A 2-Tier Application User- Interface Tier Handles User Interactions Business Tier Implements Business Rules

  8. WinForms basics � A Form is a top-level Window that is hosted and managed by the Windows operating system and which receives external interactions in the form of events � Controls in Visual Basic are toolbox items that can be placed on a form to act as user-interface elements (or widgets) � Windows considers a control to be another type of Window – a rectangular area of the screen that can receive the focus (explained later) and events � This confuses the issue, and so the Visual Basic term ‘Control’ is a better description

  9. The Focus � The system mouse can, by virtue of its pointer, be used to direct input to any visible window on a screen � The input is necessarily simple – movements, clicks, drags � The keyboard is a richer form of input, but there is no natural way of directing its input � To get over this, the Windows system registers which window has the current focus � In a text-oriented control, this is the cursor and acts as the insertion point for typed characters from the keyboard � In other controls, the focus changes the control’s appearance slightly to indicate which control is currently receiving keyboard events � Focus can be changed using the Tab key on the keyboard � TheTabIndex of a control indicates its place in the tab order

  10. Events and event handlers � Events can originate from � Forms or controls on forms � System hardware (e.g. the real-time clock) � Other windows programs � Events are usually received by forms � Normal event-handlers are Subs on a form � Event-handlers can be coded into other modules, but this involves more work � A form-based event handler is usually associated with a specific event from a specific control � It has two parameters, indicating the object that sent the event (sender) and an object containing related information (e)

  11. Example event handler Private Sub txtName_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles txtName.KeyPress Dim ch As Char ch = e.KeyChar() e.Handled = True End Sub � The above handler is set to receive KeyPress events from a text box named txtName � The sender parameter is simply a reference to this text box � As a KeyPress event handler, you can rightly expect that the other parameter will contain information on the key pressed – which key, whether Shift or Ctrl was pressed, etc.

  12. Event lists � A typical control can source a wide range of events under different circumstances � Only events for which a handler is defined will have any effect List of potential events from a TextBox control

  13. The Visual Studio form designer � The Form-Designer performs a complex task – synchronizing the graphical design of a form with the code � The actual ‘Form’ is a class that inherits from the Form class. The graphical designer is simply a different view of it – a graphical class configuration panel � As controls are added, properties changes and design wizards executed, the form code is updated to reflect the changes � Each control is a variable, and code to initialize it � Each property change is a statement that assigns a new value to a property � A double click on a control generates a skeleton event handler

  14. Windows controls � The standard WinForms toolbox contains a wide range of controls. e.g. � Text input: TextBox, RichTextBox, ComboBox � Mouse input: CheckBox, OptionButton, ListBox, ScrollBar � Display and/or organize: Label, PictureBox, Panel � Command: Button, Menu, Timer � All can be configured by the properties window or in code

  15. Dialog boxes � A dialog box is a form that performs complete operations � Buttons provided to confirm (usually OK) or cancel (usually Cancel) the operation � Operation typically involves a number of data values, which after editing them, the user selects OK or Cancel � A VB Form has AcceptButton and CancelButton properties that can refer to these buttons � This allows the DialogResult to reflect which button was pressed – True for OK, False for Cancel � A dialog box provides a Modal operation – while it is in operation, the calling code has to wait for it � This imposes a level of control over the user (1 dialog at a time)

  16. Forms � A dialog box is a special type of form � More generally, any number of forms can be in operation simultaneously � This can be a more difficult scenario to control � Generally, use a single form to input and display the properties of an object and to initiate its methods � A number of strategies can be taken to maintain control. e.g. � The object can ‘own’ (initiate, interrogate and dispose of) a form and initiate its own display, update etc. � A form can ‘contain’ an object, and update and display it (more suitable where one form is used to browse a collection of objects

  17. Delegates and event handlers � A delegate is defined as a type of Sub � A stand-in for real subs � Delegate defines name, parameters and return type, but does not contain code � The purpose of this is to be able to include a call to a sub in code before the sub has been written � Why would you do this? – think of what an event handler is � Once a delegate type has been defined, a real sub can be assigned to it (just like a variable) � Then, a call to the delegate will be a call to the sub assigned to it � e.g. Delegate Sub AlarmCall(ByVal AlarmMessage As String) Dim AC As AlarmCall � We can now assign a Sub (that matches AlarmCall) to AC

  18. Example of delegation Class AlarmClock Private myTime As Date Private myMessage As String 'Here is the delegate declaration... Using the delegate Private myResponse As AlarmCall Public Sub New(ByVal AlarmTime As Date, _ included here as a ByVal Message As String, _ template, we can write ByVal SubToCall As AlarmCall) a sub that will say myTime = AlarmTime myMessage = Message what happens when 'Assign a Sub to the delegate... an alarm goes off. myResponse = SubToCall End Sub Public Sub Go() Do If TimeOfDay >= myTime Then 'A call to the delegate method... myResponse(myMessage) Exit Do End If Loop End Sub End Class

  19. Example of delegation (contd.) Any of these 'This one displays a message on screen... three subs can be Sub AlarmMessageBox(ByVal aMessage As String) MessageBox.Show(aMessage) assigned to the End Sub delegate, and will 'This one changes a form’s caption... be called when Sub AlarmCaption(ByVal aMessage As String) the alarm goes MainForm.Caption = aMessage off. Note – all End Sub have same 'This one sends an email... parameters as Sub AlarmEmail(ByVal aMessage As String) the delegate. SendMail("me@mydomain.com", "Alarm", aMessage) End Sub Sub Main() Dim AC As AlarmClock = _ New AlarmClock(TimeValue("10:00:00"),_ "Go to airport", AddressOf AlarmEmail) AC.Go() End Sub

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend