Showing posts with label Visual Basic. Show all posts
Showing posts with label Visual Basic. Show all posts

Sunday, November 21, 2010

Visual Basic Forms Method : Hide

Affected : Form , Forms collection , MDIForm


Purpose : The Hide method makes an active and visible form disappear from the screen and is the same as setting the form's visible property to False ( 0 ) .


Syntax :
[ form . ] Hind



Private Sub Form1_Load ( )
Form2.Hide
Form2.BackColor = RGM ( 0 , 255 , 0 )
Form2.ForeColor = RGB ( 0 , 0 , 255 )
MsgBox " Press OK to see form2 "
Form2.Show
End Sub



The Hide method reduces clutter by removing a form from the screen without removing it from memory . Any forms not initially needed can be loaded and hidden until desired. The Show method displays a hidden form onscreen. Hidden forms take a little more time to load at program startup but can then be displayed very quickly.


Show Method :
The Hide method shares an inverse relationship with the Show method. Hide removes the form from sight and show restores it. In the sample syntax , the two methods work together to make a form invisible while its color change.This technique also can be used for a warning bialog box that displays context-sensitive messages. if the form is not loaded , the show method loads it into memory.



Visible Property :
When the Hide method makes a form invisible , the Visible property of the form becomes False. The difference between using the Hide method and directly changing the setting of the visible property lies in the types of objects affected . the hide method can only be used on a form .In contrast , the visible property can be used on any of the objects in visual basic.

Saturday, November 20, 2010

Visual Basic Forms Property : ControlBox

Affected : Form
Purpose : The ControlBox property governs whether a control box appears in the top-right corner of a form. This property is read -only at runtime.


Syntax :
[ form . ] ControlBox

