High Level Languages - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

High Level Languages

Description:

Delphi makes creating menus easy with the Menu Designer. ... is moved when a user browses the database, inserts records, deletes records and so on. ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 53
Provided by: scie205
Category:

less

Transcript and Presenter's Notes

Title: High Level Languages


1
High Level Languages
  • Delphi
  • Lecture 5

2
Last Lecture
  • Last Lecture we covered the following
  • Memos
  • Strings
  • Dialog Boxes
  • A Simple Word Processor

3
This Lecture
  • In Todays Lecture we will cover
  • Menus in Delphi
  • The menu designer
  • Creating menus by hand
  • Example menu item code
  • Databases
  • Borland Database
  • SQL in Delphi

4
1. Menus
  • Almost all Windows applications use Menus.They
    are a quick way to make your apps look
    professional, and are preferable to buttons.
  • Delphi makes creating menus easy with the Menu
    Designer.
  • All the Menu Designers commands are accessed via
    the Menu Designer context menu or by interacting
    with the Object Inspector.

5
2. Menu Designer
Simply Drag and Drop a Main Menu onto your Form.
Double-Clicking the icon that is now on your form
will bring up the Menu Designer.
6
Menu Designer
  • The Menu Designer has the following features
  • It can create both main menus and context menus.
  • It provides immediate access to the Code Editor
    to handle the OnClick events for menu items.
  • It can insert menus from templates or from
    resource files and it can save custom menus as
    templates.

7
Adding a Main Menu
  • As we saw, to create a main menu on your form,
    the first thing you must do is add a MainMenu
    component to your form.
  • The MainMenu component has very few properties
    and no events. All the menus work is done by the
    individual menu items.
  • To display the Menu Designer, double-click on the
    MainMenu icon.

8
Menu Designer
  • The menu designer looks like a blank form without
    grid points.
  • The menu designer can be sized in any way you
    want. The size is just for convenience and has no
    bearing on how the menu operates at runtime.
  • You can create menus by hand or import menu
    templates.

9
3. Creating a Menu by Hand
  • The menu designer always has a blank menu item
    that acts as a placeholder for any new menu items
    you wish to create.
  • When you first start the menu designer, the blank
    item is selected.
  • If you want to create a File menu (as an example)
    you would click on the Caption property, type
    File and press enter.

10
Use of Ampersand
  • The ampersand () is used to create the
    underlined character for a menu item.
  • The underlined character is the accelerator the
    user can type in combination with the Alt key to
    navigate a menu using a keyboard.
  • For instance, the Exit menu item would be typed
    as Exit, so that x is the accelerator I.e.
    Alt-x will do the same as choosing Exit from the
    menu.

11
Adding to your Menu
  • When you have pressed enter, after typing File,
    the File Menu shows up in the menu designer.
  • A new placeholder is added below the File menu,
    for sub-menu options.
  • In addition, a new pop-up placeholder is created
    to the right of the File menu.
  • You can then go on to add other menus and
    sub-menus in a similar way.

12
Refining your Menu
  • Standard File menu items are New, Open, Save,
    Save As, Print, Print Setup, Exit.
  • A separator is the horizontal line on a menu that
    separates groups of menu items.
  • To add a separator to a menu, just put in a
    hyphen (-) as a menu item.

13
Inserting a Template Menu
  • You can insert standard templates directly into
    the Menu Designer to save time - just right click
    on the Menu Designer and choose Insert From
    Template.
  • A dialog box pops up showing a list of templates
    for you to choose from.
  • You can delete unwanted menu items from an
    installed template simply by choosing an item and
    pressing delete.

14
Template List
Just right click anywhere in the menu designer
and choose Insert from Template.
15
4. Creating Menu Item Code
  • Menus work in exactly the same way as buttons.
    If you double click an item the event code for
    that item will be created for you automatically.
  • The following slides will give you an idea of how
    to program menu item event handler code.
  • We will use the example of a Windows Notepad type
    application, where text is loaded from and saved
    to text files and text it cut and pasted etc.

16
NewClick Event
  • procedure TMainForm.NewClick(Sender TObject)
  • var
  • Res Integer
  • begin
  • Open a file. First check to see if the
  • current file needs to be saved
  • if Memo.Modified then begin
  • Display a message box
  • Res Application.MessageBox(
  • The current file has changed. Save
    changes?,
  • Notepad Message, MB_YESNOCANCEL)
  • ...

17
NewClick Event
  • ...
  • If Yes was clicked then save the current
    file
  • if Res IDYES then
  • FileSaveClick(Sender)
  • If No was clicked then do nothing
  • if Res IDCANCEL then
  • Exit
  • end
  • ...

18
NewClick Event
  • ...
  • Delete the strings in the memo, if any
  • if Memo.Lines.Count gt 0 then
  • Memo.Clear
  • Set the FileName property of the Save Dialog
    to a
  • blank string. This lets us know that the file
    has
  • not yet been saved
  • SaveDialog.FileName
  • end

