More Advanced Windows Forms Controls and Coding - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

More Advanced Windows Forms Controls and Coding

Description:

Design workarounds for control arrays (no longer supported in Visual Basic .NET) ... Use the Tag property or TagIndex to identify an individual control or a group of ... – PowerPoint PPT presentation

Number of Views:633
Avg rating:3.0/5.0
Slides: 52
Provided by: campus8
Category:

less

Transcript and Presenter's Notes

Title: More Advanced Windows Forms Controls and Coding


1
More Advanced Windows Forms Controls and Coding
  • Tutorial 6

2
Lesson A
  • Advanced Windows Controls, and Inheritance and
    Polymorphism in OOP

3
Objectives
  • Understand the MonthCalendar, DateTimePicker,
    NumericUpDown, and TrackBar Windows Forms
    controls
  • Discover and apply additional Windows Forms
    controls, including the ProgressBar,
    DomainUpDown, StatusBar, ToolTip, RichTextBox,
    and ContextMenu controls
  • Understand the concepts behind inheritance and
    polymorphism in Visual Basic .NET, including
    abstract classes, sealed classes, and the related
    notions of overloads and shadows
  • Sort tiny arrays (that is, arrays with fewer than
    10 elements)
  • Design workarounds for control arrays (no longer
    supported in Visual Basic .NET)

4
Understanding Advanced Windows Forms Controls
  • Understanding the MonthCalendar Control
  • Useful alternative to the DateTimePicker control
    when you need to display a schedule showing
    multiple dates or months
  • Can only display dates that fall between the
    MinDate and MaxDate properties
  • Scoring application
  • Needs to display a two-month calendar

5
Understanding the Formatting of the
DateTimePicker Control
  • DateTimePicker control
  • Used in the Scoring application to select a time
    of day
  • Format property can be used to set the format of
    the DateTimePicker
  • ShowUpDown property
  • When False (the default), clicking the down arrow
    displays a month calendar
  • When True, the control contains an up-down arrow
    rather than a down arrow
  • Enables you to restrict user to changing only
    those elements of the date/time value that you
    have chosen to display
  • Prevents the month calendar display when that
    would be inappropriate

6
Important Properties of the DateTimePicker control
7
Advanced Windows Forms Controls
  • Understanding the NumericUpDown Control
  • NumericUpDown control
  • Provides a convenient display for a numeric value
    that the user may need to increase or decrease at
    runtime
  • Understanding the TrackBar Control
  • TrackBar control
  • Provides a visual display and functionality
    similar to a HorizontalScrollBar control

8
Properties of the NumericUpDown Control
9
Important Properties of the TrackBar Control
10
Discovering Additional Windows Forms Controls
  • Progress bar
  • Can be used in much the same manner as a scroll
    bar or track bar
  • Traditionally applied to tracking the progress of
    lengthy background computer operations
  • Provides a continually updated indicator that an
    operation is proceeding
  • DomainUpDown control
  • Contains a collection of strings, one of which is
    displayed in the control
  • A different set of properties and methods is
    needed to load and manipulate the strings
  • Sorted property
  • Determines whether the items in the control are
    displayed alphabetically
  • Wrap property
  • Determines what happens when you click after the
    last item or before the first item in the list

11
Important Properties of the ProgressBar Control
12
Important Properties of the DomainUpDown Control
13
Discovering Additional Windows Forms Controls
(Continued)
  • Understanding the StatusBar Control
  • A potentially complex control that is used to
    display the status of an application
  • Can contain any number of StatusBarPanel objects,
    each of which can display text, an image, or
    programmer-drawn controls
  • Understanding the ToolTip Control
  • ToolTips
  • Small pop-up windows that display text when the
    user moves the insertion point over a control
  • Component can be added to any container, such as
    a form, Panel control, ToolBar, or StatusBar
  • Understanding the RichTextBox Control
  • Provides for the display of formatted text using
    Rich Text Format (RTF) style and formatting
    commands
  • Allows formatting to be applied to any portion of
    text selected within the control

