0% found this document useful (0 votes)
26 views

Visual Basic 6 - String Fun.

The document discusses string manipulation functions in Visual Basic 6, including functions to get substrings, find positions, trim whitespace, and convert case. It also covers formatting numbers, dates, and times for display. The document provides examples of manipulating and selecting text in text boxes and lists the Screen and Clipboard objects.

Uploaded by

Saravana Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Visual Basic 6 - String Fun.

The document discusses string manipulation functions in Visual Basic 6, including functions to get substrings, find positions, trim whitespace, and convert case. It also covers formatting numbers, dates, and times for display. The document provides examples of manipulating and selecting text in text boxes and lists the Screen and Clipboard objects.

Uploaded by

Saravana Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Previous

Contents

Next

LESSON 7 - Text, Graphics and Multimedia


Monday, July 25, 2011

1 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Manipulating text
Whenever you are entering data, creating files or databases, you are working with text strings. Text strings contain characters that can be copied, deleted, cut and reassembled but they also have important visual characteristics: size, color, weight, transparency, etc. In this lesson we will look at different ways of manipulating those text strings.
Grockit GMAT Prep www.grockit.com/GMAT-Sample Personalized study plans. Free trial. Sign up now! Search Anonymously Apersee.com Apersee lets you search e-Discovery companies without being tracked. Free GRE Practice Test Shiksha.com/GRE-Practice-Test Online Test Series For GRE Exam. 5000+ Practice Question. Join Now ! Travel & Expense Mgmt www.channelmentor.com Corporate Travel & Expense Workflow and Accounting, Reporting

String functions Here is a list of the basic functions that work with strings: Len(string): returns the length of string, the number of characters it contains. Left(string, number): returns the number of characters specified by number from the left end of string. Right(string, number): returns the number of characters specified by number from the right end of string. Mid(string, position, number): returns the number of characters specified by number starting at character number position from the left end of string. InStr(string1, string2): returns the position of string2 in string1 - returns 0 if string2 is not found in string1. LTrim(string), RTrim(string) and Trim(string): returns string with non-significant spaces removed from the left, the right or both, respectively. LCase(string), UCase(string): returns string converted to lower-case or upper-case, respectively.

2 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Formatting Numbers, Dates and Times The Label control is still the easiest way of displaying the result of calculations. Whatever the answer is, just move it to Label5.Caption and it will appear on the form. Unfortunately, it does not always appear the way you want to see it. No problem if the result is a string but, what if it is a dollar amount or a date of some kind. That will require some formatting of the result before displaying it. We use the Format function: Label5.Caption = Format(result, "formatting characters")

Numbers For example, given that: Dim result As Single result = 3456.7 Label5.Caption = Format(result, "00000.00") Label5.Caption = Format(result, "#####.##") Label5.Caption = Format(result, "##,##0.00") 'displays: 03456.70 'displays: 3456.7 'displays: 3,456.70

3 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Label5.Caption $3,456.70

Format(result,

"$##,##0.00")

'displays:

Here is a list of what the formatting characters mean:

represents a digit, with non-significant leading and trailing zeros represents a digit, without non-significant leading and trailing zeros decimal placeholder thousands separator

# . ,

$ + - ( ) literal character; displayed as space typed

When displaying dollar amounts, it is good practice to always use the 0 placeholder with the decimal so that 10 cents does not come out as $.1 or $0.1 Using the formatting string "$#0.00" ensures that the value follows standard rules and comes out as $0.10.

Dates and Times When working with dates and times, you need to know that there is a function that will obtain the current date and time from the system clock. The function is: Now( ) and you can use it directly as in: Label5.Caption = Now( )

The result is the current date and time formatted according to what you specified in the Windows Control Panel for your system. If you want to format the result, because you don't want to see the time, for example, there are formatting characters for date and time, as there are for numbers. The main characters are:

yy yyyy m mmm mmmm d dd dddd h hh mm ss

year without the century - eg: 00 year with century eg: 2000 month number - eg: 12 short name of month - eg: dec long name of month eg: december day of the month, without zero - eg: 8 day of the month, with zero - eg: 08 name of the day of the week - eg: Monday hour, without zero eg: 7 hour, with zero - eg: 07 minutes - eg: 45 seconds - eg: 55

4 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Thus, if Now( ) is July 8, 2000 , Label5.Caption = Format(Now( ), "dddd, yyyy mmmm dd") returns: Saturday, 2000 July 08

Of course any other date can be formatted for display: Label5.Caption = Format( DateOfBirth, "yyyy-mm-dd")

Named Formats In addition to the formatting string, there are several named formats that can be used to determine the output format. These named formats are VB constants that you call when you need them:

