This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label visual basic theory. Show all posts
Showing posts with label visual basic theory. Show all posts

Wednesday, December 12, 2012

Discuss graphics method in VB with example


The Pset Method: The Pset method draw a dot on the screen, it takes the format
Pset (x , y ), color
(x,y) is the coordinates of the point and color is its color. To specify the color, you can use the color codes or the standard VB color constant such as VbRed, VbBlue, VbGeen and etc. For example, Pset(100,200), VbRed will display a red dot at the (100,200) coordinates.

The Pset method can also be used to draw a straight line on the form. The procedure is
For x= a to b
Pset(x,x)
Next x
This procedure will draw a line starting from the point (a,a) and to the point (b,b). For example, the following procedure will draw a magenta line from the point (0,0) to the point (1000,1000).
For x= 0 to 100
Pset(x,x) , vbMagenta
Next x

(b) The Line Method: Although the Pset method can be used to draw a straight line on the form, it is a little slow. It is better to use the Line method if you want to draw a straight line faster. The format of the Line command is shown below. It draws a line from the point (x1, y1) to the point (x2, y2) and the color constant will determine the color of the line.
Line (x1, y1)-(x2, y2), color
For example, the following command will draw a red line from the point (0, 0) to the point (1000, 2000).                         Line (0, 0)-(1000, 2000), VbRed
The Line method can also be used to draw a rectangle. The format is
Line (x1-y1)-(x2, y2), color, B
The four corners of the rectangle are (x1-y1), (x2-y1), (x1-y2) and (x2, y2)
Another variation of the Line method is to fill the rectangle with a certain color. The format is
Line (x1, y1)-(x2, y2), color, BF

(c) The Circle Method: The circle method takes the following format
Circle (x1, y1), radius, color
That draws a circle centered at (x1, y1), with a certain radius and a certain border color. For example, the procedure
Circle (400, 400), 500, VbRed
draws a circle centered at (400, 400) with a radius of 500 twips and a red border.


How many ways are there to specify colors in VB?


There are 4 ways to specify colors in VB:
1.  Hexadecimal numbers: It represents a Hexadecimal value that corresponds directly to one of the 16 million possible colors that can be displayed on a color monitor. If the number starts with &H00, then the remaining 6 Hexadecimal characters represent one of 16 million colors. These colors can be displayed by varying the intensities of Red, Green, or Blue. 2 Hex characters represent the intensities of Red, Green and Blue, and their values range from 0 to 255 Decimal, or 0 to FF in Hex.
An example will make this easier to understand.For instance, the hex value &H000000FF&  represents the color Red.
2.  Color Constants: Color Constants are a subset of the much larger universe of Visual Basic Intrinsic Constants. The idea behind the Visual Basic Intrinsic Constants is to give you a name that you can remember in place of a hard to remember value.
Constant         Value              Description
vbBlack           &H0                Black.
vbRed             &HFF              Red
vbGreen          &HFF00          Green
vbYellow        &HFFFF         Yellow
vbBlue             &HFF0000      Blue
vbMagenta      &HFF00FF    Magenta
vbCyan            &HFFFF00    Cyan
vbWhite          &HFFFFFF    White
3.   The QBColor Function: The QBColor function is a “holdover” from the original version of
Basic. The QBColor expands on the eight colors provided by the Color constants and gives you sixteen. The QBColor function req uires a single argument, which is a number ranging from 0 to 15. The QBColor function returns a value that can then be assigned to any of the color properties. For instance, this code returns a value which when assigned to the BackColor property of the form changes it to Green.
Form1.BackColor = QBColor(2)
4.  The RGB Function: The RGB function , while not extremely user friendly, gives you the ability to generate any valid color property value.
Here is the syntax for this function:               RGB(Red,Green,Blue)
As you can see, the RGB function requires three arguments, with valid values for each being 0 to 255.
Form1.Backcolor = RGB(255,0,0)
Form1.BackColor = RGB(Red:=0, Green:=255, Blue:=0)


