Microsoft KB Archive/43527

= Basic Program to Read Characters from the Screen into a String =

Article ID: 43527

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q43527



SUMMARY
The program below demonstrates how to use the PEEK statement to read text that is printed on the screen into a Basic string variable. The program PEEKs video memory (at segment B000h) to read the characters from the screen.

(Note: An easier method to read characters from the screen is to use the SCREEN function, which reads a character's ASCII value or its color from a specified screen location.)



MORE INFORMATION
For a color display, you must change the address of the DEF SEG statement in the function from B000h to B800h. Note that any attributes (blinking, highlight, and so on) will be lost when the text is put into the string.

Example Program
' To try this example in VBDOS.EXE: ' 1. From the File menu, choose New Project. ' 2. Copy the code example to the Code window. ' 3. Press F5 to run the program.

DECLARE FUNCTION readstr$ (xline%, xstart%, xstop%) CLS PRINT &quot;Now is the time&quot;        ' Put some text on the screen. PRINT &quot;for all good men&quot; PRINT &quot;to come to the aid&quot; PRINT &quot;of their country.&quot; a$ = readstr$(1, 1, 6)         ' Read the first six characters of the ' first line (that is, &quot;Now is&quot;). b$ = readstr$(4, 10, 18)       ' Read the last nine characters (10-18) ' on line four (that is, &quot;country.&quot;). LOCATE 10, 1 PRINT &quot;A$ = &quot;; a$ PRINT &quot;B$ = &quot;; b$

' readstr$ takes three integers and returns a string. ' The integers are:   - The line on the screen to read from. '                     - The starting character position. '                     - The ending character position. ' It returns a string containing the characters on the screen ' on the specified line between the start and stop positions. ' FUNCTION readstr$ (xline%, xstart%, xstop%) DEF SEG = &HB000 ' Changes segment for PEEK to B800h for color ' display. fstart% = (160 * (xline% - 1)) + (2 * (xstart% - 1)) fstop% = (160 * (xline% - 1)) + (2 * (xstop% - 1)) FOR x% = fstart% TO fstop% STEP 2 s$ = s$ + CHR$(PEEK(x%)) NEXT x% readstr$ = s$  DEF SEG    ' Restores DS to default to Basic's DGROUP segment. END FUNCTION

Additional query words: VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 7.00 7.10 6.00 6.00b

Keywords: KB43527

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.