Programming Seminar II Labs and Projects

PROGRAMMING SEMINAR II

CS450 Lab 1 - Getting Started

Objectives: 1) Review Delphi's Environment
	2) Use Forms and Units
	3) Use simple Components: EditBox, BitButtons, RadioButtons
	4) Build a simple Delphi program that uses multiple forms

Part 1: Build a simple program which allows the user to select the name and type of border should be displayed in a form.

a) Start with a new project and name the form MainForm.
b) Place a Tbutton on the form and enter the event handler shown in Listing 5.1 (pg. 87).
c) Add a new form and name it OptionsForm.
d) On the new form, place four radio buttons (Single Border, Dialog, No Border and Sizeable) in a Group Box named Form Style.
e) Add an empty Tedit component and a Label (titled Form Caption).
f) Add two TbitBtns and change their kind properties to bkOK and bkCancel.
g) Save your project (and unit) as Plab1.dpr and Ulab1.pas
h) Run the project and trying select a different border type and renaming the form.

Part 2: Add whatever is necessary to the OptionsForm that will allow the user to select which Border Icons should be available on the MainForm.  Remember that you may have none, one or more of the icons available.

Part 3: Add whatever is necessary to the OptionsForm that will allow the user to select which cursor should be available on the MainForm. The user should be able to select one of the 18 pre-defined cursor types as well as the "crosshair" cursor found on the book's CD.

BONUS: Create your own cursor and it to the list in part 3.

Documenting your unit by inserting comments with your name, the date and a brief description at the top of the unit.

Turn in to me a disk with all of the necessary files.  Make sure your project name is indicated on the disk.

CS450 Lab 2 - MDI Applications due: Feb. 5, 1996

