System Objects



*  What are the system objects?
*  Screen object
*  Clipboard object
*  Printer object
*  Debug object
*  App object
System objects allow you to access system resources like Clipboard, and Screen etc.  Visual Basic allows you to access the system resources through properties and methods of the system objects. The following is the list of system objects and what they do. Detailed discussion about each system object will follow.

System Object
Description
Screen
Allows you to access properties of screen such as width, height, fonts available screen etc.
Printer
Allows you to access the default printer.
Clipboard
Allows you to place data on clipboard and take data from clipboard.
App
Lets you access important information regarding the current application.
Debug
Allows you to send output to immediate window at runtime.
Table  11.1: System objects.

The following section will discuss each of the system objects in detail.

Screen Object
Allows you to access screen-related properties. Screen object is accessed using keyword Screen.
The following are the various properties of Screen object.

Property

Description

ActiveControl
Returns the control that currently has focus.
ActiveForm
Returns the form that currently has focus.
FontCount
Returns the number of fonts available for the display device.
Fonts()
This is an array that contains the names of all the available fonts for display device.
MousePointer
Determines the type of mouse pointer to be used.
MouseIcon
Contains the custom mouse icon. This is taken when MousePointer property is set to vbCustom.
Height and Width
Returns the size of the screen in Twips.
TwipsperpixelX , TwipsperpixelY
Return the number of twips per pixel on the screen.

Table 11.2: Screen object properties.

The following snippets of code demonstrate how to use screen object.

To get the list of fonts supported by the display device place them in a list box, enter:

For I  = 0 to screen.fontcount –1
      List1.additem (screen.fonts(I))
Next


To change mouse pointer to a custom mouse pointer, enter:

Screen.mouseicon  = LoadPicture(“arrow.ico”);
Screen.mousepointer = vbCustom

To center the form on the screen, enter the following code in FORM_LOAD event:

Private Sub Form_Load()
Dim x as integer, y as integer

   x = (Screen.Width - Me.Width) / 2
   y = (Screen.Height - Me.Height) / 2
   Me.Move x, y
End Sub

Printer Object
Printer object allows you to print to default printer. The following are the important properties of Printer object.

Property

Meaning

Copies
Specifies the number of copies to be printed. Default is 1.
ColorMode
Determines whether a color printer prints output in color or monochrome. Valid options are: 1 – monochrome, 2 – color. If the printer is monochrome then it ignores this property.
Devicename
Returns the name of the device.
Drivername
Returns the name of the driver
Orientation
Indicates whether documents are printed in portrait or landscape mode.
1 – portrait, 2 – landscape.
Page
Returns the current page number.
PaperSize
Returns or sets a value indicating the paper size for the current printer Please see on-line help for valid options.
PaperBin
Indicates the bin on the printer from which paper is fed when printing. Please see on-line help for valid options.
Printquality
Returns or sets a value indicating the printer resolution. Valid options are:
-1 Draft, -2 Low, -3 Medium, -4 High. 
You can also set this property to dots per inch (DPI), like 300.
Zoom
Returns or sets the percentage by which printed output is to be scaled up or down. Default is 0, which means normal size.
Table 11.3: Properties that are unique to Printer Object

Printer object also has collection of methods. While properties are used to change the settings, methods are used to actually print and control printing operations.

Following are the methods that are specific to Printer object. Remember Printer object has some of the common methods such as Circle, Line etc. But in this section, I will discuss about only the methods that are specific to Printer object. For complete list, please see on-line help.

Method

What it does?

EndDoc
Terminates a print operation sent to the printer using Printer object and releases the document to the print device or spooler. Printing will not start unless you execute EndDoc.
KillDoc
Terminates the current print job immediately.
NewPage
Ends the current pages and advances to the next page.
Print
Prints the given text to printer.
Table 11.4: Methods that are specific to Printer object.

The following are various examples regarding how to use Printer object properties and methods.

To display two lines in draft resolution mode, enter:

Printer.PrintQuality = -1  'draft resolution
Printer.Print "Who is the author of this book?"
Printer.Print "P.Srikanth"
Printer.EndDoc

To display two lines in two different pages with various other setting, enter:

Printer.CurrentX = 0
Printer.CurrentY = 0
Printer.colromode = 2  ' put in color mode
Printer.Orientation = 2 ' landscape
Printer.Zoom = 100       ' double the size
Printer.Print "This is at the uppper-left corner"
Printer.NewPage        ' go to next page
Printer.Print "This is in the second page"
Printer.EndDoc