difference between pull-down and pop-up menus


Pop up menu: This menu can be displayed anywhere on the page with a right-click of the mouse. It can be displayed anywhere on the Form.
Pop-up menus are invoked with the PopupMenu method. First, you create a menu as usual.
               PopupMenu menuname, flags, x, y, boldcommand
Only the first argument is required, and it’s the menu’s name, as shown in the Menu Editor window. The other arguments are optional. The x and y arguments are the coordinates of a point on the Form (or control) where the menu will be displayed. If you don’t specify the x and y arguments, the pop-up menu will appear at the pointer’s location. The flags argument defines the location and behavior of a pop-up menu. The last argument, BoldCommand, specifies the name of a menu command that should appear in bold. Only one command in the menu can be bold, and bold is commonly used to denote the default (or suggested) option.

CONSTANTS
DESCRIPTION

Location

vbPopupMenuLeftAlign
(default) The specified x location defines the left edge of the pop-up menu.
vbPopupMenuCenterAlign
The pop-up menu is centered around the specified x location.
vbPopupMenuRightAlign
The specified x location defines the right edge of the pop-up menu.
Behavior

vbPopupMenuLeftButton
(default) The pop-up menu triggers the Click event when the user clicks a menu item with the left mouse button only.
vbPopupMenuRightButton
The pop-up menu triggers the Click event when the user clicks a menu item with either the right or the left mouse button.


To specify both a behavior and location constant, combine them with the Or operator. The following code displays a pop-up menu with its top border centered on the Form, which triggers Click events for menu items when either button is pressed.
x = ScaleWidth / 2
y = ScaleHeight / 2
Form1.PopupMenu EditMenu, vbPopupMenuCenterAlign Or  vbPopupMenuRightButton, X, Y

Pull down Menu: This menu can be displayed when you click or bring mouse on menu then list of sub menu will be displayed under the main MENU. Pull down Menus can be created with Menu Editor.


menu editor of visual basic


 Menus are features that are available in nearly all programs nowadays . Making menus is very easy in Visual Basic using the menu editor . To start the menu editor you go to Tools | Menu Editor. The menu editor now appears , here is what is displayed on screen.


Caption : this is the name that the user will see.
Name : this is the name that the programmer uses , we use the mnu prefix for all menu items(mnuFile).
Shortcut : this assigns a shortcut to a menu item  this is a combination of keys which access a menu item for example Ctrl + c is commonly used for copy 
Checked : this allows the programmer to place a check beside a menu item , this is unchecked by default.
Enabled : This specifies whether the menu item is accessible to the user , if this is checked the menu item is grayed out and inaccessible.
Visible: this determines whether the menu item is visible if this is not checked then the menu item will not appear at run time.
WindowList : determines whether the menu item applies to an MDI document (Word and Excel are examples of MDI applications).
HelpContextID : this matches a help description if you have any in your program.
Index : this specifies a menus index in a control array.
You will also have noticed the arrows these are used to manipulate manu items the up and down arrows move the menu items up and down the list and the left / right arrows are used to indent the menu items.


Differentiate model and modeless dialog box


You can show forms from code component classes just as you would in a standard Visual Basic project—you use the Show method:
FormName.Show [ style [, ownerform]]
Here are the arguments for Show:
style—Integer that determines if the form is modal (vbModal = 1) or modeless (vbModeless =
0).
ownerform—String that specifies the component that owns the form you want to show (for
instance, you can use the keyword Me here)
In Visual Basic, Forms are shown as modal or modeless (default). A Form displayed in modal form prevents the user from accessing any other Forms in the current program (project) until she has responded to the active Form:
Private Sub mnuHelpAbout_Click()
frmAbout.Show vbModal
End Sub
Forms shown with the modeless type do not prevent the user from switching to other Forms in the application:
Private Sub mnuHelpAbout_Click()
frmAbout.Show vbModeless
End Sub