Objectives: 1) Further Explore Delphi's Environment 2) Creating MDI(Multiple Document Interface) Applications 3) Working with Child Windows 4) Adding Dialogs to MDI Applications Part 1: Create a MDI Application with two child windows and menus. a) Start with a new project. b) Create a form with the following properties and save the unit as FRAME.PAS: ---------------------------------------------------------------------------------------- Property Value ---------------------------------------------------------------------------------------- Name ‘FrameForm' Caption ‘MDI Application' FormStyle fsMDIForm ---------------------------------------------------------------------------------------- c) Create a new form with the following properties and save the unit as MDITEXT.PAS: ---------------------------------------------------------------------------------------- Control Property Value ---------------------------------------------------------------------------------------- EditForm Name ‘EditForm' EditForm FormStyle fsMDIChild Memo1 Align alClient Memo1 BorderStyle bsNone Memo1 Cursor crBeam Memo1 Scrollbars ssBoth Memo1 Text ‘' ---------------------------------------------------------------------------------------- remove the declaration var Editform : TeditForm; d) Create a new form with the following properties and save the unit as MDIBMP.PAS: ---------------------------------------------------------------------------------------- Control Property Value ---------------------------------------------------------------------------------------- BMPForm Name ‘BMPForm' BMPForm Caption ‘' BMPForm FormStyle fsMDIChild Image1 Align alLeft Image1 AutoSize true ---------------------------------------------------------------------------------------- e) Use Options | Project to remove EditForm and BMPForm from Auto-Create and select FrameForm as the main form. f) Add a TmainMenu component to FrameForm and set up the menu as shown in Table 8.4 and 8.5. Change the GroupIndex property for the Windows menu bar to 9. g) Add a TmainMenu component to EditForm and add to the File submenu as shown in Table 8.6. Change the GroupIndex property for the Edit and Character menu bars to 1. Set the AutoMerge property to true. Add a Color option below the Font option. h) Add a TmainMenu component to BMPForm and add to the File submenu as shown in Table 8.7. Remove Save, Save As and a Separator from the file menu. Set the AutoMerge property to true. Part 2: Working with Child Windows a) Create the event handler for File|New as shown in Listing 8.1. Add the public method OpenTextFile as shown in Listing 8.1. b) Place the two child units in MDIFrame's uses clause. c) Create the method: procedure TEditForm.OpenFile(FileName : string); begin Memo1.Lines.LoadFromFile(FileName); Caption := FileName end; and the event handler for File|New: procedure TEditForm.New1Click(Sender: TObject); begin FrameForm.OpenTextFile(''); end; Add MDIFRAME to the MDIEDIT's uses clause in the implementation section. d) Create an OnClose event handler for BMPForm: procedure TBMPForm.FormClose(Sender: TObject; var Action: TCloseAction); begin Action := caFree end; and event handlers for File|Close for each of the three forms. Connect the File|Exit event handler for the frame form with the File|Close menu and the set TeditForm's and TBMPForm's File|Exit event handlers to call FrameForm's OnClose event handler. Create an Windows | Close All event handler for FrameForm: procedure TFrameForm.CloseAll1Click(Sender: TObject); var i : integer; begin for i := 0 to MDIChildCount - 1 do MDIChildren[0].Close end; e) Add event handlers for Tile, Cascade and ArrangeIcons (see Listing 8.6) Choose Window for the WindowMenu property for FrameForm f) Use the event handler in Listing 8.7 for Character | Left, Character | Right and Character | Center. (Note: type method once and link the others) Add an event handler in Listing 8.8 for Character | WordWrap. Part 3: Adding Dialogs to the MDI Applications a) Add an Open Dialog to FrameForm with the properties ---------------------------------------------------------------------------------------- Property Value ---------------------------------------------------------------------------------------- Filter "Text File | *.txt | Document File | *.doc | Bitmap file | *.bmp" DefaultExt "txt" ---------------------------------------------------------------------------------------- b) Add an event handlers for File | Open as shown in Listing 8.9 and 8.10 Declare constants BMPExt = ‘.BMP'; and TextExt = ‘.TXT'; in MDIFrame. c) Add methods procedure TFrameForm.OpenBMPFile(FileName : string); begin with TBmpForm.Create(self) do OpenFile(FileName); end; procedure TBMPForm.OpenFile(FileName : String); begin Image1.Picture.LoadfromFile(FileName); Caption := FileName; Clientwidth := Image1.Picture.Width; VertScrollBar.Range := Image1.Picture.Height; HorzScrollBar.Range := Image1.Picture.Width end; d) Add a SaveDialog to FrameForm with the properties ---------------------------------------------------------------------------------------- Property Value ---------------------------------------------------------------------------------------- Filter "Text File | *.txt | Document File | *.doc | Bitmap file | *.bmp" Options [ofHideReadOnly,ofNoReadOnlyReturn,ofOverWritePrompt] DefaultExt "txt" ---------------------------------------------------------------------------------------- e) Add event handlers for File|Save and File|Save As procedure TEditForm.SaveAs1Click(Sender: TObject); begin SaveDialog1.FileName := Caption; if SaveDialog1.Execute then Caption := SaveDialog1.FileName; Save1Click(Sender); end; procedure TEditForm.Save1Click(Sender: TObject); begin if Caption = '' then SaveAs1Click(Sender) else begin Memo1.Lines.SaveToFile(Caption); Memo1.Modified := false end end; f) Add the OnCloseQuery event handler as shown in Listing 8.11. g) Add a FontDialog and a ColorDialog and their associated event handlers. h) Add a PrintDialog and event handler (see Table 8.10 and Listing 8.12). i) Add a PrinterSetupDialog and event handler. j) Add the File|Print event handler shown in Listing 8.13. k) Add the EditForm event handlers shown in Listing 8.14. l) Add the BMPForm event handlers shown in Listing 8.15 Documenting your units by inserting comments with your name, the date and a brief description at the top of the units. Turn in to me a disk with your .PAS, .DFM and .DPR files. QUESTIONS: 1) What are dialogs? How are they used? 2) What does the try - finally in Listing 8.12 do? The AssignPrn and Rewrite statements are inside the try-finally in Listing 8.12 but outside in Listing 8.4. Does it make a difference? 3) When you open a .txt or .bmp file, the Windows submenu appears even though it was not included when designing the EditForm and BMPForm. Explain how this happens. 4) Compare the two Print event handlers for EditForm and BMPForm.