14
Properties of the ToolTip Control
15
Discovering Additional Windows Forms Controls
(Continued)
  • Understanding the ContextMenu Component
  • A short pop-up menu that appears when the user
    right-clicks while the cursor hovers over a form
    or control
  • Items normally duplicate those that appear in the
    forms main menu
  • Understanding Add-ins Customizing the Toolbox
  • Possible to build your own controls and add them
    to the Toolbox
  • To see the built-in controls that you can add to
    the Toolbox
  • Right-click the Toolbox and choose Customize
    Toolbox

16
Understanding Inheritance and Polymorphism
  • Inheritance
  • Ability of a derived class or an instantiated
    object to inherit the properties, methods, and
    events of a base class
  • Single inheritance only supported in Visual Basic
    .NET
  • Polymorphism
  • Ability of a derived class or instantiated object
    to provide its own unique implementation of a
    method declared or defined in the base class

17
Understanding Inheritance and Polymorphism
(Continued)
  • Abstract class
  • A class that cannot be instantiated and must be
    inherited
  • Inherited class must implement those portions of
    the abstract class that have been declared, but
    not yet defined
  • Usually contains
  • Elements that are designed to be inherited
    without modification
  • Public or Protected methods that are modified or
    implemented in the inherited class
  • Definition of a derived class begins with the
    Inherits keyword

18
Model for the Definition of a Class Derived from
an Abstract Class
19
Understanding Inheritance and Polymorphism
(Continued)
  • Sealed class
  • Exact opposite of an abstract class
  • Cannot be inherited
  • Declared in Visual Basic .NET with the keyword
    NotInheritable in the declaration header
  • Overloading
  • Overloads keyword declares a method that matches
    the name of an existing member, but contains a
    different argument list
  • Shadowing
  • Related to the notions of overriding and
    overloading a program element
  • Overriding
  • Requires that
  • The overridden element be declared Overridable
  • The overriding element be declared with the
    Overrides keyword

20
Sample of Overloaded Methods
21
Sample of a Shadowed Program Element
22
Sorting a Tiny Array
  • Computers
  • Spend a significant amount of time sorting and
    comparing items in lists
  • Short list
  • Requires sorting only a handful of items
  • Algorithm
  • Computer compares two items at a time, and if
    they are in the wrong order, swaps their
    positions
  • More sophisticated sorting routines
  • Gain their efficiency through strategies for
    comparing the correct two items each time

23
Hildas Simple Sort
24
Understanding Workarounds for Control Arrays
  • Three workarounds for the absence of control
    arrays in Visual Basic .NET
  • Use the Tag property or TagIndex to identify an
    individual control or a group of controls
  • Write a single procedure that handles multiple
    events, often combined with a Select Case
    statement that examines the Name property of
    sender
  • Create an array of strings (or of structures)
    with values that correspond to the Name or Text
    (or perhaps other) property of each control in
    the collection

25
Lesson B
  • Implementing an Outlook-style Application

26
Objectives
  • Convert the Scoring application to an
    Outlook-style interface
  • Incorporate the MonthCalendar, DateTimePicker,
    NumericUpDown, and TrackBar Windows Forms
    controls into the Scheduling pane of the Scoring
    application
  • Modify the layout of Events.txt and structEvent
    to incorporate an EventID as an identifier or key
    for each event, change the scheduled start time
    for each event, and include country codes for
    scoring events involving national teams
  • Provide the functionality to change the starting
    dates for the Friendsville International Games
  • Allow the user to change the date and the start
    time of any specific event

27
Converting the Scoring Application to an
Outlook-style Interface
  • To convert Scoring to a Microsoft Outlook-style
    interface
  • Copy your completed Scoring application from
    Tutorial 5 into the VB.NET\Student\Tut06 folder.
    Rename the solution folder (but not the solution
    file) FinalScoring. Start Visual Basic .NET, and
    open the Scoring solution from the new
    FinalScoring folder
  • Open frmScoreMain in the Windows Form Designer,
    if necessary. Click the ListView1 control, and
    change its Dock property from Fill to Top
  • Click the form, and make the form taller, opening
    a space underneath the ListView control
  • Click the ListView control and then make it
    shorter, dividing the right side of the form into
    two roughly equal halves, with the ListView in
    the top-right pane and an empty pane at the
    bottom right. With the ListView still selected,
    click Edit Cut to remove the ListView control
    and save it on the Windows Clipboard
  • Draw a Panel control in the empty pane on the
    right, and set its Dock property to Fill

