Class Libraries Project - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Class Libraries Project

Description:

Class Libraries Project Objectives Convert a Windows Forms application into a class library Build a class library, and create a solution with multiple projects ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 29
Provided by: csudhEduE
Category:

less

Transcript and Presenter's Notes

Title: Class Libraries Project


1
Class Libraries Project
2
Objectives
  • Convert a Windows Forms application into a class
    library
  • Build a class library, and create a solution with
    multiple projects
  • Incorporate a component into a class library
    project, and use the component to exchange
    parameter data with a client program
  • Plan for the completion of the LogonManagerFinal
    application, including the MDI form, child forms,
    and calls to two class library projects

3
Converting a Windows Forms Application into a
Class Library
  • To convert FigWelcome from Tut01 into a reusable
    splash screen
  • In Windows Explorer, copy the FigWelcome folder
    from the Tut01 folder and paste it into the Tut04
    folder. Then, in Visual Studio .NET, open the
    FigWelcome solution from the Tut04 folder, and
    open frmWelcome in the Form Designer
  • Drop a Timer control from the Toolbox onto
    frmWelcome (it will appear in a tray beneath the
    form), set its Enabled property to True, set its
    Interval property to 3000 (that is, three
    seconds), and insert the following statement into
    the Timers Tick event procedure Me.Dispose().
    Run the FigWelcome program to see that it
    terminates after three seconds
  • Delete btnClose and the btnClose_Click event
    procedures. Run the FigWelcome program again. In
    Windows Explorer, in the Tut04\FigWelcome\bin
    folder, delete FigWelcome.exe
  • Right-click the project name, FigWelcome, and
    click Properties. If necessary, navigate through
    the FigWelcome Property Pages, to the General tab
    of Common Properties, and change the setting in
    the Output type combo box to Class Library. Click
    OK

4
frmWelcome in the IDE
5
Incorporating FigWelcome into Other Programs
  • To incorporate FigWelcome into other programs
  • Add a reference to the FigWelcome Class Library
  • Insert the Imports FigWelcome statement before
    the class declaration of the startup form or
    module in the client application
  • Insert the Dim declaration and ShowDialog()
    method shown in Step 5 of the previous exercise
    into the Sub Main() procedure, the Sub New()
    procedure (after the InitializeComponent() call),
    or the Form_Load event procedure of the client

6
Building The Logon Class Library
  • How do you pass parameters between the calling
    (client) program and the called (class library)
    program?
  • Case of the LogonManager application
  • The calling program (LogonManager) should call
    the Logon program
  • Calling program should receive an indication that
    the user successfully logged on (or did not)
  • Parameter may be passed to a procedure ByRef or
    ByVal
  • When passed ByRef
  • Address where the variable is stored in the
    calling routine is passed to the called routine
    (function or method)
  • When passed ByVal
  • Value (of the literal, variable, constant, or
    expression) is passed, rather than an address
    where that value is stored

7
Adding a ToolBar Control
  • To add a ToolBar control to a new application
  • Start a new Visual Basic .NET Windows application
    called SampleToolBar in the Tut04 folder. Put a
    MainMenu control on Form1 with three entries
    Exit, Change Message, and Hide Message. Name
    these menu items mnuExit, mnuChange, and mnuHide
    respectively. Drag a large Label control onto the
    form, set its BackColor property to yellow, its
    Text property to nothing, its TextAlign property
    to MiddleCenter, and its Dock property to Top
  • Drag a ToolBar control onto the form. If you
    dont see the ToolBar in the Toolbox, click the
    down arrow next to the General button to scroll
    down the list
  • Click the ToolBars Buttons property, then click
    the Collections setting
  • In the ToolBarButton Collection Editor, click
    Add. ToolBarButton1 is added. In the properties
    listing for ToolBarButton1, change the buttons
    Name property to tbrBtnExit, and change the Text
    property to Exit. Type the following message in
    the ToolTipText property This terminates the
    program