General Number Currency

Number with no thousands separator Thousands separator, two digits to the right of decimal Displays at least one digit to the left and two digits to the right of decimal Thousands separator, at least one digit to the left and two digits to the right of decimal Multiplies by 100, add percent sign to the right Display determined by Control panel settings; displays date and time Long date format specified for system Short date format specified for system

Fixed

Standard

Percent General Date Long Date Short Date

Long time setting specified by system; Long Time includes hours, minutes, seconds Short Time Shows hours and minutes

Dim DateHired As Date DateHired = "1995-10-25" Label5.Caption = Format(DateHired, "Long Date") returns: October 25, 1995

Manipulating blocks of text The TextBox and the ComboBox controls contain several properties which will allow you to manipulate blocks of text, in addition to single characters. If you have to input a large quantity of text in a TextBox, for

5 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

example, you do not want to see it all in a single line. There are 2 properties that you set that will make the data easier to see: MultiLine = True allows you to have several lines of input, all separated by <Enter>. ScrollBars = 2 - Vertical will create scrollbars, useful to read text.

Then there are 3 properties to work with a block of selected text in the control: SelStart an integer number identifying the start of selected text, the position of the first character in the block - starts at 0. SelLength an integer number identifying the number of characters selected in the block. SelText a string containing the selected text. Note that this kind of manipulation is usually done with the mouse. However, you do not have to code for the mouse events. It is automatic - when you select text in a control, the appropriate events, MouseDown, MouseUp and MouseMove, are triggered by the control.

Useful objects: Screen and Clipboard The Screen object represents the complete Windows environment. It allows access to all Forms and Controls. It has 2 important properties that we need: ActiveForm returns the name of the Form currently active. ActiveControl returns the name of the Control that currently has focus. In the example that follows we will use these properties to avoid having to name the form and the control in code. This is a way of implementing re-usability of code, an important design principle we can write code that can be re-used in many applications without having to be re-written. The Clipboard object is the system clipboard that you know from all your other Windows applications. It is the object that temporarily stores text or graphics between applications. In the case of the Clipboard object, it has 3 important methods that we will use: Clear empties the Clipboard. SetText puts the selected text into the Clipboard. GetText gets the contents of the Clipboard.

6 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Grockit GMAT Prep www.grockit.com/GMAT-Sample Personalized study plans. Free trial. Sign up now! Search Anonymously Apersee.com Apersee lets you search e-Discovery companies without being tracked. Free GRE Practice Test Shiksha.com/GRE-Practice-Test Online Test Series For GRE Exam. 5000+ Practice Question. Join Now ! Warranty Chain Management www.Tavant.com Use Tavant Warranty Solution Transform Warranty Management

Example For the purposes of this example, we will use the Registration Form from Lesson 5. We will add a Comment TextBox to the form. This textbox will be multiline, with a vertical scrollbar. Then, we will add items to the menu to allow us to edit the text entered in Comments. We want to be able to Cut, Copy, Paste and Delete blocks of text. To change the Menu, we again call upon the Menu Editor. We add the new functions under the Edit item. To insert a separator bar, just put a single hyphen in the Caption and give it a Name, mnu_sep1, for example. The menu should look like this:

Then we code the menu events. Note that we use the Screen properties exclusively in this example. Even if we are working in a control which is called txt_comments, there is nothing in the code that refers specifically to that control. We can copy this whole section to any form in any application and it will work without a hitch.

7 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Top

Graphics
When working with graphics (pictures, diagrams, images) the first thing to master in the environment is the coordinate system. That is the 2-dimensional grid that defines locations on the screen in terms of (x,y) coordinates. x is the horizontal position as measured from the left edge and y is the vertical position as measured from the top. (0,0) is the upper left-hand corner of the screen, form or other container. By default, all VB measurements are in twips. A twip is 1/20 of a printer's point (of which there are 72 per inch). Thus, there are 1440 twips in an inch, 567 in a centimeter. The measurements refer to printed size; because there are great variations between monitors, sizes may vary. You can change the units of measure yourself by setting the ScaleMode property for objects or controls.

ScaleMode Meaning 0 User-defined. Twips - 1440 per 1 inch. Points - 72 per 2 inch. Pixels - number per 3 inch depends on monitor.

8 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

4 5 6 7

Characters character = 1/6 inch high and 1/12 inch wide. Inches. Millimeters. Centimeters.

Examples: Me.ScaleMode = 5 'sets scale to inches for this form pic_EmployeePic.ScaleMode = 3 'sets scale to pixels for control

Adding Pictures
You can display pictures in 3 places in your application: On a form In a picture box In an image control

