PRB: Grid Control's Cell Blank When Using Str$

Last reviewed: June 21, 1995
Article ID: Q80904
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Professional Toolkit for Visual Basic for Windows, version 1.0 - Microsoft Visual Basic programming system for Windows, version 1.0

SYMPTOMS

With the Microsoft Professional Toolkit for Visual Basic Grid control, when you use the Str$ function to store numeric values in a grid cell, the cell appears blank if it is not wide enough to completely display the value.

CAUSE

This behavior occurs because of word wrapping. The Str$ function returns a string that begins with a space character. When this string does not fit in a grid cell, it wraps to the next line, breaking on the leading space so that no text remains on the first line of the cell.

WORKAROUND

To avoid the problem, use Format$ instead of Str$, or Ltrim$ with Str$. To work around the problem, change the assignment to Grid1.Text to one of the following:

   Grid1.Text = Format$(123456)

      -or-

   Grid1.Text = Ltrim$(Str$(123456))

This will eliminate the leading space, and the information in the cell will be displayed up to the width of the cell. You can also increase the width of the cell to allow all characters to be visible.

MORE INFORMATION

Steps to Reproduce Problem

  1. Start Visual Basic or, if Visual Basic is already running, choose New Project from the File menu (ALT, F, N). Form1 is created by default.

  2. From the File menu, choose Add File, and select GRID.VBX to add the Grid tool. The Grid tool appears in the Toolbox.

  3. Place a grid named Grid1 on Form1.

  4. Set the grid properties Cols and Rows each to 4.

  5. Size the grid so that you can see all the cells.

  6. Enter the following code. To enter the code, double-click Grid1, select Click in the Procedure box, and enter the code into the code template.

       Sub Grid1_Click ()
          Grid1.Text = Str$(123456)
          Debug.Print Grid1.Text
       End Sub
    
    

  7. Press F5 to run the program.

  8. Each time you click a cell in Grid1, this code prints "123456" in the Immediate window, but the cell remains blank.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsCus


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.