8
Adding a ToolBar Control (Continued)
  1. In the ToolBarButton Collection Editor, click Add
    again. ToolBarButton1 is added. In the properties
    listing for ToolBarButton1, change the buttons
    Name property to tbrBtnChangeLabel, and change
    the Text property to Change Message. Type the
    following message in the ToolTipText property
    Click here to change and show the message
  2. In the ToolBarButton Collection Editor, click
    Add. ToolBarButton1 is added. Change its Name
    property to tbrBtnHideLabel, and change the Text
    property to Hide Message. Type the following
    message in the ToolTipText property Click here
    to hide the message. Click OK
  3. Double-click the ToolBar control, which opens the
    ToolBar1_ButtonClick event procedure in the Code
    Editor. Insert the code in Figure 4-20
  4. Run the SampleToolBar program, and click the
    Change Message button

9
Adding an ImageList Component
  • To add an ImageList to SampleToolBar
  • Copy and paste three images (icons, bitmaps,
    tifs, or gifs) into the SampleToolBar solutions
    folder (VB.NET\Student\Tut04\SampleToolBar)
  • Drag an ImageList component from the Toolbox onto
    Form1
  • Click ImageList1s Images property, and then
    click Collections in the settings box
  • In the Image Collection Editor, click Add. In the
    Open dialog box, navigate to the SampleToolBar
    folder, and add one of the image files you have
    copied there
  • Click ToolBar1. Set its ImageList property to
    ImageList1. Now click ToolBar1s Buttons
    Collection property. In the ToolBarButton
    Collection Editor, click tbrBtnExit. Under
    tbrBtnExit properties, click ImageIndex. Select
    one of the three images (0, 1, or 2)
  • Run the SampleToolBar program to ensure that the
    images appear properly on the toolbar buttons at
    runtime

10
Adding an OpenFileDialog Control
  • OpenFileDialog control
  • Purpose is to designate a file to be opened for
    input
  • Used by the mnuFileRestore_Click() procedure
  • Has almost all the same properties, appearance,
    and behavior as the SaveFileDialog control

11
Setting up and Planning the LogonManagerFinal
Application
  • To set up the LogonManagerFinal application
  • Create a new folder called VB.NET\Student\Tut04\Lo
    gonManagerFinal
  • Into this new folder, copy the contents of either
    the VB.NET\Student\Tut03\LogonManagerDraft folder
    or the completed LogonManagerDraft application
    folder you obtain from your instructor
  • Click File Open Solution, and open the
    LogonManagerDraft.sln (solution) file. In
    Solution Explorer, click Solution
    LogonManagerDraft (1 project). In the Solution
    Properties window, click the solution name, and
    change it to LogonManager. In Solution Explorer,
    click the project name, LogonManagerDraft. Then
    in Project Properties, change the name of the
    project to LogonManager
  • Run the LogonManager project from the IDE to make
    sure that it still works correctly
  • In Windows Explorer, navigate to the
    LogonManagerFinal folder. Verify that the name of
    the solution file has been changed to
    LogonManager.sln, and the name of the project is
    now LogonManager.vbproj

12
Identifying Tasks to Complete LogonManager
  • Complete the mdiMain GUI
  • Add ToolBar and ImageList controls
  • Enhance applications GUI
  • Write new menu click event procedures for menu
    items that were under construction at end of
    Tutorial 3
  • Incorporate calls to the Logon application in
  • mnuViewExistingUser_Click()
  • mnuViewSupervisorLogon_Click()
  • Display the splash screen (FigWelcome) at startup