19
OpenClick Event
  • procedure TMainForm.OpenClick(Sender TObject)
  • var
  • Res Integer
  • begin
  • Open a file. First check to see if the
  • current file needs to be saved
  • if Memo.Modified then begin
  • Display a message box
  • Res Application.MessageBox(
  • The current file has changed. Save
    changes?,
  • Notepad Message, MB_YESNOCANCEL)
  • If Yes was clicked then save the current
    file
  • if Res IDYES then
  • FileSaveClick(Sender)
  • If No was clicked then do nothing
  • if Res IDCANCEL then
  • Exit
  • end
  • ...

20
OpenClick Event
  • ...
  • Execute the File Open dialog. If OK was
    pressed then
  • open the file using the LoadFromFile method.
    First
  • clear the FileName property
  • OpenDialog.FileName
  • if OpenDialog.Execute then begin
  • if Memo.Lines.Count gt 0 then
  • Memo.Clear
  • Memo.Lines.LoadFromFile(OpenDialog.FileName)
  • SaveDialog.FileName OpenDialog.FileName
  • end
  • end

21
SaveClick Event
  • procedure TMainForm.SaveClick(Sender TObject)
  • begin
  • If a filename has already been provided then
    there is
  • no need to bring up the File Save dialog.
    Just save the
  • file using SaveToFile
  • if SaveDialog.FileName ltgt then begin
  • Memo.Lines.SaveToFile(SaveDialog.FileName)
  • Set Modified to False since weve just
    saved
  • Memo.Modified False
  • if no filename was set then do a SaveAs
  • end else FileSaveAsClick(Sender)
  • end

22
SaveAsClick Event
  • procedure TMainForm.SaveAsClick(Sender Tobject)
  • begin
  • Display the File Save dialog to save the file
  • Set modified to False since we just saved
  • SaveDialog.Title Save As
  • if SaveDialog.Execute then begin
  • Memo.Lines.SaveToFile(SaveDialog.FileName)
  • Memo.Modified False
  • end
  • end

23
FileExitClick Event
  • Delphi handles the File Exit routine in a single
    function Close
  • This function does all the background stuff
    needed to close the form. It is the same as
    clicking on the X in the top-right corner of a
    form.
  • procedure TMainForm.ExitClick(Sender
    TObject)
  • begin
  • close
  • end

24
Pop Up Menus
  • Everything that you can do with a normal menu can
    be done with a Pop Up Menu.
  • Simply add a Pop Up Menu VCL component to your
    form, right click it and your off.
  • Note that the Pop Up Menu has a few more
    properties, such as Animation, Alignment and
    TrackButton.

25
5. Databases
  • Databases boring as sin (but useful).
  • Database programming has its own set of
    buzzwords BDE, client, server, ODBC, alias, SQL,
    query, stored procedure, etc. At its simplest a
    database consists of data stored in one or more
    tables.
  • A table probably contains fields such as
    FirstName, LastName, PhoneNumber, BraSize, etc.
  • These fields are filled with data to create
    individual records in a database file.

26
Database Basics
z
z
z
  • The term database is used to describe an
    all-encompassing data creation and maintenance
    system.
  • A complete client/server SQL database solution
    can also contain numerous queries and stored
    procedures.
  • Therefore, a database (or Database Management
    System DBMS) is more than just a table with
    data.

27
Database Example
28
Cursor
  • The pointer to the current record within a
    database is called the cursor.
  • The cursor points to the record that will be read
    if data is requested and the record that will be
    updated if any edits are made.
  • The cursor is moved when a user browses the
    database, inserts records, deletes records and so
    on.

29
Database Basics - Dataset
  • A collection of data returned by a database is
    called a dataset.
  • A dataset can be the results of a query
    containing data acquired from one or more tables.
  • Although data may come to you from several
    different sources, it is presented to you as a
    single dataset.

30
Local Databases
  • The simplest type of database is the local
    database.
  • A local database is a database that resides on a
    single machine.
  • Generally, the local database is only accessed by
    your program no one else has access to it.
  • Paradox, dBASE and Access databases are usually
    local databases.

31
Client/Server Databases
  • With a client/server database, the database
    itself is stored and maintained on a file server.
  • One or more users (the clients) have access to
    the database across a network of some sort.
  • More than one user may try to access the database
    at the same time. The server knows how to handle
    simultaneous accesses.
  • Users access the database through client
    applications that ensure correct usage.

32
Database Architecture
  • A single-tier database is a database in which
    changes happen immediately (local).
  • In a two-tier database, the client application
    talks to the database server through database
    drivers.
  • In a multitier client/server architecture, the
    client application talks to one or more
    application servers that, in turn, talk to the
    database server.

33
6. Borland Database Engine
  • To enable access to local databases and to
    client/server databases, Delphi provides the
    Borland Database Engine (BDE).
  • The BDE is a collection of DLLs and utilities
    that enables access to a variety of databases.
  • To talk to client/server databases, you must have
    the Client/Server version of Delphi.

34
Borland Database Engine (DBE)
  • The diagram below shows the relationship between
    your application, the BDE and the DB.

