VB'Net Multiple Form Apps - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

VB'Net Multiple Form Apps

Description:

Form1, what is created in the Form Designer, is actually defining a class. ... A modal form takes the application's focus until it is ... Behind the Scenes ... – PowerPoint PPT presentation

Number of Views:520
Avg rating:3.0/5.0
Slides: 23
Provided by: cisUso
Category:
Tags: apps | form | multiple | net

less

Transcript and Presenter's Notes

Title: VB'Net Multiple Form Apps


1
VB.Net Multiple Form Apps
  • ITE 285 R.Johnson

2
A Single Form Project
  • By default, project starts from Form1. In menu
    see ProjectgtProperties, Startup Object.
  • Form1, what is created in the Form Designer, is
    actually defining a class.
  • VB has been automatically declaring a default
    instance of Form1, the actual object that runs.

3
Starting from Sub Main()
  • Can manually declare the form object and launch
    it in code.
  • Where? Add a code module to project with a Sub
    Main().
  • Set the projects Startup Object to Sub Main().
    May need to uncheck App Framework!
  • Sample Sub Main() Code Dim myform as New
    frmMain Myform.ShowDialog() lt- modal!

4
Multiple Forms
  • As app grows in complexity, one form may not be
    enough.
  • Moving functionality to other helper forms
  • Reduces clutter in main form
  • Help user better focus on task at hand.
  • Relationships between forms add complexity for
    programmer and user.
  • We will see one model for keeping things simple.

5
Modal vs Modeless Forms
  • A modal form takes the applications focus until
    it is closed user cannot switch forms/windows.
  • In VB launch modal form with .Show()
  • A modeless form gets focus, but user can switch
    forms w/o closing it first.
  • In VB launch modeless form with .Show()
  • Modeless interface can be confusing without great
    care by programmer.

6
Modal Form in MS Word
  • User cannot work with document while Paragraph
    Format dialog box is onscreen.

7
Modeless Form in MS Word
  • User can work with document while Find dialog
    box is onscreen.
  • Floating on top is not default behavior in VB.

8
Designing a Custom Dialog Box
  • Typical use some main form launches a helper
    form to obtain secondary (or more detailed) input
    from user.
  • Reduces control clutter on main form.
  • More flexible than just using InputBox or
    MessageBox can have any controls.
  • Data is retrieved by main form from dialog.
  • Dialog contains validation, type conversion or
    other pre-processing code.

9
A Class Grade Averaging App
  • Main form showsall students avgs and overall
    avg.
  • Add Studentbutton opens dialog box.
  • Dialog gets a students grades average.

10
Behind the Scenes
  • Code in Add Student button delcares the dialog
    form object (from frmStudent class)
  • The dialog is dormant in memory, hidden and
    inactive.
  • BUT its properties can be changed by code before
    being shown. Main form pushes data to dialog.
  • Note dialog title (form Text) has been changed to
    show course no.

11
Launching the Dialog Box
  • studentForm.ShowDialog() will launch modally.
  • User can still see main form, but cannot switch
    to it. Only closing the dialog will return
    focus.
  • All application input goes to the dialog form.

12
User Interaction w/Dialog
  • Dialog form contians input validation code
  • Other pre-processing?
  • User cannot interact with main form until dialog
    is closed.

13
Closing the Dialog
  • User may want input used or discarded.
  • Set form DialogResult property (OK/Cancel)in the
    relevant buttons event handler.
  • Closing with upper right X button is Cancel.

14
Dialog After Closing
  • The original call to ShowDialog() returns the
    dialogs DialogResult value.
  • Use the dialog data if not Canceled.
  • Form is still in memory after closing.
  • Main form pulls data from the dialog form.

15
Simple Dialog Box Summary
  • Main form creates instance of dialog form.
  • Main form pushes to dormant dialog to pre-load
    data for editing or to change its properties.
  • Launch modally with ShowDialog().
  • User interacts with dialog, then closes it.
  • If dialog not canceled, main form pulls data
    from dialog (now dormant again).
  • Tip declare/push/show/close/pull.

16
An Improvement
  • Having the main form directly accessing dialog
    controls can be clumsy. Examples
  • retrieving Text property of a TextBox to only to
    convert the string to a number
  • checking Checked properties of a group of
    radiobuttons to map the input to a simple integer
    or character code. Male/female?
  • Solution do this pre-processing work in the
    dialog form!
  • Save the converted value to a public form-level
    variable in the dialog.
  • Pull the already converted value from the form.

17
Sample Main Form Button Code
  • Private Sub btnStudent_Click(ByVal sender As
    System.Object, ByVal e As System.EventArgs)
    Handles btnStudent.Click, mnuAdd.Click create
    the dialog object
  • Dim studentDlg As New frmStudentDlg
  • 'PUSH data TO dialog to pre-load or
    customize
  • studentDlg.Text "Student Grade Averager
    for " txtCourse.Text
  • studentDlg.lblStudNum.Text "Student "
    _
    (lstStudAvgs.Items.Count
    1)
  • 'show modally and check for canceled
    status upon closing
  • If studentDlg.ShowDialog() ltgt
    DialogResult.Cancel Then
  • OK to PULL data FROM dialog
    dblClassAvg ((dblClassAvg lstStudAvgs.Items.Co
    unt) _
    studentDlg.dblStudAvg) / (lstStudAvgs.Items.Count
    1)
  • lstStudAvgs.Items.Add(FormatNumber(stu
    dentDlg.dblStudAvg, 2))
  • txtClassAvg.Text FormatNumber(dblCla
    ssAvg, 2)
  • End If
  • End Sub

18
Sample Dialog Button Code
  • Private Sub btnSave_Click(ByVal sender As
    System.Object, ByVal e As System.EventArgs)
    Handles btnSave.Click
  • input validation or data type
    conversion code here?
  • Me.DialogResult DialogResult.OK
  • End Sub
  • Private Sub btnCancel_Click(ByVal sender As
    System.Object, ByVal e As System.EventArgs)
    Handles btnCancel.Click
  • Me.DialogResult DialogResult.Cancel
  • End Sub
  • Doing the above immediately closes the form -- no
    call to Me.Close() is needed!

19
Typical Dialog Properties
  • FormBorderStyle FixedDialog
  • ShowInTaskbar False
  • MaximizeBox, MinimizeBox False
    inappropriate if opened modally!
  • Dialog box forms do NOT have their own menus!

20
Custom Modeless Forms
  • Open with .Show()
  • DialogResult is not relevant.
  • A modeless interface easily gets confusing for
    user and programmer - - What do I do next? is
    often not clear.
  • The simple declare/push/show/close/pull model
    breaks down.

21
A Splash Screen
  • Uses something for user to see while app loads,
    or useful in nagware demos.
  • No buttons, closes automatically, quickly.
  • Get splash screen to close itself
    MyBase.Close() on first Timer tick.
  • Dont make main form (or user!) wait on splash
    form launch it modelessly with .Show() from main
    forms Load event.
  • Set splashs TopMost True.

22
Timer Control Specifics
  • Timer is not a visible control.
  • Shown in the Component Tray when added to form.
  • Is disabled by default, must set EnabledTrue.
  • Periodically fires the Tick event.
  • Interval is in milliseconds (2000 2sec)
Write a Comment
User Comments (0)
About PowerShow.com