PRB: Grid Custom Control: Surprising Results when FillStyle=1

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

- Professional Edition of Microsoft Visual Basic for Windows,

  versions 2.0 and 3.0
- Microsoft Professional Toolkit for Microsoft Visual Basic
  Programming System for Windows, version 1.0

SYMPTOMS

When the Grid custom control has its FillStyle property set to 1 (repeat), assignments to the Text and Picture properties store a value in all the cells within the selected region (determined by SelStartCol, SelStartRow, SelEndCol, and SelEndRow). However, the value returned from Text and Picture comes from the current cell (determined by the Col and Row properties).

This behavior can produce surprising results when the current cell is located outside the selected region.

When FillStyle is 0 (single), the Text and Picture properties store to the current cell and retrieve from the current cell.

RESOLUTION

To cause the Text property to return the same value assigned when FillStyle=1, set the current cell location to a cell inside the selected region. For example, use this code:

   Grid1.Text = "hello"
   Grid1.Col = Grid1.SelColStart
   Grid1.Row = Grid1.SelRowStart
   ' Length of Text is 5
   MsgBox "Len(Text)=" + Format$(Len(Grid1.Text))

STATUS

This behavior is by design.

MORE INFORMATION

The CellSelected property returns True (-1) if the current cell is within the grid's selected region; otherwise, CellSelected returns False (0).

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. The Grid tool will appear in the Toolbox.

  3. Select the Grid tool from the Toolbox, and place a grid (Grid1) on Form1.

  4. On the Properties bar (Properties window in Visual Basic version 2.0), set the grid properties Cols and Rows each to 4. In Visual Basic 2.0, you will need to press the F4 key to display the Properties Window, so you can set the Cols and Rows properties.

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

  6. Double-click the form to bring up the Code window. In the Procedure box, select Load. Enter the following code:

       Sub Form_Load ()
          Grid1.FillStyle = 1     ' Repeat.
    
          ' Set selected region.
          Grid1.SelStartCol = 2
          Grid1.SelStartRow = 2
          Grid1.SelEndCol = 3
          Grid1.SelEndRow = 3
    
          ' Set current cell, outside of selected region.
          Grid1.Col = 1
          Grid1.Row = 1
    
          ' Assign to Text.
          Grid1.Text = "hello"
    
          ' Length of Text is 0, not 5.
          Show
          MsgBox "Len(Text)=" + Format$(Len(Grid1.Text))
       End Sub
    
    

  7. Press the F5 key to run the program.


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.