App object
App object contains important information regarding the current application such as  help file to be used, whether previous instance of this application is running etc.

The following are the important properties of App object. For the complete list of properties, please see on-line help for App object.

Property

Meaning

ExeName
Returns the root name of the executable file (without the extension) that is currently running. If running in the development environment, returns the name of the project
Taskvisible
Returns or sets a value that determines if the application appears in the Windows task list.
HInstance
Returns a handle to the instance of the application. If project is run in IDE, returns the handle of the IDE.
LogMode
Specifies how logging  is to be carried out.
LogPath
Specifies the path and file name to which logging information should be written.
UnAttendedApp
Returns a value that determines if an application will run without any user interface.
Helpfile
Specifies the path and filename of a Help file used by your application to display Help or online documentation.
PrevInstance
Returns a value indicating whether a previous instance of the application is already running.
Startmode
Returns or sets a value that determines whether an application starts as a stand-alone project or as an ActiveX component.
0 – Standalone, 1 - ActiveX component.
Table 11.5:  Properties of App object.

The following are the available methods of App object.

Method

What it does?

Logevent logbuffer, eventtype
Logs an event in the application's log target, which is specified using LogPath property.
Logbuffer is the message to be logged.
EventType is: 1-error, 2-warning, 4 – information.
StartLogging logTarget, logMode
Sets the log target and log mode of an operation.
Table 11.6:Methods of App object.

To test whether application has started as a standalone or as an ActiveX component:

If App.StartMode = 0 then
   ‘do what you want for    standalone application
Else
    'do what you want for ActiveX component
End if

To prevent second instance from starting:

If not IsNull (App.PrevInstance) then
     Unload me
End if
 
Debug Object
Allows you to write to immediate window. This object is meaningful only when project is run in development environment. At run time Debug object is ignored.

The following are the methods available in Debug object.

Method

What it does?

Print
Prints the given text to Immediate window.
Assert
Suspends the execution at the line where method appears if the given condition is false. If the condition is true, it has no effect on execution.
Table 11.7: Methods of Debug object.

To suspend the execution of program if the value of variable amount is more than 10000, enter:

Debug.Assert amount > 10000

To print the value of variable amount to immediate window, enter:

Debug.Print  amount
Note: you can suspend execution of the program using stop method by pressing Ctrl + Break in VB IDE

Clipboard Object
Allows you to access system clipboard. Visual Basic applications can place data on to clipboard so that other applications can receive the data. In the same way it is also possible to receive data that is currently  placed on clipboard. The following are the available methods in clipboard.

Method

What it does?

Settext
Places the given text on the clipboard.
Setdata 
Places the given binary data, such as pictures, on the clipboard.
Gettext
Returns the text that is currently on clipboard.
Getdata
Returns the data that is currently on clipboard. This is used to retrieve binary data such as pictures.
Clear
Clears data that is on clipboard.
Getformat
Returns true if clipboard contains the data of the specified format. Please see on-line help for the list of valid formats.
Table 11.8: methods of Clipboard object.

Sample Application
It is time to consolidate. Let us develop an application that can place data on the clipboard or send the data to printer. The sample application displays the list of available fonts so that user can format the text before printing.  The application also takes care of enabling and disabling options such as cut and paste automatically.

user-interface of the application the following is the menu structure and what each menu item does.

Option

What it does?

Cut
Places the text that is currently selected in text box to clipboard. And removes the selected text from textbox.
Copy
Places the text that is currently selected in textbox to clipboard.
Paste
Copies the text that is currently available in clipboard at the cursor position in textbox. If any text is selected in text box then the text from clipboard replaces the selected text of the textbox.
Table 11.9: Meaning of options in Edit menu.
Let us now understand what these controls on the form do?

Text Box

Allows user to enter the text that can be sent either to clipboard or to printer. It supports multiple lines and also contains scrollbars.
First Combobox
Display the list of fonts available from Screen object. Initially it is set to font “Arial”. Whenever user selects a new font, the text in textbox will be displayed in the new font.

Second Combobox

Allows you to select a size for font. It displays number in the range 6 to 50 with an increment of 2. User is allowed to either select a number from the list. User is also allowed to enter a new value. The text in the textbox will be displayed in the new size.

Print command button

