2 form and controls 1

Upload: awee-dip

Post on 06-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 2 Form and Controls 1

    1/44

    Part 1 : Forms and Controls

    F5227VB.NET PROGRAMMING

    TOPIC 2

  • 8/3/2019 2 Form and Controls 1

    2/44

    VB.NET Controls

    Navigate IDE

    A Text Box Walkthrough

    A Button Walkthrough

    A Label Walkthrough

    A List Box Walkthrough

    The Name Property

    A Help Walkthrough Fonts / Auto Hide

  • 8/3/2019 2 Form and Controls 1

    3/44

    Starting a New Project

    Default: Windows

    Application

  • 8/3/2019 2 Form and Controls 1

    4/44

    The Integrated Development Environment

    (IDE)

    Menu bar provides many menu items

    Examples include File and Edit menus

    Toolbar lies below menu bar

    Provides shortcuts to the menu bar

    Toolbox

    Contains various icons representing VB controls

    Solution Explorer Shows all forms, references, classes, and modules the

    current project contains

  • 8/3/2019 2 Form and Controls 1

    5/44

    The IDE

    Menu bar

    Blank form

    in IDE

    Toolbox Toolbar

  • 8/3/2019 2 Form and Controls 1

    6/44

    Solution Explorer and Properties Window

    Solutions Explorer

    shows all the forms,

    references, classes,

    and modules in theproject

    Properties window

    shows all the

    properties of the

    selected object

  • 8/3/2019 2 Form and Controls 1

    7/44

    The Code Window

    Tabs at top let you toggle between the form and

    the code window

    Comments

    appear green

    in the IDE

    Object-oriented syntax:

    object and property

    separated by dot (period)

    Procedure box

    lists events

    the object inthe object box

    recognizes

    Object boxlists all

    objects in

    the form

  • 8/3/2019 2 Form and Controls 1

    8/44

    Understanding the IDE

    Design time

    When you are placing controls on the form

    When you are writing code in the code window

    RuntimeWhen the code in your project comes to life,

    responding to events Press Start on the Debug menu in the IDE

    Press the F5 key in the IDE Press the start button in the IDE

  • 8/3/2019 2 Form and Controls 1

    9/44

    Interfaces of VB Objects

    Forms and controls are objects

    Objects have interfaces:

    1) Properties: typically relate to appearance of

    objects2) Events: user or system actions recognized by

    the object Procedures written to handle events

    3) Methods: actions that objects are capable ofperforming

  • 8/3/2019 2 Form and Controls 1

    10/44

    1) Properties

    Special types of data associated with object

    Most relate to appearance of objects

    i.e. Label1.BackColor = Color.Red

    Some relate to behavior of objects

    i.e. btnNext.Enabled True

    Object and property separated by dot

    (period) Property must be given a value

  • 8/3/2019 2 Form and Controls 1

    11/44

    2) Events

    User or system actions the object recognizes

    i.e. a button has a Click event

    Event procedure

    Procedure written to handle a specific eventAlso called event handler

    Syntax: Private Sub ObjectName_Event

    Code surrounded by Sub End Sub

    Private refers to the scope of the procedure

    Object and event separated by underscore

  • 8/3/2019 2 Form and Controls 1

    12/44

    3) Methods

    Actions objects are capable of performing

    i.e. Me.Close()

    Syntax: Object.Method(Parameter List)

    Parameter list: arguments passed in to the

    method

    Parameter list must be enclosed in parentheses,

    even if no parameters are required

  • 8/3/2019 2 Form and Controls 1

    13/44

    A Text Box Walkthrough

    Drag Text Box from ToolBox

    Sizing

    Delete

    Properties

    Text, Color, Font, Size, Location, Visible,

    Enabled

  • 8/3/2019 2 Form and Controls 1

    14/44

    A Button Walkthrough

    Add the button

    Change the Text property

  • 8/3/2019 2 Form and Controls 1

    15/44

    A Label Walkthrough

    Add the Label

    Change the Text property

    Resize the control

  • 8/3/2019 2 Form and Controls 1

    16/44

    A List Box Walkthrough

    Add the List Box

    Add data

    Resize the control

  • 8/3/2019 2 Form and Controls 1

    17/44

    The Name Property

    How the programmer refers to a control in code

    Name must begin with a letter

    Must be less than 215 characters long

    May include numbers and the underscore

    Naming convention: use appropriate 3 character

    naming prefix

    First three letters identifies the type of control Remaining letters identifies the purpose

    E.g. a text box to store a social security number would be

    called txtSocialSecurity

  • 8/3/2019 2 Form and Controls 1

    18/44

    Common Control Name Prefixes

    Control Prefix Examplebutton btn btnComputeTotallabel lbl lblInstructionslist box lst lstOutputtext box txt txtAddress

  • 8/3/2019 2 Form and Controls 1

    19/44

    Fonts

    Proportional width fonts take up less space

    for "I" than for "W" like Microsoft Sans Serif

    Fixed-width fonts take up the same amount

    of space for each character like CourierNew

    Fixed-width fonts are good for tables

  • 8/3/2019 2 Form and Controls 1

    20/44

    Auto Hide

    Hides tool windows when not in use

    Vertical push pin icon indicates auto hide is

    disabled

    Click the push pin to make it horizontal andenable auto hide

  • 8/3/2019 2 Form and Controls 1

    21/44

    Viewing the Code

    The GUI Forms Designer generates textual

    code

    Prior to VB programmers wrote everything in

    textual code Click on the Form1.VB tab to see the code

    (not the design tab)

  • 8/3/2019 2 Form and Controls 1

    22/44

    An Event Procedure Walkthrough

    An event is an action, such as:

    The user clicks on a button

    A form is minimized

    The mouse enters or exits a control

    The form is re-drawn

    Usually, nothing happens until an event

    occurs

    Th th t i ti

  • 8/3/2019 2 Form and Controls 1

    23/44

    The three steps in creating a

    VB.NET program:

    1. Create the interface; that is, generate,

    position, and size the objects.

    2. Set properties; that is, configure the

    appearance of the objects.3. Write the code that executes when events

    occur.

  • 8/3/2019 2 Form and Controls 1

    24/44

    Changing Properties

    Properties are changed in code with the following:

    controlName.property = setting

    This is an assignment statement

    Examples:txtBox.ForeColor = Color.Red

    txtName.Text = "Hello There"

    txtName.Visible = False

    txtName.Location.X = 100

  • 8/3/2019 2 Form and Controls 1

    25/44

    Adding Code to an Event

    To add code for an event: In the VB Code Window select the control on the left side

    menu and the event of interest on the right side menu

    Or double-click the control in the designer to bring up themost common event for that control

    Other methods for opening the Code window: If the Code window is visible, click on it

    Double-click anywhere on the Form window Select the Code option from the View menu

    Press the F7 method key anywhere on the design form

    Select the View Code icon from the Project Window

  • 8/3/2019 2 Form and Controls 1

    26/44

    Program Region

  • 8/3/2019 2 Form and Controls 1

    27/44

    Event Procedures

    Private Sub objectName_event(ByVal senderAs

    System.Object, ByVal eAs System.EventArgs)

    Handles objectName.event

    For now you can ignore most of this, aside from knowing thename of the subroutine:

    Private Sub objectName_event() Handles

    objectName.event

  • 8/3/2019 2 Form and Controls 1

    28/44

    Structure of an Event Procedure

    Private Sub objectName_event(...)

    Handles objectName.event

    statements Your code goes hereEnd Sub

  • 8/3/2019 2 Form and Controls 1

    29/44

    IntelliSense

    Automatically pops up to give the programmer

    help.

  • 8/3/2019 2 Form and Controls 1

    30/44

    Code for Walkthrough

    Private Sub txtFirst_TextChanged(...)

    Handles txtFirst.TextChanged

    txtFirst.ForeColor = Color.Blue

    End Sub

    Private SubbtnRed_Click(...)

    Handles btnRed.Click

    txtFirst.ForeColor = Color.Red

    End Sub

    Private Sub txtFirst_Leave(...)

    Handles txtFirst.Leave

    txtFirst.ForeColor = Color.Black

    End Sub

  • 8/3/2019 2 Form and Controls 1

    31/44

    Assigning properties in code

    The following won't work:

    Form1.Text = "Demonstration"

    The current form is referred to by the

    keyword Me.

    Me.Text = "Demonstration"

    The Declaration Statement of an

  • 8/3/2019 2 Form and Controls 1

    32/44

    The Declaration Statement of an

    Event Procedure

    A declaration statement for an event procedure:

    Private Sub btnOne_Click(...) HandlesbtnOne.Click

    The name can be changed at will. For examplePrivate Sub ButtonPushed(...) HandlesbtnOne.Click

    Handling more than one event:Private Sub ButtonPushed(...) HandlesbtnOne.Click, btnTwo.Click

  • 8/3/2019 2 Form and Controls 1

    33/44

    The MessageBox.Show Method

    The MessageBox.Show method is used to display a boxwith a message for the user

    The message box also contains a title and an icon

    General forms of the MessageBox.Show method

    MessageBox.Show(text) MessageBox.Show(text, caption)

    MessageBox.Show(text, caption, buttons)

    MessageBox.Show(text, caption, buttons, icon)

    MessageBox.Show(text, caption, buttons, icon, defaultbutton)

    To do: Add a MessageBox.Show to the button clickevent

  • 8/3/2019 2 Form and Controls 1

    34/44

    Console.WriteLine

    Another handy way to output information is to the Console:

    Console.WriteLine("Hello there")

    Outputs the message in double quotes and adds a

    newline

    Console.Write("Hello again. ")

    Outputs the message in double quotes without a

    newline

    Useful for debugging, dont have to push the OK button and

    clutter up the screen with message boxes

    Adding Additional Event

  • 8/3/2019 2 Form and Controls 1

    35/44

    Adding Additional Event

    Procedures

    Comments

    Explanatory remarks made within a program

    Indicated by an apostrophe () or the keyword Rem

    Statement categories An executable statement causes some specific action

    to be performed by the compiler or interpreter

    A nonexecutable statement is a statement that

    describes some feature of either the program or itsdata but does not cause the computer to perform

    any action

  • 8/3/2019 2 Form and Controls 1

    36/44

    Focus on Program Design and Implementation:

    Creating a Menu of Forms

    A main menu form can be displayed as an

    applications opening window to provide the

    user with a summary of what the applicationcan do

    The main menu form is created as either:

    A set of buttons A menu bar

  • 8/3/2019 2 Form and Controls 1

    37/44

    A Sample Main Menu Screen

    Focus on Program Design and Implementation:

    Creating a Menu of Forms

  • 8/3/2019 2 Form and Controls 1

    38/44

    Adding a Second Form

    From the Project menu, select Add New Item, and then WindowsForm Default name is Form2.vb

    To display a form:Dim varName As New FormNamevarName.Show()

    E.g.:Dim secondForm as New Form2secondForm.Show()

    To make the current form invisible but retain its variables andcomponents:

    Me.Hide() (could display again with Show())

    To destroy a form and unload its variables and components:Me.Close() (need to create a new one to display again)

  • 8/3/2019 2 Form and Controls 1

    39/44

    Knowing About: The Help Facility

    Visual Basics Help Facility can be accessed byselecting either the Contents, Search, orIndex options from the Help menu

    The Contents tab displays a Table of Contentsfor the documentation

    The Index tab provides both a general indexof topics and a text box for user entry of a

    specific topic The Search tab provides a means of entering

    a search word or phrase

  • 8/3/2019 2 Form and Controls 1

    40/44

    Getting Help from the Help Menu

    Enter search

    term in the

    Look for box

    Double-click

    an item in

    the list box

    Double-click an item in the lower center pane; upper center pane

    shows results

    Knowing About: The Help Facility

  • 8/3/2019 2 Form and Controls 1

    41/44

    Knowing About: The Help Facility

    (Continued)

    Dynamic Help

    The Dynamic Help window displays a list of help

    topics that changes as you perform operations

    To open the Dynamic Help window, click Help onthe menu bar and then click Dynamic Help

    Context-sensitive Help

    Context-sensitive Help immediately displays a

    relevant article for a topic

    To use this facility, select an object and press F1

    Common Programming Errors and

  • 8/3/2019 2 Form and Controls 1

    42/44

    Common Programming Errors andProblems

    A common problem is not being able to locate

    all of the elements needed to create an

    application

    Can usually get these windows back from theView menu

    A common error is forgetting to save a

    project at periodic intervals at design time

  • 8/3/2019 2 Form and Controls 1

    43/44

    Summary

    Event-based programs execute program codedepending on what events occur, whichdepends on what the user does

    GUIs are graphical user interfaces that

    provide the user with objects that recognizeevents

    The term design time refers to the periodof time when a Visual Basic application is

    being developed under control of Visual Basic The term run time refers to the period of

    time when an application is executing

  • 8/3/2019 2 Form and Controls 1

    44/44

    Summary

    A Visual Basic program consists of a visual partand a language part

    Basic steps for developing a Visual Basicprogram:

    Create the GUI Set the properties of each object on the interface

    Write procedural code

    A form is used during design time to create agraphical user interface for a Visual Basicapplication

    The most commonly placed objects on a form arebuttons, labels, and text boxes