CS450 Lab 3 - GDI and Graphics Programming

due: Feb. 12, 1996 Objectives: 1) Further Explore Delphi's Environment 2) Creating Application using GDI and Graphics Programming 3) Print the resultant application using TPrinter Part 1: Drawing Lines a) Start with a new project. b) Create a form with the following properties and save the unit as CANVAS.PAS: ---------------------------------------------------------------------------------------- Property Value ---------------------------------------------------------------------------------------- Caption ‘Paint Canvas' Color clWhite ---------------------------------------------------------------------------------------- c) Add components to the Form that allows the user to select a Pen Color. d) Add components to the Form that allows the user to select a Pen Style. e) Add components to the Form that allows the user to select the Pen Width. f) Add components to the Form that allows the user to select a starting point and ending point for drawing a line. g) Add a PaintBox for drawing in. (Make sure that the PaintBox is sufficiently large) h) Add a button that allows the user to draw the selected line in the PaintBox. h) Add an Exit button. Part 2: "Brush Painting" a) Add components to the Form that allows the user to select a Brush Color. b) Add components to the Form that allows the user to select a Brush Style. c) Add a component to the Form that allows the user to select whether to fill in the object to be drawn. d) Add components to the Form that allows the user to select the figure (Arc, Chord, Ellipse, Pie, Polygon, Polyline, Rectangle, and RoundRectangle) to be drawn on the PaintBox. [Note each figure is going to require different parameters to be selected, i.e. if Rectangle is selected, you will have to ask the user for four values.] Part 3: Printing your BitMap a) Include a TPrinter component that will allow the contents of the PaintBox to be printed. b) Be sure to include an AbortForm (see page 307). Documenting your units by inserting comments with your name, the date and a brief description at the top of the units. Turn in to me a disk with your .PAS, .DFM and .DPR files as well as a sample of your artwork.

CS450 Lab 4 - Writing Delphi Custom Components due: Feb. 19, 1996

Objectives: 1) Explore Writing Delphi Components Part 1: Extending the TListBox Component a) Start with a new component. b) Use TTabListbox for the name of the component, TListbox for the ancestor type, and PS2 for the Palette page and save the file as LBTAB.PAS. c) Remove Graphics,Forms, Dialogs from the uses clause to improve efficiency. d) Declare the following private variables: FLongestString : Word; FNumTabStops : Word; FTabStops : PWord; FSizeAfterDel : Boolean; e) Include the uses PixDlg; clause in the implementation section and copy the uses PixDlg.pas file from the CD to your disk. f) Enter the constructor. Make sure to include override in the public section. constructor TTabListBox.Create(AOwner : TComponent); begin Inherited Create(AOwner); FSizeAfterDel := TRUE; {set tab stops to Windows defaults...} FNumTabStops := 1; GetMem(FTabStops, SizeOf(Word) * FNumTabStops); FTabStops^ := DialogUnitsToPixelsX(32) end; Include the following property in the published section: property SizeAfterDel: Boolean read FSizeAfterDel write FSizeAfterDel default True; g) Include the protected method: [Make sure to override the earlier version] procedure TTabListBox.CreateParams(var Params : TCreateParams); {OR in the styles necessary for tabs and horizontal scrolling} begin inherited CreateParams(Params); Params.Style := Params.Style or lbs_UseTabStops or ws_HScroll; end; h) Include the public method: procedure TTabListBox.SetTabStops(A : array of word); var pos : word; TempTab : word; TempBuf : PWord; begin TempTab := High(A) + 1; GetMem(TempBuf, SizeOf(A)); Move(A, TempBuf^, SizeOf(A)); for pos := 0 to TempTab - 1 do A[pos] := PixelsToDialogUnitsX(A[pos]); if Perform(lb_SetTabStops, TempTab, Longint(@A)) = 0 then begin FreeMem(TempBuf, SizeOf(Word) * TempTab); raise ETabListboxError.Create('Failed to set tabs.') end else begin FreeMem(FTabStops, SizeOf(Word) * FNumTabStops); FNumTabStops := TempTab; FTabStops := TempBuf; FindLongestString; Invalidate end; end; Declare the type ETabListboxError = class(Exception); and include the private methods: function GetLBStringLength(P : PChar); word; procedure FindLongestString; procedure SetScrollLength(P : PChar); procedure LBAddString(var Msg : TMessage); message lb_AddString; procedure LBInsertString(var Msg : TMessage); message lb_InsertString; procedure LBDeleteString(var Msg : TMessage); message lb_DeleteString; i) Write a small project to test your TabListBox. (There is an example on your disk). You will need to include your unit in the interface part and declare a variable of type TTabListBox. Your test project should include buttons for adding, deleting, inserting items in your TabListBox and a button for sorting the contents. Part 2: Creating a Marquee a) Start with a new component. b) Use TMarquee for the name of the component, TCustomControl for the ancestor type, and Samples for the Palette page and save the file as MARQUEE.PAS. c) Setup the mechanism for rendering text onto the memory canvas: i) write the SetLineHeight private method (pp. 275-6). ii) write the MakeRect private method (pg. 276) and SetItems private method iii) define private properties: MemBitmap, LineHi, VRect, FItems and FJust and the enumerated type TJustification. iv) write the PaintLine private method (pg. 277) and define the EMarqueeError type. d) Setup the mechanism that copies text from the memory canvas to the marquee window. i) write the Paint protected method (pg. 277) and the private properties InsideRect, CurrLine and FActive. ii) write the private method SetStartLine. e) Setup the timer that keeps track of when and how to scroll the window. i) write the procedure DoTimer and the private property FTimer. ii) write the methods FTimerOnTimer and IncLine and the private property FScrollDown. iii) Add the constants ScrollPixels and TimerInterval. f) Setup the constructors and destructors i) Write the class constructor Create and destructor Destroy ii) Write the methods Activate, FillBitMap, and Deactivate as well as the property FOnDone and the public property Active. g) Finishing touches:Publish the inherited properties from TCustomPanel (pg. 282). h) Write a small project to test your Marquee (There is an example on your disk). Include a button to add names / words to the Marquee list. Have the "Marquee" beep whenever it is activated.

  