Prints the text that is in textbox in the selected font and with the selected size to default printer.  It uses Printer object to send text to printer.

Creating the sample application
The following are the steps to be taken to develop the sample application.

1.       Create a new project using File-> New Project and select Standard Exe as the type of the project.
2.       Place the required control
3.       Change the properties as shown in the table below.

Object
Property

Value

Form
Caption
System Objects Demo
Label1
Caption
Print Text
Text1
Name
Txtsample

Multiline
True

Scrollbars
3 –both

Text
“ “ ( Null string)
Combo1
Name
Cmbfonts

Style
2-dropdown list
Combo2
Name
Cmbsize
Command1
Name
Cmdprint

Caption
&Print
Command2
Name
CmdQuit

Caption
&Quit

Write code for Form_Load event to add the names of the available fonts using Screen object. Also add numbers in the range 6 to 50 with an increment of 2.


Private Sub Form_Load()
  'Load fonts from Screen objects
  For i = 0 To Screen.FontCount - 1
     Cmbfonts.AddItem Screen.Fonts(i)
  Next
  Cmbfonts.Text = "Arial"    ‘ select Arial as the default font
  'add numbers with increment of 2 to cmbsize
  For i = 6 To 50 Step 2
    cmbsize.AddItem i
  Next
  ‘select 6 as the default size
  cmbsize.ListIndex = 0
End Sub
Listing 11.1: Code for Load event of the form object.

Now let us write code to change the name of the font whenever user selects a new font name in ComboBox.

Private Sub Cmbfonts_Click()
  txtsample.Font.Name = Cmbfonts.Text
End Sub
Listing 11.2: Code for Click event of cmbFonts combo box.

Text property contains the name of the font currently selected. Use Name attribute of Font object to change the name of the font for textbox.

In the same way let us write code to change size of the font whenever user selects a different size using size Combobox. But as this is a dropdown combo box, user can also directly enter text into it. So after user enters the size, the new size should be used to change the size of text in textbox. For this we use LostFocus event of the Combobox. LostFocus event occurs when user is moving out of the Combobox.

So we have to write code for two events of size Combobox. Listing 11.3 shows the code for size combo box (cmbsize).

Private Sub cmbsize_Click()
 txtsample.Font.Size = Val(cmbsize.Text)
End Sub
Private Sub cmbsize_LostFocus()
 txtsample.Font.Size = Val(cmbsize.Text)
End Sub
Listing 11.3: Code for Size ComboBox.

Whenever user clicks on Print command button, the text that is entered by the user in the textbox should be printed using the selected font name and font size. The code in listing 11.4, accomplishes that task.
Private Sub cmdprint_Click()
   ‘ Change font name and font size of the printer object so that
   ‘ text that is printed is printed with those attributes
   Printer.Font.Name = Cmbfonts.Text
   Printer.Font.Size = Val(cmbsize.Text)
   Printer.Print txtsample     ‘ send text to printer
   Printer.EndDoc              ‘ indicate end of printing job
End Sub
Listing 11.4: Code for Print command button
Let us also complete code for quit buttons.

Private Sub cmdquit_Click()
  Unload Me
End Sub
Listing 11.5: Code for Quit button.

At this stage run the project and test whether changing font name in Combobox is changing the font of the text in textbox. Also test whether changing size is changing the size of character in textbox. Also test whether typing size in size Combobox and moving out of it is changing the size of the character.

If all tests are successful then proceed to next section, otherwise find out the bug and debug it. Remember, nobody is a born programmer. Programming is learnt by writing programs. Initially your program may not be successful. But finding out errors and correcting them is all what programming is all about. So do it.

Writing code for Edit menu
Now let us turn our attention to Edit menu. Options in Edit menu are used to place text in clipboard and retrieve text from clipboard.

First let us concentrate on Cut option. When user selects Cut option, the following steps are to be taken.

¨         Place the selected text of the textbox on Clipboard
¨         Delete the text that is selected.

We use SelText property of the textbox. SelText property of the textbox contains the text that is currently selected (highlighted).
Note: If no text is selected then SelText refers to current position in the textbox.

Here is the code for Cut option of edit menu.

Private Sub mnucut_Click()

'copy the selected text on to clipboard
Clipboard.SetText txtsample.SelText
'cut the selected text
txtsample.SelText = ""

End Sub
Listing  11.6: Code for Cut option.

Copy option also does the same as Cut option except that it doesn’t delete the selected text. Here is the code for copy option.