MDI form in visual basic


The Multiple Document Interface (MDI) was designed to simplify the exchange of information among documents, all under the same roof. With an MDI application, you can maintain multiple open windows, but not multiple copies of the application. Data exchange is easier when you can view and compare many documents simultaneously. You almost certainly use Windows applications that can open multiple documents at the same time and allow the user to switch among them with a mouse-click. Microsoft Word is a typical example.

An MDI application must have at least two Forms, the parent Form and one or more child Forms. Each of these Forms has certain properties. There can be many child Forms contained within the parent Form, but there can be only one parent Form.
To create an MDI application, follow these steps:
1.  Start a new project and then choose Project ®Add MDI Form to add the parent Form.
2.  Set the Form’s caption to MDI Window.
3.  Choose Project Ø Add Form to add a regular Form.
4.  Make this Form the child Form by setting its MDIChild property to True. To denote that this is a child Form, set its Caption property to MDI Child Form.
The Arrange Property
Windows offers three ways of arranging the windows on an MDI Form. You can cascade them, tile them vertically, or tile them horizontally. Of course, the user can resize and move the windows around, but the automatic placement comes in handy when the MDI Form becomes messy and the user can no longer easily locate the desired window.
Constant
Value
Description


vbCascade
0
Cascades all child Forms
vbTileHorizontal
1
Tiles all child Forms horizontally
vbTileVertical
2
Tiles all child Forms vertically
vbArrangeIcons
3
Arranges the icons for minimized child Forms at the bottom of the MDI Form



Difference between mdi and sdi

1.      MDI = "Multiple Document Interface"
SDI = "Single Document Interface"
2.      MDI is when your application consists of an MDI parent-form
that contains all the other window/forms that the app consists of.
SDI is stand-alone, ordinary windows/forms that exist independently
of each other.
3.      MDI is multiple document interfaces and is essentially a way of creating a shell holder for lots of different windows. (in a way like a browser with tab support, it loads the browser once and then has all the windows within it)
SDI is single document interface and loads as the full program with no internal window support. Kind like a browser with no tab support so a new page has to be opened in a new version of the browser.

Advantages of SDI

·         An SDI interface works very well with multiple monitors and multiple virtual desktops. It also allows users to switch between multiple open documents using the native Windows taskbar and task manager, rather than through special code that must be written into your application.

Advantages of MDI

MDI applications can often handle multiple documents more readily than SDI programs. For example, many MDI text editors allow the user to open multiple text files side by side in the same window, making it easy to compare and look up information from a second document while working on the first.

Go to visual Basic theory index

Active-x control in visual basic


ActiveX Controls: Those are the controls you can add to the Visual Basic toolbox using the Components dialog box. You can add those controls to a Visual Basic program like any other control. You can also use ActiveX controls on the Internet, embedding them in your Web pages, as we’ll see when we work on creating ActiveX controls. ActiveX controls can support properties, methods, and events. Your ActiveX control can be built entirely from scratch, it can be built on another control (such as a list box), or it can contain multiple existing controls (these ActiveX controls are said to contain constituent controls).
When you create an ActiveX control, you create a control class file with the extension .ctl. Visual Basic uses that file to create the actual control, which has the extension .ocx. After you register that control with Windows the control will appear in the Visual Basic Components dialog box, ready for you to add to a program. You can also use these controls in Web pages.

Creating an ActiveX Control

1. Select the New Project menu item in the Visual Basic File menu to open the New Project dialog box,
2. Select the ActiveX Control item in the New Project dialog box and click on OK.
3. To design the calculator, add these controls in a vertical line in the ActiveX control: a text box, Text1; a label with the caption “+”; another text box, Text2; a command button, Command1, with the caption “=”; and a third text box, Text3.
4. Now double-click the command button to open its Click event handler:
Private Sub Command1_Click()
Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text))
End Sub
And that’s it—we’ve created a new calculator ActiveX control.