CS450 Project 1 - Writing Delphi Custom Components due: Feb. 28, 1996

Objectives: Writing a Delphi Component Your programming team is expected to select one of the following components and create it. You will be expected to do the following: 1. Create a unit for the new component. 2. Derive the component type from an existing component type. 3. Add properties, methods, and events as needed. 4. Register your component with Delphi. 5. Create a Help file for your component and its properties, methods, and events. 6. After thorough testing, place your component in the PS2 Palette (this will include designing an icon for your component) There should also be a Hint for your component. Your completed component will include the following five files: 1. The source code unit (.PAS file) 2. The compiled unit (.DCU file) 3. A palette bitmap (.DCR file) 4. A Help file (.HLP file) 5. A Help-keyword file (.KWF file) 6. Any projects that you used for testing your component You may select one of the following components or design one of your own (must have my approval by Feb. 14th): A) A Color and Sizeable 3D button. B) 3-D Multi-colored Labels (must allow Flat, Raised, Recessed, and Shadow) C) An edit window: This would include a status bar at the bottom of the window which would consist of minimally the time, date, file name, line number and position.

CS450 Lab 5 - Sharing Information with the Clipboard and DDE due: Feb. 26, 1996

Objectives: 1) Learning how to move data between applications Part 1: Creating Your Own Clipboard Format 1) Create the following form: 2) Create the following event-handlers: procedure TForm1.CopyButtonClick(Sender: TObject); var DDGObj : TDDGObject; begin DDGObj := TDDGObject.Create; with DDGObj do begin Rec.FName := Edit1.Text; Rec.MI := Edit2.Text; Rec.LName := Edit3.Text; CopyToClipBoard end; DDGObj.Free end; procedure TForm1.PasteButtonClick(Sender: TObject); var DDGObj : TDDGObject; begin DDGObj := TDDGObject.Create; if ClipBoard.HasFormat(CF_DDGDATA) then with DDGObj do begin GetFromClipBoard; Edit1.Text := Rec.FName; Edit2.Text := Rec.MI; Edit3.Text := Rec.LName; end; DDGObj.Free end; 3) Add clipbrd to the Uses clause and define the variable CF_DDGDATA : word; 4) Create the types: PDDGRec = ^TDDGRec; TDDGRec = record LName : string; FName : string; MI : string; end; TDDGObject = class(TObject) private Rec : TDDGRec; protected procedure CopyToClipBoard; procedure GetFromClipBoard; end; 5) Create the two TDDGObject methods as shown on pages 354-5. 6) Register the clipboardformat in the initialization section: CF_DDGDATA := RegisterClipBoardFormat(‘CF_DDG'); 7) Run your program. If there are problems, view the clipboard. Part 2: Interfacing the ClipBoard with another object. 1) Add a StringGrid and EditBox to your form. 2) Replace the PasteButton event handler with one that pastes the contents of the clipboard into the stringgrid in the row designated by the contents of the EditBox.

STUDY GUIDE - SPRING 1996


Midterm Exam - Mon. Mar. 4 6:00 p.m.
Format: 1 program
Chapters covered: 1-15

You should know how to use the following components: TForm, TScreen, TApplication, TmainMenu, Tbutton, Tlabel, Tpanel, Tspeedbutton, Tstrings, Tcanvas, Tprinter, Dialogs, Timage, TDDEServer, TDDEClient, ToleContainer, TBitButton

  1. You should know how to create MDI Applications, GDI Applications, Graphic Programming
  2. You should understand messages
  3. You should understand the steps in writing components
  4. You understand how to share information using the Clipboard
  5. You should be able to write simple applications using OLE

Lab 7 - Building Database Applications due: Mar. 25, 1996

Objectives: 1) Learning how to build database applications

