----在編程中經常會遇到應用程序中要輸入大量數據的問題,如果能在該程序中實現象電子表格那樣的輸入界面,就可以解決這個問題。
----VB雖提供了Grid網格控件,但其只具有顯示輸出功能,要使其能接受輸入數據,必須對它加以改造,在計算機報刊中的許多文章都對此進行了探討,但所用方法都不能令人滿意,筆者經過摸索,找出以下辦法,供有興趣者參考。
----首先在VB5.0環境下Form1中放上如圖1所示的各控件:Text1;Grid1;
Command1;Command2;Vscroll1;Hscroll1。
圖1Form1及其控件
----雙擊Form1打開Code窗口,編制程序代碼。
Dimin putcolnum,in putrownumAsInteger
PrivateSubCommand2_Click()
End
EndSub
PrivateSubForm_Activate()
Text1.SetFocus'文本框獲得焦點
EndSub
PrivateSubForm_Load()
Grid1.ColWidth(0)=540
Grid1.RowHeight(0)=216'單元格的寬和高
in putcolnum=13
in putrownum=15'表中顯示的列、行數
Grid1.Cols=20
Grid1.Rows=20'表中總的列、行數
HScroll1.Height=300
VScroll1.Width=300'水平和垂直滾動條的高、寬度
Grid1.Height=(in putrownum 1)'
(Grid1.RowHeight(0) Grid1.GridLineWidth*12)
12 HScroll1.Height
'800×600象素時為12;640×480為15
Grid1.Width=(in putcolnum 1)*
(Grid1.ColWidth(0) Grid1.GridLineWidth*12)
12 VScroll1.Width
'表的高、寬度
HScroll1.Width=Grid1.Width-VScroll1.Width
VScroll1.Height=Grid1.Height-HScroll1.Height
'水平和垂直滾動條的寬、高度
HScroll1.Left=Grid1.Left
HScroll1.Top=Grid1.Top Grid1.Height-HScroll1.Height
'水平滾動條的位置
VScroll1.Left=Grid1.Left Grid1.Width-VScroll1.Width
VScroll1.Top=Grid1.Top
'垂直滾動條的位置
HScroll1.Min=1
HScroll1.Max=Grid1.Cols-in putcolnum
VScroll1.Min=1
VScroll1.Max=Grid1.Rows-in putrownum
'水平和垂直滾動條的范圍
Text1.Width=Grid1.ColWidth(0)
Text1.Height=Grid1.RowHeight(0)
'文本框的寬、高度
Text1.Left=Grid1.Left Grid1.ColWidth(0)
Grid1.GridLineWidth*12
Text1.Top=Grid1.Top Grid1.RowHeight(0)
Grid1.GridLineWidth*12
'文本框的位置
'初始化賦值
Text1.Visible=True
ForI=1ToGrid1.Cols-1
Grid1.Col=I
Grid1.Row=0
Grid1.Text=Str(I)
Grid1.Col=0
Grid1.Row=I
Grid1.Text=Str(I)
NextI
Grid1.Col=1
Grid1.Row=1'網格的列、行初始位置
Grid1.SelStartCol=1
Grid1.SelStartRow=1'網格中單元格的列、行初始位置
Grid1.LeftCol=1
Grid1.TopRow=1'網格中左上角的列、行初始位置
EndSub
PrivateSubGrid1_MouseDown(ButtonAsInteger,
ShiftAsInteger,XAsSingle,YAsSingle)
Grid1.Col=Grid1.SelStartCol
Grid1.Row=Grid1.SelStartRow
Text1.Text=Grid1.Text
colnum=Grid1.Col-Grid1.LeftCol 1
rownum=Grid1.Row-Grid1.TopRow 1
Text1.Left=Grid1.Left (Grid1.ColWidth(0)
Grid1.GridLineWidth*12)*colnum
Text1.Top=Grid1.Top (Grid1.RowHeight(0)
Grid1.GridLineWidth*12)*rownum
EndSub
PrivateSubGrid1_MouseUp(ButtonAsInteger,
ShiftAsInteger,XAsSingle,YAsSingle)
Text1.SetFocus
EndSub
PrivateSubHScroll1_Change()
startcol=Grid1.LeftCol
Grid1.LeftCol=HScroll1.Value
Grid1.SelStartCol=Grid1.SelStartCol
Grid1.LeftCol-startcol
Grid1.Col=Grid1.SelStartCol
Text1.Text=Grid1.Text
EndSub
PrivateSubText1_Change()
Grid1.Text=Text1.Text
EndSub
PrivateSubText1_KeyDown(KeyCode
AsInteger,ShiftAsInteger)
SelectCaseKeyCode
Case38'光標向上
Grid1.Text=Text1.Text
IfGrid1.SelStartRow<2Then
IfGrid1.TopRow>1Then
VScroll1.Value=VScroll1.Value-1
Else
Grid1.SelStartRow=1
EndIf
Else
IfGrid1.TopRow=Grid1.SelStartRowThen
VScroll1.Value=VScroll1.Value-1
Else
Grid1.SelStartRow=Grid1.SelStartRow-1
EndIf
EndIf
Grid1.Col=Grid1.SelStartCol
Grid1.Row=Grid1.SelStartRow
colnum=Grid1.Col-Grid1.LeftCol 1
rownum=Grid1.Row-Grid1.TopRow 1
Text1.Text=Grid1.Text
Text1.Left=Grid1.Left (Grid1.ColWidth(0)
Grid1.GridLineWidth*12)*colnum
Text1.Top=Grid1.Top (Grid1.RowHeight(0)
Grid1.GridLineWidth*12)*rownum
ExitSub
Case40'光標向下
Grid1.Text=Text1.Text
IfGrid1.SelStartRow>in putrownum-1Then
IfGrid1.TopRow
VScroll1.Value=VScroll1.Value 1
Else
IfVScroll1.Value=VScroll1.MaxAnd
Grid1.SelStartRow
Grid1.SelStartRow=Grid1.SelStartRow 1
Else
Grid1.SelStartRow=Grid1.Rows-1
EndIf
EndIf
Else
IfGrid1.TopRow=Grid1.SelStartRow-in putrownumThen
VScroll1.Value=VScroll1.Value 1
Else
Grid1.SelStartRow=Grid1.SelStartRow 1
EndIf
EndIf
Grid1.Col=Grid1.SelStartCol
Grid1.Row=Grid1.SelStartRow
colnum=Grid1.Col-Grid1.LeftCol 1
rownum=Grid1.Row-Grid1.TopRow 1
Text1.Text=Grid1.Text
Text1.Left=Grid1.Left (Grid1.ColWidth(0)
Grid1.GridLineWidth*12)*colnum
Text1.Top=Grid1.Top (Grid1.RowHeight(0)
Grid1.GridLineWidth*12)*rownum
ExitSub->