|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Adding A Flex Grid Control To A ProgramThe Program Design Department is calling. Can you whip up a program to display a few tables of data? No problem, you say, Ill use the Microsoft flex grid control. They ask, and how about spreadsheets? You say, no problemflex grids can handle that too. You can add a flex grid to a Visual Basic project easily; just follow these steps:
This gives you a blank flex grid control in your program; the next step is to fill it with data. To start doing that, take a look at the next topic in this chapter.
Working With Data In A Flex Grid ControlYoure writing your new program, SuperDuperDataCrunch, and its time to write the code for the spreadsheet part. You can use a flex grid control herebut how do you insert and work with the data in a flex grid? To see how this works, well build a small spreadsheet example program that adds a column of numbers. This will show how to insert and access data in a flex grid, as well as how to handle text insertion direct from the user in a rudimentary way (well see a better method in the next topic in this chapter). Several flex grid properties will help us here:
We start by adding a flex grid to a form; give it 7 rows in the Rows property and 7 columns in the Cols property. Well begin by labeling the column heads with letters and the row heads with numbers, just as you would see in any spreadsheet program. Flex grids have FixedCols and FixedRows properties, which set the header columns and rows in the flex grid. These columns and rows are meant to label the other columns and rows, and they appear in gray by default (the other cells are white by default). Both FixedCols and FixedRows are set to 1 by default. Well add a column of numbers here, so we can also place labels in the first column of cells, Item 1 to Item 6, and a label at the bottom, Total, to indicate that the bottom row holds the total of the six above. These labels are not necessary, of course, but well add them to show that you can use text as well as numbers in a flex grid. These labels will appear in column 1 of the flex grid, and users can place the data they want to add in column 2. The running sum appears at the bottom of column 2, as shown in Figure 12.13.
To set text in a flex grid cell, you set the Row and Col properties to that location and then place the text in the flex grids Text property. Heres how we set up the row and column labels in MSFlexGrid1 when the form loads:
Sub Form_Load()
Dim Items(6) As String
Dim intLoopIndex As Integer
Items(1) = "Item 1"
Items(2) = "Item 2"
Items(3) = "Item 3"
Items(4) = "Item 4"
Items(5) = "Item 5"
Items(6) = "Total"
For intLoopIndex = 1 To MSFlexGrid1.Rows 1
MSFlexGrid1.Col = 0
MSFlexGrid1.Row = intLoopIndex
MSFlexGrid1.Text = Str(intLoopIndex)
MSFlexGrid1.Col = 1
MSFlexGrid1.Text = Items(intLoopIndex)
Next intLoopIndex
MSFlexGrid1.Row = 0
For intLoopIndex = 1 To MSFlexGrid1.Cols 1
MSFlexGrid1.Col = intLoopIndex
MSFlexGrid1.Text = Chr(Asc("A&") 1 + intLoopIndex)
Next intLoopIndex
MSFlexGrid1.Row = 1
MSFlexGrid1.Col = 1
End Sub
The rows and labels appear as in Figure 12.14.
Weve set up the labels as we want thembut what about reading data when the user types it? We can use the flex grids KeyPress event for that: Sub MSFlexGrid1_KeyPress(KeyAscii As Integer) End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |