13
Visual Basic .NET for Mainframe Programmers VBN‐001

Visual Basic.NET for Mainframe Programmers

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers 

VBN‐001

Page 2: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐1 

Getting Started with Visual Basic 

This section will introduce you to some new terms and the Visual Studios Integrated Development 

Environment (IDE). You will also learn how to develop and run projects.

Page 3: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐2  Copyright © MindCross Training & Consulting ‐ All rights reserved.

Page 4: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐3 

Visual Basic IDE Overview 

The Integrated Development Environment (IDE) The IDE in Visual Studios is comprised for four main areas. These are: 

•  Toolbox 

•  Properties 

•  Solution Explorer 

•  Tabbed work area 

Modes of Operation 

Within Visual Basic there are two modes of operation: 

•  Design mode – for developing applications •  Runtime mode – for testing and running applications 

When you initiate Visual Studios you will automatically be placed in design mode. When you debug or run a project you will be switched into runtime mode. Anytime you are in runtime mode you will see “Running” in the Visual Studios title bar. 

The design area for an application is 

a form

Page 5: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐4  Copyright © MindCross Training & Consulting ‐ All rights reserved. 

What is a Project? A Visual Basic Project is a container for all files and information about an application. The actual project file has an extension of .vbproj.   All the files and information associated with a project can be found in the Solution Explorer window. 

Different  types  of projects  can  be  created within  the  IDE.  The projects are created from project  templates. Some of the ones you may use include: 

•  Windows application •  Class library •  Web control and Windows control library •  ASP.NET Web application •  ASP.NET Web service application 

IDE Default Environment Settings 

When you start working with the Visual Studios development environment a collection of settings will be available to you based on the environment settings defined. This can be specified the first time you start Visual Studios after installation or any time you begin working with Visual Studios. The three types of development activity from which you can choose include: 

•  General development •  Visual Basic development •  Web development 

It’s  important  to have  the appropriate one  selected  based on  the work  you will  be doing.  To specify the environment go to Tools >> Import and Export Settings. You can select to reset the settings,  in  which  case  you  will  be  prompted  with  the  development  options,  or  you  can import/export  your  preferred  settings  from  a  specified  location.  The  settings  environment settings will look similar to this: 

Tour of the Integrated Development Environment 

Projects as Containers 

Import and Export Settings Wizard 

A group of projects may be called a solution ‐ ‐ 

hence the term “Solution Explorer”

Page 6: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐5 

Designing Visual Basic Forms 

ToolBox ‐ Controls To start designing forms in Visual Basic you will use the 

Toolbox. By default the Toolbox is minimized, so the first thing you’ll want to do is change the display of the Toolbox. 

•  Place your cursor over the Toolbox icon. 

•  When the Toolbox is opened click the “Auto Hide” button in the top right corner. 

The items in the toolbox are controls. These controls will be used to design the user interface. The controls are grouped into types. However, you can change the display  based on your preferences. 

Some of the common tools include: 

•  Button •  CheckBox •  Label •  RadioButton •  TextBox 

The Format Menu 

As your form is designed you will find it helpful to use the format menu for tasks such as aligning items, sizing, spacing, etc. Once your design is complete you may want to use “Lock Controls” to be sure your design doesn’t accidently change. 

Tabbing Order When  creating  a  form  with  user  input  items  you’ll  want  to think about tabbing order ‐ ‐ the order in which the users will tab through the input fields. 

Set  the tabbing order by using  the Tab Order  function on the View menu. Once you are finished you can turn it off again. 

How to: Resize controls 

How to: Set the Tab Order 

Windows Forms Controls by Function 

•  ComboBox •  ProgressBar •  PictureBox •  ListBox •  Panel

Page 7: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐6  Copyright © MindCross Training & Consulting ‐ All rights reserved. 

Properties As discussed in the section on “Object‐oriented Programming” properties are information or characteristics about something. For instance they maintain values about controls on your form. 

The properties window displays the name of the item, or control, you’re working with, the inheritance information, along with a list of design time properties and their values. Some common properties we’ll be working with include: 

•  (Name) •  Text •  Font •  BackColor •  ForeColor •  Enabled •  Visible •  Location 

Each item, such as a control, form, or class, has a different set of properties associated with it. The properties listed are not a complete list of all properties available. The ones listed are the design properties ‐ ‐ meaning the properties you can change at design time. There are other properties that can only be changed programmatically. These changes would take place at runtime. 

The Name property is unique. The Name property is the internal name of the form or control. You will use it any time you refer to that item within your program code; therefore, it’s important to have naming conventions and make it short, yet descriptive. It’s common practice to use a value that indicates the type of item. 

Name property examples frmMain txtLastName

Page 8: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐7 

Creating a Project 

The following steps will walk you through the process of creating a simple project. The project created will look similar to the window shown to the right. 

1.  File > New Project 2.  Select the project type:  Windows Application 3.  Give it a name:  Project1 4.  Design the form using the Toolbox 

•  Add a panel control sizing it about the same size as the form 

•  Add two labels 5.  Set the Text property for Label 1 to:  Hello World! 6.  Set the Text property for Label 2 to:  Current date 7.  Set the form properties 

•  Set the Text property to:My First Project Notice that the Text property changes the title bar 

•  Set the Backcolor property to any color of your choice 8.  Add code to Form1_Load by double clicking on the title bar of your form in the design 

panel. The code should be typed exactly as follows: Label2.Text = "Today is " & Now 

Notes:  Now is a system property of the DateTime object that holds the current date and time. The symbol & is used for concatenation.

Page 9: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐8  Copyright © MindCross Training & Consulting ‐ All rights reserved. 

Running and Saving a Project 

1.  Compile and run your project in one of the following ways 