28
Converting the Scoring Application to an
Outlook-style Interface (Continued)
  • With the Panel control still selected, click Edit
    Paste to put the ListView control on the form,
    above Panel1
  • With the ListView control still selected,
    double-click the Splitter control in the Toolbox,
    and set its Dock property to Top
  • Draw another Panel control in the empty space
    below the ListView control, and set its Dock
    property to Fill Open the Code Editor for
    frmScoreMain. At the end of the header statement
    for the Sub ListView1_DoubleClick() procedure,
    add the phrase Handles ListView1.DoubleClick
  • Open the Code Editor for frmScoreMain. At the end
    of the header statement for the Sub
    ListView1_DoubleClick() procedure, add the phrase
    Handles ListView1.DoubleClick
  • Run the Scoring application to ensure that
    everything still works as it did at the end of
    Tutorial 5, and to see that you can resize the
    two right panes on frmScoreMain

29
Adding Controls to frmScoreMain
  • Task
  • Add the appropriate controls to the Scheduling
    pane of frmScoreMain
  • MonthCalendar control has two purposes
  • Displays the calendar dates for the Friendsville
    International Games
  • Displays the calendar date for an individual
    event
  • TrackBar control
  • Provides a visual representation of the Event Day

30
Incorporating an Identifier or Key for Each Event
  • To use the revised Events.txt file and make the
    necessary coding changes in Scoring
  • Replace the code in Module1 structEvent with the
    code in Figure 6-36
  • Copy the Events.txt file from the
    VB.NET\Student\Tut06 folder to the
    VB.NET\Student\Tut06\FinalScoring\bin folder
  • The coding that loads the Events.txt file into an
    internal array must also be modified. This occurs
    in the frmScoreMain.vb file in the
    frmScoreMain_Load() procedure. Substitute the
    code shown in Figure 6-37 for the corresponding
    code in the original frmScoreMain_Load()
    procedure. This code adds the statements that
    input the four new elements in Events.txt,
    namely, the EventID, Country1, Country2, and
    StartTime
  • The EventID also needs to appear in the ListView
    control. Therefore, modify the AddListViewItem()
    procedure in frmScoreMain.vb, as shown in Figure
    6-38

31
Revised Layout of Events.txt
32
Loading Events.txt into an Array
33
AddListViewItem() Procedure
34
Providing Functionality to Change the Start Date
for the Games
  • To incorporate the variable StartDate for the
    Games
  • Copy the file StartDate.txt from the
    VB.NET\Student\Tut06 folder to the
    VB.NET\Student\Tut06\FinalScoring\bin folder
  • Add the following public variable to the
    declaration of public variables in Module1 for
    the StartDate Public pStartDate As Date. Insert
    the code in Figure 6-39 into frmScoreMain_Load()
  • Place a bright red Done button on the form on top
    of the upper middle of the MonthCalendar control.
    Name the button btnDone, and open the Properties
    list. Set the Visible property to False
  • Program the mnuToolsChangeStartDate_Click()
    procedure to ask the user whether the user wants
    to change the start date for the Friendsville
    International Games
  • When the user clicks the Done button, the new
    start date is written to the file, and the
    MonthCalendar control is updated with the new
    time frame

35
Loading the Start Date for the Games
36
Displaying and Altering Schedule Data for a
Specific Event
  • To display and alter the event day and time
  • Open the ListView1_Click() procedure and code it,
    as shown in Figure 6-42
  • The TrackBar control should always mirror the
    value of the NumericUpDown control (nudDay). In
    the Code editor, open the ValueChanged() event
    procedure for nudDay, and insert the code shown
    in Figure 6-44
  • To change its scheduled day or time, the user
    selects a list view item and then clicks Tools
    Change Event Date. To achieve this functionality,
    program the mnuToolsChangeEventDate() procedure
    to ask the user if he or she wants to change the
    day or time for this event
  • Clicking the Done button in this case should
    trigger the code shown in Figure 6-45
  • You have now been given two sets of code for the
    btnDone_Click() procedure. Your code must
    determine which code segment to execute based on
    the value of lblEventID, as explained earlier