35
BDE Drivers
  • Database formats can be very different.
  • Delphi comes with a set of drivers that enable
    your application to talk to several different
    types of databases.
  • These drivers translate high-level database
    commands (such as open or post) into commands
    specific to a particular database type (specifics
    not needed).

36
BDE Aliases
  • A BDE alias is a set of parameters that describes
    a database connection
  • It tells the BDE which type of driver to use and
    the location of the database files on disk and
    maybe the users username.
  • After you create an alias for your database, you
    can use that alias to select the database in your
    Delphi programs.

37
Delphi Database Components
  • With a basic introduction to how databases are
    pieced together, we can now go on to consider the
    various database components, provided by the VCL.
  • The VCL database components fall into two
    categories
  • Non-visual data access components
  • Visual data-aware components

38
Delphi Database Components
  • The non-visual data access components provide the
    mechanism that enables you to get at the data.
  • The visual data-aware components enable you to
    view and edit the data.
  • Again VCL components can be simply drag dropped.

39
Delphi Database Components
  • The data access components are derived from the
    TDataSet class and include TTable, TQuery and
    TStoredProc.
  • The visual data-aware components include TDBEdit,
    TDBListBox, TDBGrid, TDBNavigator and more.
  • The visual components act like TEdit, TListBox
    etc. except that they are linked to a specific
    database table.

40
VCL Database Components
41
The Query Component
  • The Query component provides a way of accessing a
    database table using Structured Query Language
    (SQL).
  • SQL is a powerful way of accessing tables.
  • The Query component is the preferred method of
    accessing data in client/server databases.
  • We will end our discussion on databases by
    describing the primary properties and methods of
    the TQuery class.

42
7. SQL Property
  • The SQL property is a TStringList that contains
    the SQL statements to execute.
  • You can set the SQL propertys value via the
    Object Inspector at design time or through code
    at runtime.
  • Remember the SQL property is a string list, not a
    string. Make sure you clear the previous contents
    before adding lines to it, e.g.
  • Query1.SQL.Clear
  • Query1.SQL.Add(select from country)

43
SQL Commands
  • SQL is a reasonably powerful query language.
  • Refer to the DBS Lectures from last semester for
    a more in depth discussion.
  • The following slides, which are for reference
    only, describe the following fundamental SQL
    commands
  • SELECT
  • INSERT
  • DELETE
  • UPDATE

44
Executing SQL Statements
  • The statements in the SQL query property will be
    executed when either the Open method or the
    ExecSQL method is called.
  • The Open method is used for SQL statements that
    include SELECT.
  • The ExecSQL method is used for SQL statements
    that include INSERT, UPDATE or DELETE.

45
Executing SQL Statements
  • The SQL SELECT statement retrieves certain
    columns from a database, e.g.
  • Query1.SQL.Clear
  • Query1.SQL.Add(select Name, Capital
  • from country)
  • Query1.Open

46
Executing SQL Statements
  • The SQL DELETE statement deletes records from a
    dataset.
  • Query1.SQL.Clear
  • Query1.SQL.Add(delete from country
  • where name Jimland)
  • Query1.ExecSQL

47
Executing SQL Statements
  • The SQL INSERT command inserts a record into a
    dataset.
  • Query1.SQL.Clear
  • Query1.SQL.Add(insert into country)
  • Query1.SQL.Add((Name, Capital))
  • Query1.SQL.Add(values (Jimland,
  • Jimville))
  • Query1.ExecSQL

48
Executing SQL Statements
  • The SQL UPDATE command will update a dataset.
  • Query1.SQL.Clear
  • Query1.SQL.Add(update country)
  • Query1.SQL.Add(set Capital
  • Royburg)
  • Query1.SQL.Add(where Name
  • Royland)
  • Query1.SQL.ExecSQL

49
Executing SQL Statements
  • To make your SQL statements more flexible, you
    can use parameters.
  • A parameter is similar to a variable.
  • Query1.SQL.Add(select from country where Name
    Param1)
  • Query1.SQL.ParamByName(Param1).AsString
    Brazil
  • Query1.Open
  • The value of Param1 in the Params property is
    substituted for the parameter name.

50
Executing SQL Statements
  • Another way to set the parameters value is to
    use the Params array
  • Query1.Params0.AsString Brazil
  • Using the Params array is more error prone to
    using ParamByName as you would have to remember
    the order (the index) of each parameter.

51
Databases in Delphi
  • The previous slides intended to give you a brief
    (so you might stay awake) introduction to
    database access through Delphi.
  • Delphi makes the development of Database Client
    Applications very easy with its various VCL
    components.
  • For more information refer to Part II of the
    Delphi Developers Guide on the course Web site
    chapters 12 to 27.

52
Next Lecture
  • DELPHI MULTIMEDIA PROGRAMMING
  • The coursework is announced, so probably worth
    coming to. I will also answer any questions about
    the coursework then.
  • The Coursework deadline March 21st 5pm.
Write a Comment
User Comments (0)
About PowerShow.com