VB Grid Custom Control Refreshes on All Cell Change Events

ID: Q84584


The information in this article applies to:
  • Microsoft Professional Toolkit for Microsoft Visual Basic programming system for Windows, versions 1.0, 2.0, and 3.0


SUMMARY

The Grid custom control (GRID.VBX) will refresh (update the control's contents) on all change events occurring to cells in the grid. So, for example, when you enter text into a cell in the grid, a refresh of the grid occurs after every letter of a word is entered. This behavior is by design.


MORE INFORMATION

It is normal behavior for the Grid control to refresh whenever a change occurs to a cell contained in it. This is desirable behavior, because it ensures that current information is always displayed in the grid.

However, slowdowns due to the refreshing time can be a problem. If a grid is large enough, it can take a significant amount of time to refresh it. If there is a large number of data items to enter, the wait is compounded. There is no way to toggle the refresh of the grid when text is entered into a cell; it always occurs. However, there are methods to minimize the number of change events that occur to the grid, thus minimizing the wait. Two of these methods are shown below.

Steps to Reproduce Behavior

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


  2. From the File menu, choose Add File. In the Files box, select the GRID.VBX custom control file. The Grid tool appears in the Toolbox.


  3. Add a Grid control to default Form1 by double-clicking its icon in the Toolbox. Also add a text box control in the same manner.


  4. Set the following properties for Grid1: Cols = 10, Rows = 20. Size the grid so that you can see all the columns and rows. Also, set the Text property of Text1 to "" (blank).


  5. Add the following code:
    
       Sub Form_Load ()
          Form1.Show               ' This code fills the grid with ASCII values
          For columns = 0 To 9     ' to show the effect of refreshing Grid1.
             For rows = 0 To 9
                Grid1.Row = rows
                Grid1.Col = columns
                Grid1.Text = Chr$(63 + rows + columns)
             Next rows
          Next columns
          Text1.SetFocus
       End Sub
    
       Sub Text1_KeyPress (KeyAscii As Integer)
          Grid1.Text = Text1.Text  ' This sets the contents of Grid1.Text to
       End Sub                     ' what is entered into Text1. 


  6. Press the F5 key to run the program.

    Enter some text into Text1. Notice how every entry on the keyboard causes the grid to update. You can tell this is occurring by the flickering of the contents of Grid1 on every key press.

    If direct entry of data into a cell is desired, a slight modification to the code above significantly reduces the number of times the grid refreshes. The code below allows entry of text into a text box, and the contents are transferred to a cell in the grid when the user presses the ENTER key. To demonstrate this behavior, change the code in the Text1_KeyPress event to the following:
    
       Sub Text1_KeyPress (KeyAscii As Integer)
          If KeyAscii = 13 Then       ' Did the user press the ENTER key?
             Grid1.Text = Text1.Text  ' Yes - assign Text1.Text to Grid1.Text.
             KeyAscii = 0             ' Suppresses the default "beep" sound.
             Text1.Text = ""          ' Clear the text box for the next entry.
          End If
       End Sub 
    This change filters the input somewhat by only updating Grid1.Text when the user presses the ENTER key. (If you want to change it to some other value, use a different KeyAscii value.) The benefit of this method is that an update only occurs whenever the user presses the ENTER key, not on every key press event.

    Yet another alternative is to first store the data entered in the text box into an array. Then, when data entry is complete, transfer the contents of the array to the grid. This forces all changes to the grid to be done in one refresh, thus reducing the total waiting time required for the grid to refresh. To accomplish this, do the following:


  7. Add a command button to Form1. Set the Caption property to "Place array items into grid".


  8. Add the following code to the general Declarations section of Form1:
    
       Dim Words$(100)
       Dim GridNum As Integer
       ' (Add the following to the Command1 Click event procedure:)
    
       Sub Command1_Click ()
          For y = 0 To (GridNum - 1)
             Grid1.Row = Int(19 * Rnd + 1) ' Sets the row & column to a random
             Grid1.Col = Int(9 * Rnd + 1)  ' place in the grid, and prints the
             Grid1.Text = Words$(y)        ' item there.
          Next y
          Erase Words$             ' Clears the array.
          GridNum = 0              ' Resets the array item counter.
          Text1.SetFocus           ' Sets the focus back to the text box.
       End Sub 


  9. Replace the code in the Text1_KeyPress event of Form1 with the following:
    
       Sub Text1_KeyPress (KeyAscii As Integer)
          If KeyAscii = 13 Then
             Words$(GridNum) = Text1.Text ' Transfers contents of Text1.Text
                                          ' to a string array.
             Text1.Text = ""
             GridNum = GridNum + 1  ' Increments the array item counter to
                                    ' prepare for the next word to be
                                    ' entered.
             Debug.Print GridNum    ' Prints the current record number in
                                    ' the immediate window. (optional)
             KeyAscii = 0
          End If
       End Sub 


  10. Press the F5 key to run the program. Enter a few words, pressing the ENTER key after each word. Notice that the grid does not refresh after the ENTER key is pressed. The items are being placed into an array with each press of the ENTER key. When you are finished, choose Command1 to place the new items in the grid. The grid will refresh only once now, as the new items are randomly placed in the grid.


Additional query words: 1.00 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: August 30, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.