13
Completing the mdiMain GUI
  • To add an image list and toolbar to mdiMain
  • Add an ImageList control to mdiMain. Add any
    three icons (.ico files) to its Images Collection
  • Add a ToolBar control to mdiMain. Set its
    ImageList property to ImageList1. In the Buttons
    property, click Collection. In the ToolBarButton
    Collection Editor, add three ToolBarButtons, and
    set the appropriate Text and ImageIndex property
    for each button, as suggested by Figure 4-26. Set
    the ToolTipText property to Fill out the
    application form to become a user, Update/verify
    your user information and change your password,
    and Logon as the system supervisor, respectively
  • To open the ToolBar1_ButtonClick event procedure,
    in mdiMains Code window, select ToolBar1 from
    the Class Name combo box at the top left. Then
    select ButtonClick from the Method Name combo box
    at the top right
  • Recall that you can use a Select Case control
    structure to differentiate among ToolBarButton
    controls. Add the code shown in Figure 4-27
  • Run the LogonManagerFinal application to see that
    it still works as it did at the end of Tutorial 3
    and to verify that the new toolbar buttons work
    as advertised

14
Implementing a RadioCheck
  • To implement the RadioCheck in front of the
    Accept New Users menu item
  • Insert the mnuViewSupervisor_Select() procedure
    shown in Figure 4-28
  • Code uses a local Boolean variable,
    blnNewUserPending, initialized to False, to
    indicate the presence of at least one new user
    record
  • When an N record is encountered
  • Boolean variable is set to True

15
ToolBar1_ButtonClick() Event Procedure
16
Programming Procedures on the File Menu
  • To create and test the backup and restore
    procedures
  • Begin by dragging a SaveFileDialog control and an
    OpenFileDialog control onto mdiMain
  • Insert the code shown in Figure 4-29 into
    mnuFileBackup_Click()
  • Program the mnuFileRestore_Click() procedure, as
    shown in Figure 4-30
  • Run the LogonManagerFinal application. Log on as
    the system supervisor (recall that the
    supervisors e-mail address is 1 and the password
    is SUPERVISOR). Then click File Backup, and
    save the Users file in the Temp folder as
    Users.bak. Then click File Restore, and restore
    the Users file you have just backed up. Exit the
    program and return to the IDE

17
Code for mnuFileBackup_Click()
18
Code for mnuFileRestore()_Click
19
Programming Procedures on the View Menu
  • To (partially) program the remaining stubs in the
    View menu
  • Open the mnuViewExistingUser_Click() procedure.
    Insert the code shown in Figure 4-31
  • Open mnuViewSupervisorEditUserInfo_Click(). Type
    the code shown in Figure 4-32
  • Run the LogonManagerFinal program to verify that
    the procedures you have just created work
    correctly. Exit the program and return to the IDE
  • Add a new form named frmViewAll to the
    LogonManager project. Set its WindowState
    property to Maximized. Add a ListBox control, and
    set its Dock property to Fill
  • Insert the code shown in Figure 4-33 into the
    frmViewAll_Load() procedure

20
Programming Procedures on the View Menu
(Continued)
  • In mdiMain, in mnuViewSupervisorViewAllUsers_Click
    (), insert code shown in Figure 4-35 to maximize
    mdiMain and show the child form
  • Run the LogonManagerFinal program, logon as the
    supervisor, and view the child form. Exit the
    program and return to the IDE
  • Type the code into the mnuViewSupervisorResetExpir
    ationDates_Click() event procedure shown in
    Figure 4-36
  • Run the LogonManagerFinal application again. Log
    on as the supervisor, and click View Supervisor
    View All Users. Note the password expiration
    dates for each active user. Now click View
    Supervisor Reset Expiration Dates. Then click
    View Supervisor View All Users again, and
    note the revised password expiration dates for
    all active users except the supervisor. Exit the
    application and return to the IDE