Argument of the ControlBox Property
form : Name property of the form ( if no name is specified, references the current form's controlbox property )



Public Sub SetColor ( FormName As Form )
If FormName.ControlBox = 0 Then
FormName.BackColor = RGB ( 192, 192 192 )
Else
FormName.BackColor = RGB ( 0 , 128 , 0 )
End If
End Sub

Visual Basic Forms Property : AutoShowChildren

Purpose : The AutoShowChildren property indicates whether an MDI child form can be hidden when it is loaded. By using this property, you can load but not show an MDI child form.


Syntax :
Name.AutoShowChildren [ = Boolean% ]

Arguments of the AutoShowChildren Property
Argument : Description
boolean% : An optional Boolean expression that specifies whether MDI child forms are automatically visible. if not included defaults to True.
Name : Name property of MDIForm



Private Sub Form_Load
MDIForm1.AutoShowChildren = True
End Sub

Visual Basic Forms Method : Arrange

Purpose : Use the Arrange method on an MDI form to arrange all MDIchild forms contained within the MDI form.


Syntax :
[ Name ] . Arrange Setting%

Arguments of the Arrange Method
Arguments : Decsription
Name : Name property of the MDI form
setting% : A value that specifies how to arrange the windows or icons



Private Sub menuWindow_Click ( Index As Integer )
Select Case Index
Case 0 : MDIForm.Arrange vbCascade
Case 1 : MDIForm.Arrange vbTileHorizontal
Case 2 : MDIForm.Arrange vbTitleVertical
Case 3 : MDIForm.Arrange vbArrangeIcons
End Select
End Sub

Visual Basic Forms Event : Activate

Purpose : The Activate event fires when a form, MDI form , or data report becomes the active window in an application.


Syntax :
Sub Form_Activate ( )
Sub MDIForm_Activate ( )
Sub DataReport_Activate ( )



An object can become active by a user action such as clicking the object or by using the Show or SetFocus method in code. The activate event can occur only when an object is visible . A form loaded with the Load statement , for example , isn't visible unless you use the Show method or set the form's Visible property to True. The Activate event occurs only when moving the focus within an application. Moving the focus to or from an object in another application doesn't trigger the event.


The Deactivate Event :
the Deactivate and Activate events have an inverse relationship. Whereas the Activate eent occurs when a form becomes active , the Deactivate event occurs when a form is no longer active. The Deactivate event occurs only when moving the focus within an application. Moving the focus to or from an object in another application doesn't trigger either event. The Deactivate event doesn't occur when unloading an object.
The LostFocus event occurs before the Deactivate event.

Wednesday, November 3, 2010

Visual Basic Operating Environment Function : Shell

Purpose : The Shell function runs a specified .EXE , .COM , .BAT OR .PIF program.


Syntax :
Shell ( program-name$ [ , mode% ] )



A% = Shell ( " WINWORD.EXE " )
B% = Shell ( " C : \ WORD \ WINWORD.EXE " , 3 )



The Shell function loads an executable file into the windows environment and returns the task ID number that Windows assigns to each tunning program.
the first example loads and runs the program file WINWORD.EXE . it assumes that the file is in the default directory or in a directory specified by the path statement. because the mode is not specified , it uses the default mode of 2 ( minimized with focus )



Shell function constants of then mode% parameter
consant : value : Description
vbHide : 0 : Hides window with focus
vbNormalFocus : 1 : give window focus and restores ti to its original size and position.
vbMinimizedFocus : 2 : Minimizes and gives window focus
vbMaximizedFocus : 3 : Mazimizes and gives window focus
vbNormalNoFocus : 4 : Restores window to its original size and position with-out focus
vbMinimizedNoFocus : 6 : Minimizes window without focus.


Visual Basic Operating Environment Function : Environ, Environ$

Purpose : The Environ and Environ$ function return setting from the operating system's environment table.


Syntax :
Environ ( { entry_name$ | entry-position% } )
Environ$ ( { entry_name$ | entry-position% } )



A$ Environ ( " path " )
A$ Environ ( 1 )



DOS maintains a table of values called environment variables. values in the environment table typically store information abut such things as the current drive and path, the location of the command processor, or special settings needed y various programs.
The Environ and Environ$ functions allow the program to read the current setting of the operating system's environment table . Entries in the environment table are set by using the DOS command syntax SET ENTRY_NAME = ENTRY_VALUE. see a DOS reference manual for more information on the SET command. Environ allows you to specify either the name or position of the entry to be retrieved.

Visual Basic Operating Environment Function : Command

Purpose : The Command and command$ function return a variant or string that contains any Command-line Parameters used when Visual Basic or the Visual Basic environment Program was started.

Syntax :
Command
Command$

Params$ = Command$

in this example stores command-line parameters for the current program into the string Params$. these functions return any test that followed the CMD parameter on the command line. the string returned by the visual basic environment can be modified by choosing properties from the project menu. When used with a Visual Basic program , Command$ returns any text that followed the executable filename on the command line.

Monday, November 1, 2010

Visual Basic Operating Environment Statement : Beep

Purpose : The Beep statement causes the computer's speaker to produce a short tone.

Syntax :
Beep

If ErrorNumber% > 0 Then Beep

The example beeps the speaker if the variable ErrorNumber is set. The Beep statement causes the computer to send a short tone to the speaker. you can control neither the tone not the duration; however, issuing several beep command consecutively can create the effect of a longer beep.

Visual Basic Operating Environment Statement : AppActivate

Purpose : The AppActivate statement activates a running Windows program and gives it the Current focus. It does not change the window State property (minimized or maximized) Of the program.

Syntax :
AppActivate ProgramTitle$ [, wait]

AppActivate “ paintbrush - ( Untitled ) “
AppActivate “ paintbrush-( untitled )” , True

The first example gives the paintbrush application the focus, assuming that it is already running and has no file loaded.
The second example works the same as the first example , except that it waits until the calling application has the focus.

Wednesday, October 13, 2010

Visual Basic File System : FileSystemObject

Purpose :
The FileSystemObject object allows you to work with drives, folders and files programmatically just as you can in the windows explorer interactively. you can copy, move and create file and folder; get information about the drives , folders and files; and so on. FileSystemObject gives you direct access to the windows explorer functionality with better performance than the standard file system access in visual basic and see the object list.

Object of the FileSystemObject Model

Drive : Gathers information about drives attached to the system, such as how much room is available.
Folder : Creates , deletes , or moves folders , plus queries the system as to their names pates and so on.
Files : Creates , deletes or moves files , pluse queries the system as to their names, paths and so on.
FileSystemObject : Creates, deletes , gains information about and generally manipulates drives folders and files . this is he main object of the group.
TextStream : Reads and writes text files.

  • Use the create object method, or dimension a variable as a File System object object to create a File system object object
  • Use to appropriate method on the newly created object.
  • Access the object’s properties.
Creating a File System Object

Dimension a variable as type FileSystemObject object.
Dim fso As New FileSystemObject

Use the CreateObject nethod to create a FileSystemObject object
Set fso = CreateObject ( “ Scripting.FileSystemObject “ )

Getting Drive information:
- Total size of the drive in bytes ( TotalSize property )
- How much space is available on the drive in bytes ( AvailableSpace and FreeSpace properties )
- What letter is assigned to the drive ( DriveLetter property )
- What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM disk ( DriveType property )
- Drive’s serial number ( SerialNumber property )
- Type of file system the drive use, such as FAT, FAT32, NTFS and so on ( FileSystem property )
- Whether a drive is available for use (IsReady property)
- Name of the share and /or volume ( ShareName and VolumeName properties )
- Path or root folder of the drive (path and RootFolder properties)

Project>References>Microsoft Scripting Runtime

Option Explicit
Private Sub Command1_Click()
Dim fso As New FileSystemObject, drv As Drive, s As String
Dim t As String
Set drv = fso.GetDrive(fso.GetDriveName ( " c : " ) )
s = "Drive " & drv.DriveLetter & " . "
s = s & "Total Space : " & FormatNumber(drv.TotalSize / 1024, 0 )
s = s & " kb " & vbCrLf
s = s & drv.VolumeName & vbCrLf
s = s & " Free Space : " & FormatNumber(drv.FreeSpace / 1024, 0 )
s = s & " kb " & vbCrLf
'
Select Case drv.DriveType
Case 0: t = "Unknown"
Case 1: t = "Removable"
Case 2: t = "Fixed"
Case 3: t = "Network"
Case 4: t = "CD-ROM"
Case 5: t = "RAM Disk"
End Select
'
s = s & t & " Drive " & vbCrLf
s = s & drv.SerialNumber & vbCrLf
s = s & drv.FileSystem & vbCrLf
s = s & drv.Path & vbCrLf
s = s & drv.RootFolder & vbCrLf
MsgBox s
End Sub

Download Vb Program


Working with Folder

-Create a folder : FileSystemObject.CreateFolder
-Delete a folder : Folder.Delete or FileSystemObject.DeleteFolder
-Move a folder : Folder.Move or FileSystemObject.MoveFolder
-Copy a folder : Folder.Copy or FileSystemObject.CopyFolder
-Retrieve the name of a folder : Folder.Name
-Find out whether a folder exists on a drive : FileSystemObject.FolderExists
-Get an instance of an existing folder object : FileSystemObject.GetFolder
-Find out the name of a folder’s parent folder : FileSystemObject.GetParentFolderName
-Find out the Path of System folder : FileSystemObject.GetSpecialFolder


Option Explicit
'Project>References>Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As New FileSystemObject, fldr As Folder, s As String
Set fldr = fso.GetFolder ( " c:\a\b " )
Debug.Print "Folder Name : " & fldr.Name
Debug.Print "Contained on drive : " & fldr.Drive
If fldr.IsRootFolder = True Then
Debug.Print " This folder is a root folder"
Else
Debug.Print " This folder isn't a root folder "
End If
Debug.Print fldr.ParentFolder
Debug.Print fldr.Path
Debug.Print FormatNumber(fldr.Size / 1024, 0) & " KB "
Debug.Print fldr.DateCreated & " Folder Created Date "
Debug.Print fldr.DateLastModified & " Folder Last Modified Date "
Debug.Print fldr.DateLastAccessed & " Folder Last Accessed Date "
End Sub

Private Sub Command2_Click()
Dim fso As New FileSystemObject
fso.CreateFolder ( " c:\1 " )
Debug.Print "Basename = " & fso.GetBaseName( " c:\1 " )
fso.DeleteFolder ( " c:\1 " )
End Sub


.
Working with Files
You can work with files in Visual Basic by the new using the new object-oriented FSO object. Such as Copy, Delete, Move, and open As Text Stream; others; or by using the older existing Functions, such as open, close, file Copy, Get Attar, and so on. Note that you can move copy Or delete files regardless of their file type. There are two major a categories of file manipulation.
  • Creating adding, or removing data; and reading files
  • Moving, copying and deleting
Using the File System object to create a file or manipulate the data contained in an Existing is as simple as using the correct method.

Copy File : Copies one or more file from location to another
Delete File : Deletes a specified file
Move File : Move one or more files from one location to another
OpensTextStream : Open a specified file and returns a TextStrem object that can be Used to read from, write to, or append to the file
Read Line : Reads an entire line (up to, but not including, the newline character) From a Tex Stream file and return s the resulting
ReadAll : Reads an entire Text stream file and returns the resulting string
WrightBlankLine : Writes a specified number of newline characters to a text stream file
Write Line : Writes a specified number of newline character to a Texts tram file
Create Text File : Creates a specified filename and returns a Text Stream object that can Be used to read from or write to file.


Option Explicit
'here i am created folder "a" in C:\ drive
'and under folder "a" also created folder "b"

Private Sub Command1_Click()
Dim fso As New FileSystemObject, txtfile As TextStream
Set txtfile = fso.CreateTextFile( "c:\testfile.txt", True )
txtfile.Write ( "This is a test. " )
txtfile.WriteLine ( " Texting 1, 2, 3." )
txtfile.WriteBlankLines (3)
txtfile.Close
End Sub

Private Sub Command2_Click()
Dim fso As New FileSystemObject
Dim fil1 As File, ts As TextStream
Dim s As String
Set fil1 = fso.GetFile( "c:\testfile.txt" )
'
Set ts = fil1.OpenAsTextStream(ForReading)
s = ts.ReadLine
MsgBox s
ts.Close
'
Set ts = fil1.OpenAsTextStream(ForWriting)
ts.Write ("Hello Samrudhi")
ts.WriteLine (s)
ts.Close
'
Set ts = fil1.OpenAsTextStream(ForReading)
s = ts.ReadLine
MsgBox s
ts.Close
End Sub

Private Sub Command3_Click()
Dim fso As New FileSystemObject, ts As TextStream
Dim fil1 As File, fil2 As File
Set fil1 = fso.GetFile( "c:\testfile.txt" )
fil1.Move ( "c:\a\testfile.txt" )
MsgBox "File moving in folder C:\a "
fil1.Copy ( "c:\a\b\testfile.txt" )
MsgBox "File copy in folder C:\a\b "
Set fil1 = fso.GetFile ( "c:\a\testfile.txt" )
Set fil2 = fso.GetFile ( "c:\a\b\testfile.txt" )
fil1.Delete
fil2.Delete
MsgBox " both file is delete"
End Sub


Monday, October 11, 2010

Visual Basic File System Statement : SetAttr

Purpose : The SetAttr Statement sets the attribut information for DOD files.

Syntax :
SetAttr filename$ , attrbuteBits%

SetAttr "MYFILE.TMP" , vbReadOnly , vbSystem

All file , directories and volumes have an attribute byte associated with then. This information indicates whether the filename is normal, read-only, hidden, system a volume label, a directory, or has been modified since the last backup.

Constants used with the SetAttr function
Constant : Value : Description
vbNormal : 0 : Normal
vbReadOnly : 1 : Read-Only
vbHidden : 2 : Hidden
vbSystem : 4 : System file
vbDirectory : 16 : Directory
vbArchive : 32 : File has changed since last backup

Visual Basic File System Statement : RMDir

Purpose : The RMDir statement removes a subdirectory from a disk.

Syntax :
RmDir dirname$

dirname$ must be in the format of
[ dirve: ] [\] dir [ \ subdir ] [\ subdir ] .......

RmDir "TEST"
RmDir "\TEST"
RmDir "D:\TEST"

Visual Basic File System Statement : Name

Purpose : The Name statement renames a file or directory or moves a file to another directory.

Syntax :
Name oldname As newname

Name "Test_1.Dat " As " Test_2.Dat "

in the example changes the name of the file Test_1.Date in the default directory to Test_2.Dat.

Visual Basic File System Statement : MKDir

Purpose : the MKDir statement creates a subdirectory on the specified drive.

Syntax :
MKDir dirname$

dirname$ must be in format of
[drive:][ \ ] dir [ \ subdir ] [ \ subdir ] ...

MKDir "TEST"
MKDir "\TEST"
MKDir "D:\TEST"

the first example creates the subdirectory TEST below the defult directory on the default drive.
the second line creates the subdirectory TEST below the root directory of the default drive.
and third example creates the subdirectory TEST below the root directory of the D drive.

Visual Basic File System Statement : Kill

Purpose : The Kill Statement deletes the specified file from the disk.

Syntax :
Kill filename$

Kill File1.List ( File1.ListIndex )

in this example erases the file that is currently selected in the FileListBox named File1.

Visual Basic File System Function : GetAttr

Purpose : The GetAttr function lets you determine a file , directory or volume label's attributes.

Syntax :
GetAttr ( fileName$ )

if GetAttr ( fileName$ ) And vbHidden Then checkHidden.Value=CHECKED

Constants used with the GetAttr function
Constant : Value : Description
vbNormal : 0 : Normal
vbReadOnly : 1 : Read-Only
vbHidden : 2 : Hidden
vbSystem : 4 : System file
vbDirectory : 16 : Directory
vbArchive : 32 : File has changed since last backup

Visual Basic File System Function : FileDateTime

Purpose : The FileDateTime function returns a date that indicates the date and time a file was created or last modified.

Syntax :
FileDateTime ( filename$

lastSaved = CVDate ( fileDateTime ( "DATAFILE.MDB" )

Usethe FileDateTime function to datermine when a DOS file was created or last saved. The function returns a date.

Visual Basic File System Statement : FileCopy

Purpose : Use the FileCopy statement to copy a file. this is similer to the DOS copy command.

Syntax :
FileCopy source$ , dest$

FileCopy " temp.tem" , dataFileName

This command copies a xopies a DOS file. the source$ file can be opened for read-only access but must not be opened for write access. You can specify a drive and directory in either of the filenames, but you cannot specify any wildcards. The sample syntax copies the file TEMP.TEM to the filename specifed by the variable dataFileName


Visual Basic File System Function : Dir, Dir$

Purpose : The Dir and Dir$ function return a filename that matches the supplied pattern, file attribute or volume label of a drive . the Dir$ function is retained of backward compatibility.

Syntax :
For the first call to Dir

Dir [ ( pattern$ [ , Attributes ] ) ]

For each successive call for the same pattern :

Dir

Temp$ = Dir ( "*.DOC" )
Do Until Temp$ = ""
i=i+1
Docs ( i ) = Temp$
Temp$ = Dir
Loop
Temp$ = Dir ( "*.*" , vbHidden )
Temp$ = Dir ( "c:\ " , vbDirectory )

Constants used with the Dir function
Constant : Value : Description
vbNormal : 0 : Normal
vbHidden : 2 : Hidden
vbSystem : 4 : System file
vbVolume : 8 : Volume label;
vbDirectory : 16 : Directory