Testing an ActiveX Control (In Browser): You can test, and even debug, ActiveX controls in the Microsoft Internet Explorer. Just select the Start item in the Visual Basic Run menu to see the ActiveX control you’re designing at work.

Registering an ActiveX Control: First, use the File menu’s Make activex.ocx menu item to create activex.ocx. Next, we’ll use regsvr32.exe, which is usually found in the C:\windows\system directory, to register that control with Windows. Here’s how to register our ActiveX control:
c:\windows\system>regsvr32 c:\vbbb\activex.ocx
After the ActiveX control is registered, it will appear in the Visual Basic Components dialog box

Creating A Visual Basic Project Group To Test An ActiveX Control:
1. Create a new Visual Basic standard EXE project, Project1.
2. Select the Add Project item in the File menu.
3. Click the Existing tab in the Add Project dialog box, select the name of the ActiveX calculator project and click on OK.
4. This adds the calculator ActiveX project to the current project and creates a program group. Select the Select Project Group As item in the File menu, accepting all default file names including the group file itself, group1.vbg.
5. Close the calculator project’s window that makes the calculator ActiveX control available to us in the other project and it will appear in the toolbox.
6. Add a new calculator control to Form1 of Project1.
7. Select the Run menu’s start item to start Project1.


Database connectivity in visual basic


Microsoft database programming object sets:
Data Access Objects (DAO),
Remote Data Objects (RDO), and
ActiveX Data Objects (ADO).
DAO: When Visual Basic first started working with databases, it used the Microsoft Jet database engine, which is what Microsoft Access uses. To support the Jet database engine, Microsoft added the data control to Visual Basic, and you can use that control to open Jet database (.mdb) files. Microsoft also added a set of Data Access Objects (DAO) to Visual Basic:
DBEngine—The Jet database engine
Workspace—An area can hold one or more databases
Database—A collection of tables
TableDef—The definition of a table
QueryDef—The definition of a query
Recordset—The set of records that make up the result of a query
Field—A column in a table
Index—An ordered list of records
Relation—Stored information about the specific relationship between tables
The Data Control
The Data control gives you access to databases without any programming. You can set a few properties of the control and use regular controls such as textboxes to display the values of the fields in the database.
The data control’s Database and Recordset properties refer to those Database and Recordset objects, and you can manipulate the data using those properties.

RDO: Remote Data Objects (RDO) connects to databases using ODBC. You set up ODBC connections to databases using the ODBC item in the Windows Control Panel, and then use one of those connections with the RDO objects. The Remote Data Objects are designed in parallel with the Data Access Objects; for example, the database engine is rdoEngine instead of DBEngine, Recordsets have become rdoResultsets, TableDefs became rdoTables, Workspaces became rdoEnvironments, Field objects became rdoColumn objects, and so on. Although the names have changed, the command set is very similar to DAO.

The Remote Data Control
Like the data control, the remote data control gives you access to a database and displays data in bound controls. Unlike the data control, however, you use the remote data control to access ODBC data sources.
In fact, the remote data control behaves like the data control in most respects, with some differences; for example, you can treat the remote data control’s SQL property like the data control’s RecordSource property, but it cannot accept the name of a table by itself unless you populate the rdoTables collection first.

ADO: Microsoft’s latest set of data access objects are the ActiveX Data Objects (ADO). These objects let you access data in a database server through any OLE DB provider. Here are the ADOs:
Connection
Command
Parameter
Recordset
Field
Erro
Collection
Event

The ADO Data Control
The ADO data control is similar to the data control and the remote data control. The ADO data control is designed to create a connection to a database using Microsoft ActiveX Data Objects (ADO). At design time, you create a connection by setting the ConnectionString property to a valid connection string, then set the RecordSource property to a statement appropriate to the database manager.


class module and standard module in visual basic