21
Disabling Controls for Multiple Users and Purposes
  • To disable user-editable controls in frmUser when
    the Accept New Users menu item has been selected
  • In frmUser, insert a new procedure called
    DisableControls(). In this procedure, disable all
    of the user-editable controls on frmUser
    (TextBoxes, ComboBox, CheckBox, and RadioButtons)
  • In mdiMain, in the mnuViewSupervisorAcceptNewUsers
    _Click() procedure, insert the following
    statement before showing the child form
    frmUserTemp.DisableControls()
  • Run the LogonManagerFinal program. Create a new
    user named Dublin Ireland, with an e-mail address
    of Dublin_at_fgdt.org, and the password dublinfgdt.
    His secret question asks for his mothers maiden
    name, and the answer is Casey. Submit the New
    User
  • Click View Supervisor. A RadioCheck appears in
    front of Accept New Users. Log on as the
    supervisor. Click Accept New Users. Dublin
    Irelands record appears. Verify that all the
    user-editable controls have been disabled. Click
    Accept. Click View Supervisor. The RadioCheck
    no longer appears in front of Accept New Users
  • Exit the application and return to the IDE

22
Adding a Call to the Logon Application
  • To insert the Logon application into LogonManager
  • In Solution Explorer, add a reference to the
    Logon.dll in the LogonManagers References by
    right-clicking References in Solution Explorer,
    then clicking Add Reference. In the Add Reference
    dialog box, click the Projects tab. Browse to
    find the Logon.dll (in VB.NET\Student\Tut04\Logon\
    bin), and click OK
  • In the Code window for mdiMain, insert the
    following statement before the Public Class
    mdiMain statement Imports Logon
  • Replace the stub code with a call to the Logon
    application in the mnuViewSupervisorLogon_Click()
    procedure as shown in Figure 4-37
  • Navigate to mnuViewExistingUser_Click(), and add
    the code shown in Figure 4-38 at the beginning of
    the procedure, replacing the InputBox function
    call. In this case, the user need not be a
    supervisor
  • Run the program, checking that the two calls to
    the Logon application work as advertised. Exit
    the program and return to the IDE

23
Code for mnuViewSupervisorLogon_Click()
24
Calling Logon from mnuViewExistingUser_Click()
25
Adding the Call to the FigWelcome Splash Screen
  • To insert the FigWelcome splash screen into
    LogonManager
  • In Solution Explorer, add a reference to the
    FigWelcome.dll in the LogonManagers References
  • In the Code window for mdiMain, insert the
    following statement before the Public Class
    mdiMain declaration Imports FigWelcome
  • Navigate to the Windows Form Designer generated
    code section, in Sub New(), and insert the
    following after the InitializeComponent() method
    call
  • Dim MyWelcome As New frmWelcome()
  • MyWelcome.ShowDialog()
  • Run the LogonManager program

26
Understanding the Power of a Class Library
  • To implement a change to Logon
  • Open the Logon solution, open frmLogon in the
    Form Designer, and set the BackgroundImage
    property to the bitmap file Gone Fishing.bmp.
    Then run the TryLogon project, which calls Logon
    and causes it to compile, updating its DLL
  • Run LogonManager.exe from the Start menu or from
    Windows Explorer. Click View Existing User to
    invoke Logon
  • Open LogonManager in the IDE, if necessary, and
    run it from within the IDE. This calls the new
    DLL for the class library. When Logon is invoked,
    it appears with the new background. Exit the
    application, and exit Visual Studio .NET
  • Now run LogonManager once more from the Start
    menu or from Windows Explorer

27
Summary
  • ToolBar control
  • Often incorporated into a form
  • ImageList control
  • Contains a collection of images
  • OpenFileDialog control
  • Allows the user to specify a file to be opened
    for input

28
Summary
  • Class library application
  • Compiles to a DLL (dynamic link library) file
  • To call a class from another application
  • Add a reference to the classs DLL in the calling
    application
  • To make the members of a class available without
    qualification
  • Use the Imports statement
  • Component
  • Can be defined in a class library
  • Can contain procedures with parameters passed
    from a calling routine
  • Efficiency of OOP
  • Becomes obvious during program maintenance
Write a Comment
User Comments (0)
About PowerShow.com