Private Sub mnucopy_Click()
 'copy the selected text on to clipboard
 Clipboard.SetText txtsample.SelText
End Sub
Listing 11.7: Code for Copy option.
Paste option takes the text that is in the clipboard and places it either at the current position, if nothing is selected, or replaces the selected text. Both the purposes can be served using SelText property of textbox. Listing 11.8, shows the code for Paste option.

Private Sub mnupaste_Click()
   ‘ get text from clipboard and replace selected text
   ‘ or place it at the cursor position if not text is selected
   txtsample.SelText = Clipboard.GetText
 
End Sub
Listing 11.8: Code for Paste option.

The next and last step is automatically enabling and disabling options in Edit menu depending upon the context. That means if no text is selected then Cut and Copy options are to be disabled. In the same way if no text is existing in clipboard then Paste option is to be disabled.

That means Cut and Copy are enabled only when text is selected in textbox.

Paste option is enabled only when clipboard contains text.

The following is the code for click event of Edit menu. Click event of Edit menu is the place where you have to enable or disable because, click event occurs before menu is displayed to user.

Note: But you cannot select click event of Edit menu in design mode like you can select for options. Because whenever you click on edit menu the options will be displayed but code window is not invoked. So get into code window and then select mnuEdit as the object and Click as the event and write the following code.

Private Sub mnuedit_Click()
  
' disable all options
mnucut.Enabled = False
mnucopy.Enabled = False
mnupaste.Enabled = False

If txtsample.SelText <> "" Then
    ' enable cut and copy options
    mnucut.Enabled = True
    mnucopy.Enabled = True
End If

‘ check whether clipboard has any text

 If Clipboard.GetFormat(vbCFText) Then
     mnupaste.Enabled = True
 End If

End Sub
Listing 11.9: Code for Edit menu.

GetFormat method of Clipboard object returns true if data in the clipboard is of the specified type. Here we are checking whether clipboard has any text, using  vbCFText constant. For the list of available constants and corresponding numeric values, please see on-line help for GetFormat method of Clipboard object.

System objects are useful in Visual Basic. They are used to access system devices. App does not refer to any device but it contains information regarding application.
Post a Comment

System Objects



*  What are the system objects?
*  Screen object
*  Clipboard object
*  Printer object
*  Debug object
*  App object
System objects allow you to access system resources like Clipboard, and Screen etc.  Visual Basic allows you to access the system resources through properties and methods of the system objects. The following is the list of system objects and what they do. Detailed discussion about each system object will follow.

System Object
Description
Screen
Allows you to access properties of screen such as width, height, fonts available screen etc.
Printer
Allows you to access the default printer.
Clipboard
Allows you to place data on clipboard and take data from clipboard.
App
Lets you access important information regarding the current application.
Debug
Allows you to send output to immediate window at runtime.
Table  11.1: System objects.

The following section will discuss each of the system objects in detail.

Screen Object
Allows you to access screen-related properties. Screen object is accessed using keyword Screen.
The following are the various properties of Screen object.

Property

Description

ActiveControl
Returns the control that currently has focus.
ActiveForm
Returns the form that currently has focus.
FontCount
Returns the number of fonts available for the display device.
Fonts()
This is an array that contains the names of all the available fonts for display device.
MousePointer
Determines the type of mouse pointer to be used.
MouseIcon
Contains the custom mouse icon. This is taken when MousePointer property is set to vbCustom.
Height and Width
Returns the size of the screen in Twips.
TwipsperpixelX , TwipsperpixelY
Return the number of twips per pixel on the screen.

Table 11.2: Screen object properties.

The following snippets of code demonstrate how to use screen object.

To get the list of fonts supported by the display device place them in a list box, enter:

For I  = 0 to screen.fontcount –1
      List1.additem (screen.fonts(I))
Next


To change mouse pointer to a custom mouse pointer, enter:

Screen.mouseicon  = LoadPicture(“arrow.ico”);
Screen.mousepointer = vbCustom

To center the form on the screen, enter the following code in FORM_LOAD event:

Private Sub Form_Load()
Dim x as integer, y as integer

   x = (Screen.Width - Me.Width) / 2
   y = (Screen.Height - Me.Height) / 2
   Me.Move x, y
End Sub

Printer Object
Printer object allows you to print to default printer. The following are the important properties of Printer object.

Property

Meaning

