Visual Basic 6.0 Grid Tutorial

Written by laura gittins
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Visual Basic 6.0 Grid Tutorial
(Jupiterimages/Comstock/Getty Images)

When designing a software program, using a table with rows and columns can help you display a lot of information in a small amount of space. Visual Basic 6.0 has a component called the FlexGrid control, which allows you to create a grid with as many rows and columns that you need as well as populate each cell with data. You can add a grid to your VB6 project and use it to store information without needing to connect to an external database.

Skill level:

Other People Are Reading


  1. 1

    Open a new project. Click on "Project" on the menu, and then click "Components." Scroll down and click the check box for "Microsoft FlexGrid Control 6.0." Click "OK" to close the dialogue box.

  2. 2

    Double-click the "MSFlexGrid" control in the toolbox to add it to your form. Click the edges to change the size if necessary. Add a text box and two command buttons by double-clicking them in the toolbox. Click and drag them on your form to arrange them with your grid above and the text box followed by both command buttons in a row beneath the grid.

  3. 3

    Click the grid tool. Select the "Name" property in the "Properties" window, and change it to "grdExample." Change the text box name property to "txtData" and the command buttons to "cmdAdd" and "cmdRemove," respectively. Change the caption property for "cmdAdd" to "Add" and "cmdRemove" to "Remove."

  4. 4

    Open the code window. Create a global variable by typing "Dim index As Integer" on the first line. You will use this variable as a counter for the number of rows in your grid.

  5. 5

    Open the "Form_Load()" event. Type the following lines of code:

    index = 0
    With grdExample
        .Cols = 2
        .Rows = 2
        .FocusRect = flexFocusHeavy
        .SelectionMode = flexSelectionFree
        .ColWidth(0) = 250
        .ColWidth(1) = 2000
        .Row = 0
        .Col = 1
        .Text = "Data Value"
    End With

    This code initialises your index variable and adjusts your grid to have two rows and two columns, labelling the first column as "Data Value." You can add more columns or rows by changing the integers in the "Cols" and "Rows" properties and setting their values the same way as you do with existing columns and rows.

  6. 6

    Open the "grdExample_KeyPress(KeyAscii As Integer)" event. Type the following lines of code:

    Select Case KeyAscii
        Case 32 To 126
            grdExample.Text = grdExample.Text & Chr(KeyAscii)
        Case 8        'backspace
            If Len(grdExample.Text) > 0 Then
                grdExample.Text = Left(grdExample.Text, (Len(grdExample.Text) - 1))
            End If
    End Select

    This code allows a user to select a data cell and use his keyboard to type the change information in it.

  7. 7

    Open the "cmdAdd_Click()" event. Type the following lines of code:

    index = index + 1
    If index = 1 Then
        grdExample.Row = 1
        grdExample.Col = 0
        grdExample.Text = index
        grdExample.Col = 1
        grdExample.Text = txtData.Text
        grdExample.AddItem index & vbTab & txtData.Text
    End If

    This function checks to see if you are entering data in the first row. If so, it submits your data from your text box into the existing first row. If not, it increments your index variable and adds a new row, and then submits the data to it.

  8. 8

    Open the "cmdRemove_Click()" event. Type the following lines of code:

    If index > 0 Then
        If index = 1 Then
            grdExample.Row = 1
            grdExample.Col = 0
            grdExample.Text = ""
            grdExample.Col = 1
            grdExample.Text = ""
            grdExample.RemoveItem index
        End If
        index = index - 1
    End If

    This code removes a row of data from your grid. If you only have one row, it will clear the data but keep the blank row in the grid. It also checks to ensure you do not delete rows that do not exist, preventing an error.

  9. 9

    Press "F5" to run your program. Click "Add" to add another row to your grid, or click "Remove" to delete one. Click on one of the data cells and type to enter additional information.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.