The PictureBox control and the Image control are very similar in operation. However the PictureBox is more flexible in its methods and properties and is the one you should use most of the time. There is little use for the Image. In all 3 cases you will display a picture contained in a graphics file (.BMP, .GIF, .JPEG, .WMF, etc). The name of the file containing the picture will be entered in the Picture property of the control or form. In a form, the picture cannot be resized or moved, so it should be of the correct size before you use it. The picture will serve as background and other controls that you put on the form will be displayed over it. The PictureBox's AutoSize property must be set to True, otherwise the control's size will not change to accomodate the picture, as in the example below.

9 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

In the above example the pictures were all added to the controls at design time. You can also insert or remove a picture at run time. You use the LoadPicture function, as in: pic_departmentlogo = LoadPicture("C:\Pictures\acctnglogo.bmp") Removing the picture is done with the LoadPicture function without a file name: pic_departmentlogo = LoadPicture ("" )

Drawing controls
There are 2 controls in the toolbox which allow you to draw directly on the form - the Line control and the Shape control. Both are easy to use and fairly obvious. The main properties of each that have to be manipulated are: BorderColor for the color of the line or shape and BorderStyle to use a solid or dashed line. In addition, the Shape control has: Shape for rectangle, circle, etc., FillColor and FillStyle to determine how the shape will be filled and BackStyle for transparent or opaque.

Top

Multimedia
Multimedia refers to devices other than the screen or the printer to play sounds, watch videos or record music. This is done through the use of the Multimedia control. Don't look for it in the toolbox, it's not there. It is an additional control that you must load. First, create anew form in Project Lesson7 and call it "multimed.frm". Then, in the menu, at Project --> Components, find the item "Microsoft Multimedia Control 6.0" and check the box next

10 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

to it. Hit OK and that's it. The Multimedia control should now appear in your toolbox. If you select the multimedia control and put it down on the form, you will have a button bar like all the ones you've seen on CD players, recorders, etc. In the DeviceType property you specify what type of device this control controls:

DeviceType Device CDAudio CD Audio player Digital audio tape DAT player Overlay Overlay Scanner Scanner Videotape player Vcr and recorder Videodisc Videodisc player Other devices not Other specified

Example: a simple CD player We create a new form in Lesson7 and call it multimed.frm. After adding the Multimedia control to the toolbox, we put a MM control on the form. Since we will only be using the one MM control, we'll leave its name as MMControl1. The only property we have to change at this time is the DeviceType, to tell it that we are using the CD player, so we write CDAudio in DeviceType. We add a few labels to complete the form and we get:

Now we have to write the code to operate the CD player. Before we start to write the code there are a few things to know about the MM control. There is a Track property which contains the number of the current track. But its most important property is called the Command property and it can take on several values that, in fact, operate the device.

11 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

Command value Open Close Eject Play Pause Next

Meaning Opens the device Closes the device Ejects the CD Plays the device Pauses the device Goes to next track Goes to beginning of current track. If used within 3 seconds of most recent Prev, goes to beginning of previous track Initializes recording Saves the open device file Step backward or forward a track Stops the device Step forward through tracks

Prev

Record Save Seek Stop Step

Understand that both Track and Command are run-time properties because they are meaningless at design time. For example, to open the CD player: MMControl1.Command = "Open" 'we assign the value "Open" to the Command property To pause: MMControl1.Command = "Pause" 'we assign the value "Pause" to the Command property Now, as you have seen, the trick is to know with which events to associate the code that has to be written. The first one is fairly obvious: when we load the form, the Form_Load event, we will open the device. Now, one we haven't used before. When we unload the form, we will close the device. The reason is that, once launched, the device will keep on playing, even if the form is closed. So, just click on the Form_Unload event and write the code there. Finally, just to see that things are running smoothly, we will use the StatusUpdate event for the MM control to display the track being played every time the status of MMControl1 changes (change track, pause, play are all status changes). As you will see, once the CD starts playing, you can use the buttons in the MM toolbar to control what it does.

12 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

You may notice that some of the buttons for the CD Player are not used during play. If you want you can hide these buttons from the control by using the (Custom) property. This will open an editor window that will allow you to customize the MMControl.

13 of 14

7/25/2011 7:43 PM

Visual Basic 6 - string functions - date functions - graphics

http://www.profsr.com/vb/vbless07.htm

You might also want to visit this site for Free Visual Basic 6 tutorials and sample source code examples .

Ads by Google

Visual Basic

2011

PLAB Exam Books

HR Exam

If you haven't found the Visual Basic resource you're looking for, use our Google Search box for more information.

Top

Home | Tutorials | Contact | Sitemap | Add URL | Privacy policy

14 of 14

7/25/2011 7:43 PM

You might also like