Copies
Specifies the number of copies to be printed. Default is 1.
ColorMode
Determines whether a color printer prints output in color or monochrome. Valid options are: 1 – monochrome, 2 – color. If the printer is monochrome then it ignores this property.
Devicename
Returns the name of the device.
Drivername
Returns the name of the driver
Orientation
Indicates whether documents are printed in portrait or landscape mode.
1 – portrait, 2 – landscape.
Page
Returns the current page number.
PaperSize
Returns or sets a value indicating the paper size for the current printer Please see on-line help for valid options.
PaperBin
Indicates the bin on the printer from which paper is fed when printing. Please see on-line help for valid options.
Printquality
Returns or sets a value indicating the printer resolution. Valid options are:
-1 Draft, -2 Low, -3 Medium, -4 High. 
You can also set this property to dots per inch (DPI), like 300.
Zoom
Returns or sets the percentage by which printed output is to be scaled up or down. Default is 0, which means normal size.
Table 11.3: Properties that are unique to Printer Object

Printer object also has collection of methods. While properties are used to change the settings, methods are used to actually print and control printing operations.

Following are the methods that are specific to Printer object. Remember Printer object has some of the common methods such as Circle, Line etc. But in this section, I will discuss about only the methods that are specific to Printer object. For complete list, please see on-line help.

Method

What it does?

EndDoc
Terminates a print operation sent to the printer using Printer object and releases the document to the print device or spooler. Printing will not start unless you execute EndDoc.
KillDoc
Terminates the current print job immediately.
NewPage
Ends the current pages and advances to the next page.
Print
Prints the given text to printer.
Table 11.4: Methods that are specific to Printer object.

The following are various examples regarding how to use Printer object properties and methods.

To display two lines in draft resolution mode, enter:

Printer.PrintQuality = -1  'draft resolution
Printer.Print "Who is the author of this book?"
Printer.Print "P.Srikanth"
Printer.EndDoc

To display two lines in two different pages with various other setting, enter:

Printer.CurrentX = 0
Printer.CurrentY = 0
Printer.colromode = 2  ' put in color mode
Printer.Orientation = 2 ' landscape
Printer.Zoom = 100       ' double the size
Printer.Print "This is at the uppper-left corner"
Printer.NewPage        ' go to next page
Printer.Print "This is in the second page"
Printer.EndDoc

App object
App object contains important information regarding the current application such as  help file to be used, whether previous instance of this application is running etc.

The following are the important properties of App object. For the complete list of properties, please see on-line help for App object.

Property

Meaning

ExeName
Returns the root name of the executable file (without the extension) that is currently running. If running in the development environment, returns the name of the project
Taskvisible
Returns or sets a value that determines if the application appears in the Windows task list.
HInstance
Returns a handle to the instance of the application. If project is run in IDE, returns the handle of the IDE.
LogMode
Specifies how logging  is to be carried out.
LogPath
Specifies the path and file name to which logging information should be written.
UnAttendedApp
Returns a value that determines if an application will run without any user interface.
Helpfile
Specifies the path and filename of a Help file used by your application to display Help or online documentation.
PrevInstance
Returns a value indicating whether a previous instance of the application is already running.
Startmode
Returns or sets a value that determines whether an application starts as a stand-alone project or as an ActiveX component.
0 – Standalone, 1 - ActiveX component.
Table 11.5:  Properties of App object.

The following are the available methods of App object.

Method

What it does?

Logevent logbuffer, eventtype
Logs an event in the application's log target, which is specified using LogPath property.
Logbuffer is the message to be logged.
EventType is: 1-error, 2-warning, 4 – information.
StartLogging logTarget, logMode
Sets the log target and log mode of an operation.
Table 11.6:Methods of App object.

To test whether application has started as a standalone or as an ActiveX component:

If App.StartMode = 0 then
   ‘do what you want for    standalone application
Else
    'do what you want for ActiveX component
End if

To prevent second instance from starting:

If not IsNull (App.PrevInstance) then
     Unload me
End if
 
Debug Object
Allows you to write to immediate window. This object is meaningful only when project is run in development environment. At run time Debug object is ignored.

The following are the methods available in Debug object.

Method

What it does?

Print
Prints the given text to Immediate window.
Assert
Suspends the execution at the line where method appears if the given condition is false. If the condition is true, it has no effect on execution.
Table 11.7: Methods of Debug object.