37
Final Coding Elements
  • To complete the Scoring application
  • Program the About menu selection to display a
    simple About Scoring form, and program the Exit
    menu selection to end the application
  • Whenever a tree view item is selected,
    frmScoreMain_Load() should be invoked again,
    because the same basic actions are called for.
    Therefore, insert the call that follows as the
    first statement in Sub TreeView1_AfterSelect()
    frmScoreMain_Load(sender, e)
  • Run the Scoring application, try every option,
    and clean up any odds and ends that you may have
    overlooked up to this point

38
Lesson C
  • Implementing an Abstract Class

39
Objectives
  • Plan for the remaining tasks in the Scoring
    application
  • Automatically search through and select an item
    from a combo box based on data selected from an
    array
  • Determine when to use Public (Global) variables
  • Build an abstract class and use it in an
    application
  • Create classes that use inheritance and
    polymorphism

40
Planning the Remaining Tasks in the Scoring
Application
  • Application needs the following features
  • For events in team sports, the nationality and
    flag icon (in the ctrlNation controls) should be
    automatically selected
  • Standardized information about athletes needs to
    be stored in a file, and the file needs to be
    loaded into an array at startup
  • A new frmCompetition must be created
  • In all the individual scoring modules
  • User should be able to select competitors from a
    list of eligible athletes
  • ctrlNation control corresponding to each
    competitor should be automatically selected
  • In all the individual scoring modules
  • Results should indicate first, second, and third
    place finishers
  • FigWelcome and Logon class libraries should be
    called

41
Automatically Searching and Selecting from a
Combo Box
  • Internal properties and methods of a user-defined
    control
  • Not programmatically accessible to projects that
    use them
  • ctrlNation
  • Must be modified to search through its list of
    nations and display the correct country name and
    flag
  • Class frmTeam includes ctrlTeamScore
  • Two country codes
  • Must be passed from frmScoreMain to frmTeam, then
    from frmTeam to ctrlTeamScore and then from
    ctrlTeamScore individually to ctrlNation1 and
    ctrlNation2
  • In ctrlNation
  • Create a method that searches through the
    available nation codes
  • If it finds the right one, assign the correct
    country name and flag icon to the constituent
    controls in ctrlNation

42
Revisiting Public (Global) Variables Adding
Athletes to Module1
  • Public variables and constants can present
    problems due to
  • Naming collisions
  • Misinterpretation
  • Scoping problems
  • Difficulties related to the reuse of software
    components
  • Principles
  • Variable needed in only one place should be
    defined in only that one place, with the most
    restricted scope possible
  • When a variable is needed in very few places or
    when it is needed in a class that could be
    reused, it should be passed as a parameter to the
    components that need it
  • Only when a variable is useful and necessary
    throughout an application should it be declared
    as a public variable

43
Building an Abstract Class frmCompetition
  • To create frmCompetition
  • Open frmSpeed in the Windows Form Designer. In
    the Properties window, change its name from
    frmSpeed to frmCompetition. Right-click the name
    frmSpeed.vb in Solution Explorer, select Rename,
    and type the new filename frmCompetition.vb
  • Delete the eight labels (lblName1, lblName2, and
    so on) and replace them with ComboBox controls
    named cboName1, cboName2, ..., cboName8. Set the
    Tag property of each ComboBox to 1, 2, ..., 8,
    respectively, and set the Visible property for
    each to False. Set the Text property for each to
    Select a competitor
  • Make minor modifications to the GUI change the
    Name property of btnEnterTimes to
    btnEnterResults, and change the Buttons Text
    property to Enter Results. Within the Competitors
    group box, the name of the label that serves as a
    column header for Times should be set to
    lblResultsColumnHeader, and its Text property
    should be Results
  • Replace the original btnEnterCompetitors_Click()
    procedure with the procedure shown in Figure 6-55
    for loading the names of potential competitors
    into each combo box
  • When a competitors name is selected, the program
    should automatically select the name and flag of
    the country that competitor represents