The Class Module will completely hide the implementation details. It will expose its functionality and the developer will never see or edit its source code. Class modules can immensely improve your productivity, help you solve many common and intricate programming problems, and even permit you to perform tasks that would be extremely difficult, if not impossible, otherwise.
Even if Visual Basic isn’t a full-fledged object-oriented programming language, you can still use its classes to better organize your code into truly reusable modules and design your applications entirely using concepts derived from the Object-Oriented Design discipline.

Steps to create a new Class:
1.  In the New Project add a Class Modules folder in the Project Explorer window and a Class Module under it. The Class Module is named Class1 by default.
2.  Change the Class Module’s Name property to CTimer. The Class Module doesn’t have a visible interface, so the Code window for the new component will be displayed.
3.  Enter the lines in the Class Module’s Code window.

Code : The CTimer Class’ Code
Dim totalInterval As Double
Dim T1 As Double

Public Sub StartCounting()
    T1 = Time
End Sub

Public Sub StopCounting()
    totalInterval = totalInterval + Time - T1
End Sub

Property Get ElapsedTime() As Double
    ElapsedTime = totalInterval
End Property

Public Sub ResetTimer()
    totalInterval = 0
End Sub
The code of the Class Module is quite similar to the Module’s code, but it doesn’t have a public variable. The totalInterval variable can’t be accessed directly from any procedure outside the Class Module. It can only be read through the ElapsedTime() procedure. Notice that this is a special type procedure called Property Get. Every time an application attempts to read the value of the totalInterval variable, the ElapsedTime() procedure is invoked. Its code reads the value of the totalInterval local variable and assigns it to the ElapsedTime() procedure. This value is then passed to the calling application.
When an application attempts to set a property value, a similar procedure is invoked, only it’s a Property Let procedure. This Class doesn’t have any properties that can be set, so there are no Property Let procedures in it. We’ll discuss both Property procedures after looking at this simple example. The Property Let and Property Get procedures act like buffers between the Class and the application that uses it. For one thing, the application can’t set the variable directly.
Testing the CTimer Class
1.  Add a new project with the File -> Add Project command. Visual Basic will add a new folder to the Explorer window, the Project1 folder, and it will place a Form under it. 
2.  In order to use the CTimer Class in the test project, we must first add a reference to the Class. Open the Project menu and select References. In the References dialog box select the entry TimerProject by checking it. The References dialog box displays the name of the project, not the name of the Class.
Then, open the Form’s Code window and enter the lines in Code:
Dim TMR As New CTimer
 
Private Sub Command1_Click()
    If Command1.Caption = “Start Timing” Then
        TMR.StartCounting
        Command1.Caption = “Stop Timing”
    Else
        TMR.StopCounting
        Command1.Caption = “Start Timing”
    End If
End Sub
 
Private Sub Command2_Click()
    ETime = TMR.ElapsedTime
    MsgBox “I’ve been counting for ” & vbCrLf & _
        Hour(TMR.ETime) & “ hours” & vbCrLf & _
        Minute(TMR.ETime) & “ minutes and ” & vbCrLf & _
        Second(TMR.ETime) & “ seconds” & vbCrLf
End Sub
 
Private Sub Command3_Click()
    End
End Sub

Standard module.

                        1. When using multiple forms in a project, it is important for you to consider each sub procedure or function procedure that you create. If the procedure will be used in only one form, then it should be included in the code for that form module. If, in fact, you will need to use the procedure in multiple forms, write the procedure in standard code module.
                        2. A standard code module is a Visual Basic file with extension .bas, which is added to project. Standard code module does not contain a form, only code.
                        3. Create a new standard code module by selecting the Add Module command on the project menu. Select module on the new tab of the add module dialog box and click open. A new code window titled module1 opens on the screen. Module is also added to project explorer.
                        4. Standard code module has a general declarations section and procedure, just like form module. You can declare a variable and procedure as you declare in form module.