To suspend the execution of program if the value of variable amount is more than 10000, enter:

Debug.Assert amount > 10000

To print the value of variable amount to immediate window, enter:

Debug.Print  amount
Note: you can suspend execution of the program using stop method by pressing Ctrl + Break in VB IDE

Clipboard Object
Allows you to access system clipboard. Visual Basic applications can place data on to clipboard so that other applications can receive the data. In the same way it is also possible to receive data that is currently  placed on clipboard. The following are the available methods in clipboard.

Method

What it does?

Settext
Places the given text on the clipboard.
Setdata 
Places the given binary data, such as pictures, on the clipboard.
Gettext
Returns the text that is currently on clipboard.
Getdata
Returns the data that is currently on clipboard. This is used to retrieve binary data such as pictures.
Clear
Clears data that is on clipboard.
Getformat
Returns true if clipboard contains the data of the specified format. Please see on-line help for the list of valid formats.
Table 11.8: methods of Clipboard object.

Sample Application
It is time to consolidate. Let us develop an application that can place data on the clipboard or send the data to printer. The sample application displays the list of available fonts so that user can format the text before printing.  The application also takes care of enabling and disabling options such as cut and paste automatically.

user-interface of the application the following is the menu structure and what each menu item does.

Option

What it does?

Cut
Places the text that is currently selected in text box to clipboard. And removes the selected text from textbox.
Copy
Places the text that is currently selected in textbox to clipboard.
Paste
Copies the text that is currently available in clipboard at the cursor position in textbox. If any text is selected in text box then the text from clipboard replaces the selected text of the textbox.
Table 11.9: Meaning of options in Edit menu.
Let us now understand what these controls on the form do?

Text Box

Allows user to enter the text that can be sent either to clipboard or to printer. It supports multiple lines and also contains scrollbars.
First Combobox
Display the list of fonts available from Screen object. Initially it is set to font “Arial”. Whenever user selects a new font, the text in textbox will be displayed in the new font.

Second Combobox

Allows you to select a size for font. It displays number in the range 6 to 50 with an increment of 2. User is allowed to either select a number from the list. User is also allowed to enter a new value. The text in the textbox will be displayed in the new size.

Print command button

Prints the text that is in textbox in the selected font and with the selected size to default printer.  It uses Printer object to send text to printer.

Creating the sample application
The following are the steps to be taken to develop the sample application.

1.       Create a new project using File-> New Project and select Standard Exe as the type of the project.
2.       Place the required control
3.       Change the properties as shown in the table below.

Object
Property

Value

Form
Caption
System Objects Demo
Label1
Caption
Print Text
Text1
Name
Txtsample

Multiline
True

Scrollbars
3 –both

Text
“ “ ( Null string)
Combo1
Name
Cmbfonts

Style
2-dropdown list
Combo2
Name
Cmbsize
Command1
Name
Cmdprint

Caption
&Print
Command2
Name
CmdQuit

Caption
&Quit

Write code for Form_Load event to add the names of the available fonts using Screen object. Also add numbers in the range 6 to 50 with an increment of 2.


Private Sub Form_Load()
  'Load fonts from Screen objects
  For i = 0 To Screen.FontCount - 1
     Cmbfonts.AddItem Screen.Fonts(i)
  Next
  Cmbfonts.Text = "Arial"    ‘ select Arial as the default font
  'add numbers with increment of 2 to cmbsize
  For i = 6 To 50 Step 2
    cmbsize.AddItem i
  Next
  ‘select 6 as the default size
  cmbsize.ListIndex = 0
End Sub
Listing 11.1: Code for Load event of the form object.

Now let us write code to change the name of the font whenever user selects a new font name in ComboBox.

Private Sub Cmbfonts_Click()
  txtsample.Font.Name = Cmbfonts.Text
End Sub
Listing 11.2: Code for Click event of cmbFonts combo box.

Text property contains the name of the font currently selected. Use Name attribute of Font object to change the name of the font for textbox.

In the same way let us write code to change size of the font whenever user selects a different size using size Combobox. But as this is a dropdown combo box, user can also directly enter text into it. So after user enters the size, the new size should be used to change the size of text in textbox. For this we use LostFocus event of the Combobox. LostFocus event occurs when user is moving out of the Combobox.

So we have to write code for two events of size Combobox. Listing 11.3 shows the code for size combo box (cmbsize).

Private Sub cmbsize_Click()
 txtsample.Font.Size = Val(cmbsize.Text)