44
Building an Abstract Class frmCompetition
(Continued)
  • The logic for entering results has not changed,
    but you just changed the name of the button from
    btnEnterTimes to btnEnterResults, and therefore
    you must also change the name of the procedure to
    btnEnterResults_Click(). Further, your code
    should reflect the generalized nature of the
    class. Generalize the InputBox prompt in the
    btnEnterResults_Click() procedure by entering
    Enter the result for competitor i
  • The previous btnCompetitionOver_Click() procedure
    needs to be replaced completely, because it was
    designed to select only the winner. The new
    procedure determines the order of finish, from
    first to last place. Replace the existing
    btnCompetitionOver_Click() procedure with the
    code in Figure 6-57
  • Insert the AssignWinners() procedure, called from
    frmCompetition into Module1, as shown in Figure
    6-58
  • You should test the program at this point
  • Lastly, some elements of frmCompetition must be
    customized for each type of scoring. To implement
    this idea, you need to convert frmCompetition to
    an abstract class, and you must require each
    derived class to override a procedure that
    provides the requisite settings

45
Using Inheritance and Polymorphism frmSpeed,
frmDistance, and frmJudge
  • Abstract class
  • Graphical interface (if any) of a class derived
    from an abstract class cannot be viewed in the
    Windows Form Designer
  • Reason
  • Designer must instantiate the object to
    materialize the view, and by its nature, an
    abstract class cannot be instantiated
  • To add or manipulate graphical objects in a class
    derived from an abstract class
  • Enter the statements in code
  • Assign any nondefault property settings in code

46
Code for frmSpeed
47
Adding FigWelcome and Logon
  • To add FigWelcome and Logon to the Scoring
    application
  • Add the necessary references to the class
    libraries in the References list for the
    application
  • Use the Imports statement to import each library
    before the class declaration for frmScoreMain
  • To incorporate FigWelcome and Logon into
    frmScoreMain, insert the code shown in Figure
    6-62 at the top of frmScoreMain_Load()
  • Test the completed Scoring application to ensure
    that everything works as intended

48
Incorporating FigWelcome and Logon
49
Summary
  • MonthCalendar control
  • Useful for picking a range of dates or a list of
    separate dates spanning one or more months
  • ProgressBar control
  • Provides a visual measure of progress toward the
    accomplishment of some task, such as downloading
    a file
  • Customizing the toolbox
  • Allows many other Visual Basic .NET controls to
    be available as add-ins or built-ins
  • Abstract classes
  • Must be inherited they cannot be instantiated
  • Simplest sort routine
  • Appropriate for sorting an array of up to 10
    elements
  • Common techniques for manipulating groups of
    related controls
  • Using the Tab or TabIndex property to identify
    each control or group of controls
  • Inserting an event handler that handles multiple
    events
  • Creating a parallel array that corresponds to the
    group of controls

50
Summary
  • Conversion of an existing application from a
    Windows Explorer-style GUI to an Outlook-style
    GUI is relatively straightforward
  • Graphical controls (MonthCalendar,
    DateTimePicker, NumericUpDown, and TrackBar)
  • Can be intertwined to support date and time
    scheduling and manipulation
  • Can interact with the user and with each other to
    provide robust functionality
  • DateTimePicker control
  • Preferred for selecting an individual date or
    time of day

51
Summary
  • Inheritance and polymorphism
  • Can greatly simplify complex coding tasks
  • From an existing control library
  • Possible to modify the source, or
  • You can derive from the source and modify the
    derived class
  • As coding tasks become more complex
  • Scoping rules and encapsulation become more
    important
  • Simple sorting algorithm
  • Also applicable to multidimensional lists,
    provided they are short lists
Write a Comment
User Comments (0)
About PowerShow.com