•  F5 or Toolbar item: Start Debugging 

•  Menu:  Debug >> Start Debugging 2.  Set the location for where your project files should be saved 

•  Tools >> Options 

•  Click Project and Solutions 

•  For Visual Studios Projects Location browse and create the folders: My Documents\Visual Basic\Class\ 

•  Click OK 

3.  Save all your project files 

•  File  >>  Save ALL 

•  Name:  Project1 

•  Directory:  Project1 

A project file is a file containing pointers to other files. There are many file are associated with Visual Basic projects. Each file is considered a project item. 

Note:  The project files will be discussed later in the course. 

More Details on the IDE 

Visual Basic Menus 

View Menu The view menu, for the most part, gives you a list of the available windows in Visual Basic. If you accidently close a window, or simply wish to view a particular window, you can invoke the View menu and select the window you want to view. 

Object Browser One of the windows that we have not yet discussed is the Object Browser. The Object Browser is extremely useful, as it shows a complete list of components and system objects available to you and all the associated information for each object. 

Since it’s sometimes hard to find exactly what you’re looking for a Search field is available to help you look for a particular object or object information. 

Object Browser

Page 10: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐9 

The Object Browser window is broken down into four areas as follows. 

For each object/component it shows the following information: 

•  Class name, and from where it is inherited 

•  Summary information 

•  Properties 

•  Methods 

•  Events 

•  Description and summary information 

Project Menu The Project menu shows all the possible items that can be added to a project or solution. As more items are added they will be displayed in the Solution Explorer window. 

Debug Menu The Debug menu is used to both run and test your project and to debug your project. The debugger is very user friendly and works similar to debugging utilities in other programming languages. For instance, to create a line break in your code you simply click on the line where you want the break to occur. 

Tools Menu The main item to discuss in the Tools menu is the Options. The options item contains a lot of defaults for your development environment. In fact, there’s a section specific to just the Visual Basic environment called “VB Specifics”. 

Walkthrough: Debugging a Windows Form 

Environment, Options Dialog Box 

Search 

Object listing : : : : 

Properties 

Methods 

Events 

Description information 

Members

Page 11: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐10  Copyright © MindCross Training & Consulting ‐ All rights reserved. 

Toolbox Window We already discussed the Toolbox; however, one problem using the Visual Studios Toolbox is the large number of tools from which to choose.  Since it’s sometimes hard to find what you’re looking for keep in mind that the tools are, by default, listed in groups. 

You can change the display of the Toolbox by clicking the bar specific to what you’re creating, for example, “All Windows Forms”. 

Or, you can invoke the popup window with a Right Click and select “Sort Items Alphabetically”. This will change the display to sort all the tools in ascending order. 

Solution Explorer Window The Solution Explorer window shows a complete listing of all files, items, and information associated with your project or solution. As you add new items using the Project menu they will appear in the Explorer window. From here you can also view the Project Properties by clicking “My Project”. These properties will be discussed later in the course. 

Basic Coding Structures 

Code Window Visual Basic code, like most other programming languages is very structured. The layout of all code modules is similar to what you see here. 

The General Declarations section typically contains variables that will be shared throughout the module. That is an optional section. Below that is the definition of the class on which you are working with. This main section will be set up for you automatically by Visual Basic. Any code that you write will be contained within this section ‐ ‐ with the exception of general declarations. 

Procedures are Blocks of Code Within the class start and end will be a series of procedures where all code will reside ‐ ‐ all code must be contained within some type of procedure block. There are a variety of different types of procedures in Visual Basic. Each will be discussed in detail throughout the course. 

General Declarations Public Class . . . 

Procedure : End Procedure : End . . . 

End Class

Page 12: Visual Basic.NET for Mainframe Programmers

Visual Basic .NET for Mainframe Programmers  Getting Started with Visual Basic 

Copyright © MindCross Training & Consulting ‐ All rights reserved.  Page 2‐11 

For now, we’ll mention two common types of procedures: 

•  Sub Procedures •  Function Procedures 

Sub Procedures 

Function Procedures 

Handling Events Events are actions triggered by a state change or activity start of end. When events are triggered a block of code is executed and that block of code is a Sub Procedure which is responsible for handling the event process. 

Each object, for example a button or textbox, has a set of events associated with it. The available events can be viewed from the Object Browser or code window. 

: <modifier>  Sub <procedure‐name> : End Sub 

<modifier>  Function <procedure‐name> (arg1, arg2, …) : End Function : 

Sub procedures are a block of code which performs a task and does not return a value. 

Function procedures are a block of code that does return one or more values. 

Object List of events

Page 13: Visual Basic.NET for Mainframe Programmers

Getting Started with Visual Basic  Visual Basic .NET for Mainframe Programmers 

Page 2‐12  Copyright © MindCross Training & Consulting ‐ All rights reserved. 

Since an event code block is actually a Sub Procedure the syntax is the same. The syntax for an event should be similar to the following: 

<modifier>  Sub  <object‐name_event‐name> . . . code 

End Sub 

Events Overview 

Environment, Options Dialog Box 

Mouse Events in Windows Forms 

Setting Properties Programmatically During runtime of an application you can programmatically obtain the value of a property or set any modifiable property. The syntax to do this is fairly straight forward. Take a look at the examples below. 

frmMain.Text = “Hello World!” 

lblMsg = “Please try again” 

Label2.Text = "Today is " & Now 

Button1.Visible = False 

When working in the Visual Basic code window, by default, you will see dynamic help which shows popup menus of information as you type your code. These dynamic windows can be extremely helpful as you learn Visual Basic. 

Code Example 

Private Sub Form1_FormClosing . . . MsgBox("Goodbye") 

End Sub