Part 1: Creating A Simple Database Application

Part 2: Using the Database Form Expert

Part 3: Adding a Calculated Field

Part 4: Formatting your Display

Add any buttons or labels that are necessary to improve the appearance and use of your program.

Lab 8 - Working with SQL and the TQuery Component

due: Apr. 10, 1996

Objectives: 1) Learning how to write SQL-based applications

Part 1: Creating the Main Database Form

  • 1) Create the following form (Use the EMPLOYEE database in DBDEMOS) and add three additional TQuery's.
    See diagram on handout

    Part 2: Setting up the SQL properties

  • 2) Enter the following SQL statement for the DeleteQuery's SQL property:
    DELETE FROM EMPLOYEE WHERE EMPNO = :EMPNO
  • 3) Enter the following SQL statement for the InsertQuery's SQL property:
    INSERT INTO EMPLOYEE (EMPNO, LASTNAME, FIRSTNAME, PHONEEXT, HIREDATE, SALARY)
    	VALUES (:EMPNO, :LASTNAME, :FIRSTNAME, :PHONEEXT, :HIREDATE, :SALARY)
  • 4) Enter the following SQL statement for the UpdateQuery's SQL property:
    	UPDATE EMPLOYEE
    		SET EMPNO = :EMPNO,
    			LASTNAME = :LASTNAME,
    			FIRSTNAME = :FIRSTNAME,
    			PHONEEXT = :PHONE,
    			HIREDATE = :HIREDATE,
    			SALARY = :SALARY
    		WHERE EMPNO = :EMPLOYEE_NBR

    Part 3: Adding the buttons and events

  • 5) Add buttons and the following events for inserting, deleting, updating and getting a record. Make sure to include the method ClearFields (see book).
    procedure TDatabase.InsRecordClick(Sender: TObject);
    begin
         Screen.Cursor := crHourGlass;
         try
            with InsertQuery do begin
              try
                 Close;
                 Prepare;
                 Params[0].AsInteger := StrToInt(EditEmpNo.Text);
                 Params[1].AsString := EditLastName.Text;
                 Params[2].AsString := EditFirstName.Text;
                 Params[3].AsString := EditPhoneExt.Text;
                 Params[4].AsDate := StrToDate(EditHireDate.Text);
                 Params[5].AsFloat := StrToFloat(EditSalary.Text);
                 ExecSQL;
                 GridQuery.Close;
                 GridQuery.Open;
              except
                  on E: EDataBaseError do
                     MessageDlg(E.Message, mtInformation, [mbOK], 0);
              end;
            end;
            ClearFields;
         finally
                Screen.Cursor := crDefault;
                ClearFields
         end
    end;
    procedure TDatabase.DelRecordClick(Sender: TObject);
    var
       EmployeeNumber : integer;
    begin
       EmployeeNumber := GridQuery.Fields[0].AsInteger;
       if MessageDlg(Format('Delete "%d"?', [EmployeeNumber]), mtConfirmation,
          mbOKCancel, 0) = mrOK then begin
          Screen.Cursor := crHourGlass;
          try
             with DeleteQuery do begin
                  Prepare;
                  Params[0].AsInteger := EmployeeNumber;
                  ExecSQL
             end;
             GridQuery.Close;
             GridQuery.Open
          finally
             Screen.Cursor := crDefault
          end
       end
    end;
    procedure TDatabase.GetRecordClick(Sender: TObject);
    var
       Count : integer;
    begin
       for count := 0 to ControlCount - 1 do begin
           if Controls[count] is TEdit then
              with TEdit(Controls[count]) do
                   Text := GridQuery.Fields[Tag].AsString
       end;
       UpdRecord.Enabled := true
    end;
    procedure TDatabase.UpdRecordClick(Sender: TObject);
    var
       count : integer;
    begin
       Screen.Cursor := crHourGlass;
       try
          UpdateQuery.Prepare;
           for count := 0 to ControlCount - 1 do begin
               if Controls[count] is TEdit then
                  with TEdit(Controls[count]) do
                       UpdateQuery.Params[Tag].AsString := Text;
           end;
           UpdateQuery.Params[0].AsInteger := StrToInt(EditEmpNo.Text);
           UpdateQuery.ExecSQL;
           GridQuery.Close;
           GridQuery.Open;
           UpdRecord.Enabled := false
       finally
           Screen.Cursor := crDefault
       end
    end;

    Part 4: Answer the following questions:


    Lab 9 - Understanding the Address Book Application

    due: Apr. 17, 1996

    Objectives: Understanding some of the features used in the Address Book Application


    Project 2 - Building Database Applications

    due: Apr. 1, 1996

    Objectives: Build a database application to

    Part 1: Looking for records in a Database

    Part 2: Changing the Data and Computing New Values

    Part 3: Formatting your Display

    Add any buttons or labels that are necessary to improve the appearance and use of your program.


    Final Project - Building Database Applications

    presentations due: Apr. 29, 1996

    Objective: As a class, develop an on-line registration system for NMHU to be used by students, faculty and the registar.

    Narrative: Your class has been assigned to develop an on-line registration system for NMHU. Your first task will be to assign duties. The project can be divided into four subtasks. The project leader will be required to develop the introductory interface which will tie into the other three parts. The project leader will also be in charge of assigning duties among the three groups and making sure that the other three parts interface nicely. The project leader will also be responsible for overseeing the databases and will need to contact me whenever there is a problem with the databases. A copy of the databases are located on my share drive in \Delphi\OnLine. [I would suggest you make a working copy of all files to the computer you are using and label the path with an alias]. The structure of the databases is shown below. Make sure all parts of your application are user-friendly and they include About boxes and Help screens.

    Part 1: One of the three 2-programmer teams will be responsible for handling the student registration form(s). Students will use this part to

    Students may also use this part to change their personal data, i.e. address, phone #, etc. All students have a PIN# which protects the integrity of the database. This PIN# can only be changed by the student.
  • Reports available: Student schedule

    Part 2: The second 2-programmer team will be responsible for developing the interface for the registrar. The registrar will want to be able to

    The registrar interface will be password protected.

    Reports available

    :

    Part 3: The third team will be responsible for developing the faculty interface.

    Faculty will want to view

    The faculty interface will be password protected for the individual faculty member.
  • Reports available: all of the lists above

    Database Tables

    See handout for format of tables

    1. Student.DB
    2. Courses.DB
    3. Stud-cou.DB
    4. Faculty.DB