----Grid control is one of the most common controls in Visual Basic, which is available from VB3.0 to VB5.0. It is also one of the favorite tools for VB enthusiasts. It can be used to display and browse data in the form of tables, especially for database applications. Direct binding can display and browse database information. However, the fly in the ointment is that Grid does not have editing and printing functions, and the positions of columns cannot be exchanged with each other. The author has tried to add these functions to Grid to make it even better and more powerful. The improvement method and source program are given below. Readers only need to write the source program according to the steps to make your Grid have printing function. The author debugged this program using VB5.0 in the HP5/100Window95 environment and passed it.
----There are three ways to add printing to the Grid control: 1. Print the control directly, 2. realize the printing function through PRinter, and 3. realize printing by calling MS-Word and MS-Excel.
----First, open an application, add the DATA control DATA1 in FORM1, set the CONNECT attribute of DATA1 to dBASEIII, and then set the DATABASENAME attribute to D:/PJXM.DBF. Then add MSFLEXGRID space GRID1 in FORM1, and set the DATASOURCE attribute of GRID1 to DATA1. In this way, the information of the database PJXM.DBF will be displayed in GRID1.
----Method 1: Directly print the form method, add a command button (command) in FORM1, set the CAPTION attribute to direct printing, and then write the following code:
Subcommand_click
Form1.printform
Endsub
----In this way, the data of GRID1 can be printed out by printing form FORM1. Unfortunately, only the data part displayed in GRID1 can be printed. If it is not displayed, it cannot be printed. Moreover, this printing method is very like a screen. Hard copy prints other controls as well. It also cannot flexibly control fonts, etc.
----Method 2: Print through PRINTER. this method
----1. Add the print command button (command1) and function (prnt1) to realize the printing function. Write the following code. The reader can write it into a standard function or process with slight changes.
Functionprnt1(xAsInteger,yAsInteger,
fontAsSingle,txtAsString)
printer.CurrentX=x
printer.CurrentY=y
printer.FontBold=False
printer.FontSize=font
printer.Printtxt
EndFunction
Subcommand1_click
DimfntAsSingle
Dimppasinteger
Pp=0'Set the starting page number 0
Dimstry,strx,strx1,stry1,linw,page1,pAsInteger
Statica(8)AsInteger' defines the number of columns to print
ss$="Internal Settlement Deposit Statement"'define header
kan=0
Fori=0To8
a(i)=1500' defines the width of each column
kan=kan a(i)'calculates the total width of the table
Next
page1=50' defines the number of lines per page
strx=200
strx1=200' defines the starting position in the X direction
stry=1400
stry1=1400' defines the starting position in the Y direction
linw=240' defines line width
fnt=8'define font size
printer.fontname="宋体"'define font
dd=prnt1(4000,700,18,ss$)'Print title
printer.Line(strx-50,stry-30)
-(strx kan-10,stry-30)
Forj=0Togridrow-1'gridrow is the number of lines to be printed
grid1.row=j
strx=strx1
printer.Line(strx-50,stry-30)
-(strx kan-10,stry-30)
p=p 1
Fori=0To8
grid1.col=i
dd=prnt1(strx,stry,fnt,grid1.text)
strx=strx a(i)
Next
Ifp>page1Then'nextpage
p=0
strx=strx1
'linelastline
printer.Line(strx-50,stry linw)
-(strx kan-10,stry linw)
stry=stry1
'linecol
Forn=0To8
printer.Line(strx-30,stry-30)
-(strx-30,stry (page1 2)*linw)
strx=strx a(n)
Next
printer.Line(strx-30,stry-30)
-(strx-30,stry (page1 2)*linw)
pp=pp 1
foot$="th" cstr(pp) "page"
dd=prnt1(strx-30-1000,stry (page1 2)
*linw 100,10,foot$)'Print page corner number
printer.NewPage'nextpage
dd=prnt1(4000,700,18,ss$)'Print title
strx=strx1
stry=stry1
printer.Line(strx-50,stry-30)-
(strx kan-10,stry-30)'printfirstrow
Else
stry=stry linw
EndIf
Next
st=stry
Ifp<page1Then' mark the remaining blank lines on the last page
Foro=pTopage1 1
strx=strx1
printer.Line(strx-50,stry-30)
-(strx kan-10,stry-30)
stry=stry linw
Next
EndIf
stry=stry1
strx=strx1
stry=stry1'linecol
Forn=0To8
printer.Line(strx-30,stry-30)-
(strx-30,stry (page1 2)*linw)
strx=strx a(n)
Next
printer.Line(strx-30,stry-30)-
(strx-30,stry (page1 2)*linw)
pp=pp 1
foot$="th" cstr(pp) "page"
dd=prnt1(strx-30-1000,stry (page1 2)
*linw 100,10,foot$)'Print page corner number
printer.EndDoc'End of printing
Endsub
----This method can easily adjust fonts, font styles, line shapes, pages, paper sizes, etc. through flexible programming. Can print out satisfactory results. If MICROSOFTWORD and MICROEXCEL are installed on your computer, the most exciting way to use it is to send the GRID table to MICROSOFTWORD and MICROEXCEL through VB. Generate MICROSOFTWORD and MICROEXCEL tables. In this way, you can make full use of the printing and editing functions of MICROSOFTWORD and MICROEXCEL to print more ideal results. Introduced one by one below.
----Method 3: Print by generating MICROSOFTWORD form
----1. Write in declaration:
DimmswordAsObject
----2. Add the print command button (command2), and set CAPTION to "Generate WORD table"
"Format", write the following code,
PrivateSubcommand2_Click()
screen.MousePointer=11
Setmsword=CreateObject("word.basic")
DimAppID,ReturnValue
appID=Shell("d:/office97/office/WINWORD.EXE",1)
'RunMicrosoftWord.
msword.AppActivate"MicrosoftWord"
'msword.AppActivate"MicrosoftWord",1
full
Screen.MousePointer=0
EndSub
----2. Write the following process full()
Subfull()
DimiAsInteger,jAsInteger,
colAsInteger,rowAsInteger
DimcellcontentAsString
Me.Hide
cols=4'The number of columns in the table
row=gridrow' prints the number of rows in the table
msword.filenewdefault
msword.MsgBox"Creating MS_WORD report,
Please wait...","",-1
msword.leftpara
msword.screenupdating0
msword.tableinserttable,col,row,,,16,167
msword.startofdocument
forj=0togridrow'The number of rows in the table
grid1.row=j
Fori=1Tocols
Gri1d.col=i
IfIsNull(grid1.text)Then
cellcontent$=""
Else
cellcontent$=grid1.text
EndIf
msword.Insertcellcontent$
msword.nextcell
Nexti
Nextj
msword.tabledeleterow
msword.startofdocument
msword.tableselectrow
msword.tableheadings1
msword.centerpara
'msword.startdocument
msword.screenrefresh
msword.screenupdating1
msword.MsgBox"End","",-1
Me.Show
EndSub
----Method 4: Print the form by sending it to MICROSOFTEXCEL
----1. Add the print command button (command3), and set CAPTION to "Generate EXCEL table"
"Format", write the following code
PrivateSubcommand3_Click()
DimiAsInteger
DimjAsInteger
DimxlAppAsExcel.application
DimxlBookAsExcel.Workbook
DimxlSheetAsExcel.Worksheet
SetxlApp=CreateObject("Excel.Application")
xlApp.Visible=True
'SetxlBook=xlApp.Workbooks.Add
'OnErrorResumeNext
SetxlBook=xlApp.Workbooks.Add'Open("d:/text2.xls")
SetxlSheet=xlBook.Worksheets(1)
xlSheet.Cells(6,1)="i"
Fori=0Togridrow
grid1.Row=i
Forj=0To6
Grid1.Col=j
IfIsNull(Grid1.Text)=FalseThen
xlSheet.Cells(i 5,j 1)=Grid1.Text
EndIf
Nextj
Nexti
ExitSub->