End Sub
Private Sub cmbsize_LostFocus()
 txtsample.Font.Size = Val(cmbsize.Text)
End Sub
Listing 11.3: Code for Size ComboBox.

Whenever user clicks on Print command button, the text that is entered by the user in the textbox should be printed using the selected font name and font size. The code in listing 11.4, accomplishes that task.
Private Sub cmdprint_Click()
   ‘ Change font name and font size of the printer object so that
   ‘ text that is printed is printed with those attributes
   Printer.Font.Name = Cmbfonts.Text
   Printer.Font.Size = Val(cmbsize.Text)
   Printer.Print txtsample     ‘ send text to printer
   Printer.EndDoc              ‘ indicate end of printing job
End Sub
Listing 11.4: Code for Print command button
Let us also complete code for quit buttons.

Private Sub cmdquit_Click()
  Unload Me
End Sub
Listing 11.5: Code for Quit button.

At this stage run the project and test whether changing font name in Combobox is changing the font of the text in textbox. Also test whether changing size is changing the size of character in textbox. Also test whether typing size in size Combobox and moving out of it is changing the size of the character.

If all tests are successful then proceed to next section, otherwise find out the bug and debug it. Remember, nobody is a born programmer. Programming is learnt by writing programs. Initially your program may not be successful. But finding out errors and correcting them is all what programming is all about. So do it.

Writing code for Edit menu
Now let us turn our attention to Edit menu. Options in Edit menu are used to place text in clipboard and retrieve text from clipboard.

First let us concentrate on Cut option. When user selects Cut option, the following steps are to be taken.

¨         Place the selected text of the textbox on Clipboard
¨         Delete the text that is selected.

We use SelText property of the textbox. SelText property of the textbox contains the text that is currently selected (highlighted).
Note: If no text is selected then SelText refers to current position in the textbox.

Here is the code for Cut option of edit menu.

Private Sub mnucut_Click()

'copy the selected text on to clipboard
Clipboard.SetText txtsample.SelText
'cut the selected text
txtsample.SelText = ""

End Sub
Listing  11.6: Code for Cut option.

Copy option also does the same as Cut option except that it doesn’t delete the selected text. Here is the code for copy option.

Private Sub mnucopy_Click()
 'copy the selected text on to clipboard
 Clipboard.SetText txtsample.SelText
End Sub
Listing 11.7: Code for Copy option.
Paste option takes the text that is in the clipboard and places it either at the current position, if nothing is selected, or replaces the selected text. Both the purposes can be served using SelText property of textbox. Listing 11.8, shows the code for Paste option.

Private Sub mnupaste_Click()
   ‘ get text from clipboard and replace selected text
   ‘ or place it at the cursor position if not text is selected
   txtsample.SelText = Clipboard.GetText
 
End Sub
Listing 11.8: Code for Paste option.

The next and last step is automatically enabling and disabling options in Edit menu depending upon the context. That means if no text is selected then Cut and Copy options are to be disabled. In the same way if no text is existing in clipboard then Paste option is to be disabled.

That means Cut and Copy are enabled only when text is selected in textbox.

Paste option is enabled only when clipboard contains text.

The following is the code for click event of Edit menu. Click event of Edit menu is the place where you have to enable or disable because, click event occurs before menu is displayed to user.

Note: But you cannot select click event of Edit menu in design mode like you can select for options. Because whenever you click on edit menu the options will be displayed but code window is not invoked. So get into code window and then select mnuEdit as the object and Click as the event and write the following code.

Private Sub mnuedit_Click()
  
' disable all options
mnucut.Enabled = False
mnucopy.Enabled = False
mnupaste.Enabled = False

If txtsample.SelText <> "" Then
    ' enable cut and copy options
    mnucut.Enabled = True
    mnucopy.Enabled = True
End If

‘ check whether clipboard has any text

 If Clipboard.GetFormat(vbCFText) Then
     mnupaste.Enabled = True
 End If

End Sub
Listing 11.9: Code for Edit menu.

GetFormat method of Clipboard object returns true if data in the clipboard is of the specified type. Here we are checking whether clipboard has any text, using  vbCFText constant. For the list of available constants and corresponding numeric values, please see on-line help for GetFormat method of Clipboard object.

System objects are useful in Visual Basic. They are used to access system devices. App does not refer to any device but it contains information regarding application.
Post a Comment