'Msflexgrid Assignment Method
Msflexgrid1.textmatrix (0, 1) = "instructor"
Msflexgrid1.textmatrix (1, 2) = "student"
Msflexgrid1.textmatrix (2, 3) = "principal"
Msflexgrid1.textmatrix (3, 4) = "class teacher"
Msflexgrid1.textmatrix (4, 5) = "Educational Director"
Msflexgrid1.additem 2
Msflexgrid1.additem 4
Msflexgrid1.additem 6
Msflexgrid1.additem 8
''How to retrieve the data of a cell in msflexgrid
Private sub msflexgrid1_click ()
Msgbox msflexgrid1.textmatrix (msflexgrid1.row, msflexgrid1.col), vbokonly, "message"
End sub
Msflexgrid1.row: The current row in the table
Msflexgrid1.col, the current column in the table
'Initialize msflexgrid
Dim I as integer
Msflexgrid1.rows = 0
For I = 0 to 10
Msflexgrid1.additem "AA" + STR (I)
'For I = 0 to 10
Msflexgrid1.col = 1
Msflexgrid1.row = I
Msflexgrid1.text = CSTR (I)
Msflexgrid1.col = 2
Msflexgrid1.row = I
Msflexgrid1.text = CSTR (I) & CSTR (I)
Msflexgrid1.col = 3
Msflexgrid1.row = I
Msflexgrid1.text = CSTR (I) & CSTR (I)
Next I 'implements the msflexgrid control. The single line background is light gray, and the double behavior is blue.
Dim J as integer
With msflexgrid1
. Allowbigselection = true' Set grid Style
. Fillstyle = flexfillrepeat
For J = 0 to. Rows-1
. Row = J:. Col =. fixedcols
. Colsel =. Cols ()-. fixedcols-1
If J mod 2 = 0 then
. Cellbackcolor = & hc0c0c0 'light gray
Else
. Cellbackcolor = vbblue 'Blue
End if
Next J
End with 'insert a background image into the msflexgrid Control Cell
Set msflexgrid1.cellpicture = loadpicture ("F: \ temp \ snow.bmp") how to move the 'msflexgrid control to the last row msflexgrid1.toprow = msflexgrid1.rows-1
'Hide the first line.
'Private sub commandemediclick1 ()
'Msflexgrid1. rowheight (1) = 0
'End sub
'Hide the first column
'Private sub command_click2 ()
'Msflexgrid1. colwidth (1) = 0
'End sub
Right-click msflexgrid, select properties, and select "select mode ".
Click to get the line code
Msgrid1.col is the column number you selected
Msgrid1.row is the row number you selected
Msgrid1.text is the content of the selected cell.
> Assign text to cells in msflexgrid
Msflexgrid. textmatrix (3, 1) = "hello"
> Insert a background image in the msflexgrid control cell.
Set msflexgrid. cellpicture = loadpicture ("c: \ temp \ 1.bmp ")
> Select a Unit
Msflexgrid. Row = 1
Msflexgrid. Col = 1
> Format the selected unit in bold.
Msflexgrid. cellfontbold = true
> Add a new row
Use the additem method to separate the content of different cells with TAB characters
Dim row as string
Row = "AAA" & vbtab & "BBB"
Msflexfrid1.additem row
> How does one implement the msflexgrid control? the background of a singular row is white, and the background of a double row is blue?
Dim I as integer
With msflexgrid1
. Allowbigselection = true' Set grid Style
. Fillstyle = flexfillrepeat
For I = 0 to. Rows-1
. Row = I:. Col =. fixedcols
. Colsel =. Cols ()-. fixedcols-1
If I mod 2 = 0 then
. Cellbackcolor = & hc0c0c0 'light gray
Else
. Cellbackcolor = vbblue 'Blue
End if
Next I
End
> How to move the msflexgrid control to the last row
Msflexgrid1.toprow = msflexgrid1.rows-1
> How to determine whether msflexgrid has a scroll bar
Declare function getscrollrange lib "USER32" (byval hwnd as long, byval nbar as long, lpminpos as long, lpmaxpos as long) as long
Public const sb_horz = & H0
Public const sb_vert = & H1
Public Function vsscroll (mshgrid as mshflexgrid) as Boolean 'judge the visibility of the horizontal scroll bar
Dim I as long
Vsscroll = false
I = getscrollrange (mshgrid. hwnd, sb_horz, lpminpos, lpmaxpos)
If lpmaxpos <> lpminpos then vsscroll = true
End Function
Public Function hescroll (mshgrid as mshflexgrid) as Boolean 'judge the visibility of the vertical scroll bar
Dim I as long
Hescroll = false
I = getscrollrange (mshgrid. hwnd, sb_vert, lpminpos, lpmaxpos)
If lpmaxpos <> lpminpos then hescroll = true
End Function
> When running the program, you want to dynamically increase the number of columns of msflexgrid.
Insert a column after Column 2nd:
Private sub form_load ()
Me. mshflexgrid1.cols = 5
Mshflexgrid1.rows = 2
For I = 0 to me. mshflexgrid1.cols-1
Me. mshflexgrid1.textmatrix (0, I) = I
Me. mshflexgrid1.textmatrix (1, I) = I
Next
End sub
Private sub commandementclick ()
Me. mshflexgrid1.cols = me. mshflexgrid1.cols + 1
Me. mshflexgrid1.colposition (5) = 3
End sub
> Please refer to the use of the alignment function in msflexgrid.
Set msflexgrid1.colalignment (INDEX) = N
> Obtain the selected row in the msflexgrid control.
Msflexgrid1.rowsel is the currently selected row
> How to adjust the column width by code
Ms flexgrid1.colwidth (I) = 4000
Bytes --------------------------------------------------------------------------------------
'Outdatatotext
'Output the content displayed in the msflexgrid control to a text file.
Public sub outdatatotext (Flex as msflexgrid)
Dim s as string
Dim I as integer
Dim J as integer
Dim K as integer
Dim strtemp as string
On Error goto ERT
Me. mousepointer = 11
On Error resume next
Doevents
Dim filenum as integer
Filenum = freefile
Open "D: aa.txt" for output as # filenum
With flex
K =. Rows
For I = 0 to k-1
Strtemp = ""
For J = 0 to. Cols-1
Doevents
Strtemp = strtemp &. textmatrix (I, j )&","
Next J
Print # filenum, left (strtemp, Len (strtemp)-1)
Next I
End
Close # filenum
Me. mousepointer = 0
Msgbox "exported"
ERT:
Msgbox err. Description
Me. mousepointer = 0
End sub
Added the msflexgrid editing function.
Overview
The msflexgrid control does not provide the text editing function. The following example shows how to use a textbox to edit the current grid.
After you press the next key, move the textbox to the current position and activate it. When you press ENTER or move to another grid,
Put the content in textbox into the grid.
Steps
1. Open VB6 and start a new project.
2. Select "parts" in the "project" menu, and select "Microsoft FlexGrid Control..." in the list .."
3. Place a msflexgrid control and a Textbox Control (text1) to form1. Modify the msflexgrid Control name to grid1,
Set the row of grid1, column 4, fixed row, column 0. Set visiable of text1 to false and borderstyle
None (0 ).
4. Add the statement in the form1 code:
Const asc_enter = 13 'press ENTER
Dim grow as integer
Dim gcol as integer
5. Add the code to the grid_keypress process:
Private sub grid1_keypress (keyascii as integer)
'Move the text box to the current grid cell:
Text1.top = grid1.celltop + grid1.top
Text1.left = grid1.cellleft + grid1.left
'Save the position of the grids row and Col for later:
Grow = grid1.row
Gcol = grid1.col
'Make text box same size as current grid cell:
Text1.width = grid1.cellwidth-2 * screen. twipsperpixelx
Text1.height = grid1.cellheight-2 * screen. twipsperpixely
'Transfer the grid cell text:
Text1.text = grid1.text
'Show the text box:
Text1.visible = true
Text1.zorder 0' put text1 at the top!
Text1.setfocus
'Redirect this keypress event to the text box:
If keyascii <> asc_enter then
Sendkeys CHR $ (keyascii)
End if
End sub
6. Add code to the textdeskkeypress process:
Private sub text=keypress (keyascii as integer)
If keyascii = asc_enter then
Grid1.setfocus 'set focus back to grid, see text_lostfocus.
Keyascii = 0 'ignore this keypress.
End if
End sub
7. Add code to the text‑lostfocus process:
Private sub text=lostfocus ()
Dim tmprow as integer
Dim tmpcol as integer
'Save current settings of grid row and Col. This is needed only if
'The focus is set somewhere else in the grid.
Tmprow = grid1.row
Tmpcol = grid1.col
'Set row and Col back to what they were before text=lostfocus:
Grid1.row = grow
Grid1.col = gcol
Grid1.text = text1.text 'transfer text back to grid.
Text1.selstart = 0 'Return caret to beginning.
Text1.visible = false 'Disable text box.
'Return row and Col contents:
Grid1.row = tmprow
Grid1.col = tmpcol
End sub
8. Press F5 to start the test. You can move freely in the grid and press enter to start or end editing.
Several functions using the msflexgrid Control
When using VB to process data display, using tables is a good method. Although the DataGrid can be bound to the data source, there are always some shortcomings, that is
Poor appearance, so sometimes msflexgrid is a good way to display data. The following functions are used to control the msflexgrid program.
(My language skills are limited. Please forgive me)
''Msflexgrid operation function
''Merge Columns
Public Function mergecol (gridobj as object, byval startcol as long, byval endcol as long, byval
Colvalue as string, byval currentrow as long) as Boolean
If startcol> endcol or startcol> gridobj. Cols or currentrow> gridobj. Rows then
Msgbox "sorry, the row and column settings are incorrect! ", Vbokonly, app. Title
Mergecol = false
Exit Function
End if
For I = startcol to endcol
Gridobj. mergecol (I) = true
Gridobj. textarray (faindex (gridobj, currentrow, I) = colvalue
Gridobj. colalignment (I) = flexaligncentercenter
Next I
Gridobj. mergerow (currentrow) = true
Mergecol = true
End Function
''Merge rows
Public Function mergerow (gridobj as object, byval startrow as long, byval endrow as long, byval
Rowvalue as string, byval currentcol as long) as Boolean
If startrow> endrow or startrow> gridobj. Rows or currentcol> gridobj. Cols then
Msgbox "sorry, the row and column settings are incorrect! ", Vbokonly, app. Title
Mergerow = false
Exit Function
End if
For I = startrow to endrow
Gridobj. mergerow (I) = true
Gridobj. textarray (faindex (gridobj, I, currentcol) = rowvalue
Gridobj. colalignment (currentcol) = flexaligncentercenter
Next I
Gridobj. mergecol (currentcol) = true
Mergerow = true
End Function
''CONVERSION INDEX
Public Function faindex (gridobj as object, byval row as integer, byval Col as integer) as long
If row> gridobj. Rows or row <0 or Col> gridobj. Cols or Col <0 then
Msgbox "sorry, the row and column settings are incorrect! ", Vbokonly, app. Title
Faindex =-1
Exit Function
End if
Faindex = row * gridobj. Cols + col
End Function
''Insert row
Public Function setitem (gridobj as object, byval row as integer, byval Col as integer, byval
Setvalue as string) as Boolean
If row> gridobj. Rows or row <0 or Col> gridobj. Cols or Col <0 then
Msgbox "sorry, the row and column settings are incorrect! ", Vbokonly, app. Title
Setitem = false
Exit Function
End if
Gridobj. textarray (faindex (gridobj, row, col) = setvalue
Setitem = true
End Function
''To get the cell value
Public Function getitem (gridobj as object, byval row as integer, byval Col as integer) as string
If row> gridobj. Rows or row <0 or Col> gridobj. Cols or Col <0 then
Msgbox "sorry, the row and column settings are incorrect! ", Vbokonly, app. Title
Getitem = ""
Exit Function
End if
Getitem = gridobj. textarray (faindex (gridobj, row, col ))
End Function
The content of each cell in the msflexgrid control cannot be directly edited by users, but we can use some tips
You can easily implement this editing function to expand the application of msflexgrid (this is a very common function in practical applications ).
You can easily edit the data of the msflexgrid control by following the following steps:
For example, put a text box txtvalue and a msflexgrid control grid on the form.
'Keypress event of the text box Control
Private sub txtvalue_keypress (keyascii as integer)
'Put some processing procedures, such as processing when only a number is entered
Dim I
I = 1
End sub
Private sub txtvalue_change ()
Grid. Text = txtvalue. Text
End sub
'Add the following code to the grid entercell event
Private sub grid_entercell ()
Txtvalue. Text = grid. Text
Txtvalue. selstart = 0
Txtvalue. sellength = Len (txtvalue. Text)
End sub
'When the user inputs data, the keypress event of the text box is called directly.
Private sub grid_keypress (keyascii as integer)
Txtvalue_keypress keyascii
End sub
OK, so an editable msflexgrid control is complete. It's easy !!
Private sub fgrid1_click () 'click msflexgrid
Sphy = trim (fgrid1.textmatrix (fgrid1.row, 0 ))
Delrecord
End sub
Private sub fgrid1_rowcolchange () 'select
Sphy = trim (fgrid1.textmatrix (fgrid1.row, 0 ))
Delrecord
End sub
Private sub deleterecord () 'Delete
Dim I as integer'
Dim ssql as string
Dim response as string
Response = msgbox ("are you sure you want to delete the current record? ", Vbinformation + vbyesno," warning ")
If response = vbyes then
Ssql = "delete from login where loginname = '" & sphy &"'"
Gcnleaguers. Execute (ssql)
Msgbox "deleted successfully! ", Vbinformation + vbokonly," system prompt"
End if
Exit sub
End sub
You may need to refresh it after deletion.
Mshflexgrid1.selectionmode = flexselectionbyrow
Or
Private sub form_load ()
With mshflexgrid1
. Fixedcols = 0
. Cols = 2
. Rows = 0
. Additem "1" & vbtab & "11"
. Additem "2" & vbtab & "22"
. Additem "3" & vbtab & "33"
End
End sub
Private sub mshflexgrid1_rowcolchange ()
Static intprevrow as long
If mshflexgrid1.row <> intprevrow then
Intprevrow = mshflexgrid1.row
Dim I as long
For I = 0 to mshflexgrid1.cols-1
Msgbox mshflexgrid1.textmatrix (intprevrow, I)
Next
End if
End sub
Principle
When you click a cell in msflexgrid to input data, an entercell event is generated.
Box to initialize, enter the content in the current cell, and select all text. When the user needs to press the button for input
The event that calls txtvalue is handled by the text box.
The processing result is synchronized with the current cell of the grid, which makes it as convenient for users to edit cell cells as if they were using text boxes.
It is easy to display multiple lines of text in a grid cell. You only need to change the wordwarp attribute to true.
1. VB6's built-in mshflexgrid only supports display of 2048 records. This problem is solved by installing the VB6 SP6.
2. Use the toprow attribute. This attribute can be used to move the mshflexgrid row display by code. To perform a Scroll display from top to bottom, It is very convenient to use this attribute.
3. Use the code to highlight a line. To select the third line, use the code:
Mshflexgrid1.row = 3
Mshflexgrid1.toprow = 3
Mymshflexgrid. colsel = mshflexgrid1.cols-1
In mshflexgrid, how does one change the color of a line? Option explicit
Public J as long
Private sub rowcolor (I as long, J as long)
'I indicates the current vertex.
Dim N as long 'n' stands for columns
With me. mshflexgrid1
For n = 1 to. Cols-1
. Row = I
. Col = N
. Cellbackcolor = & hc0ffff
If j> 0 and I <> J then
. Row = J
. Cellbackcolor = & hffffff
End if
Next n
J = I
End
End sub
Private sub form_load ()
With me. mshflexgrid1
. Cols = 5
. Rows = 6
End
End sub
Private sub mshflexgrid1_click ()
Call rowcolor (Me. mshflexgrid1.row, J)
Debug. Print J
End sub