WEB开发网
开发学院WEB开发综合 用VB实现类电子表格的数据录入 阅读

用VB实现类电子表格的数据录入

 2006-02-27 11:44:19 来源:WEB开发网   
核心提示:在编程中经常会遇到应用程序中要输入大量数据的问题,如果能在该程序中实现象电子表格那样的输入界面,用VB实现类电子表格的数据录入,就可以解决这个问题,VB虽提供了Grid网格控件,首先在VB5.0环境下Form1中放上如图1所示的各控件:Text1;Grid1;Command1;Command2;Vscroll1;Hsc
----在编程中经常会遇到应用程序中要输入大量数据的问题,如果
能在该程序中实现象电子表格那样的输入界面,就可以解决这个问
题。

----VB虽提供了Grid网格控件,但其只具有显示输出功能,要使其
能接受输入数据,必须对它加以改造,在计算机报刊中的许多文章都
对此进行了探讨,但所用方法都不能令人满意,笔者经过摸索,找出
以下办法,供有兴趣者参考。

----首先在VB5.0环境下Form1中放上如图1所示的各控件:Text1;Grid1;
Command1;Command2;Vscroll1;Hscroll1。
图1Form1及其控件

----双击Form1打开Code窗口,编制程序代码。

Diminputcolnum,inputrownumAsInteger
PRivateSubCommand2_Click()
End
EndSub

PrivateSubForm_Activate()
Text1.SetFocus'文本框获得焦点
EndSub


PrivateSubForm_Load()
Grid1.ColWidth(0)=540
Grid1.RowHeight(0)=216'单元格的宽和高

inputcolnum=13
inputrownum=15'表中显示的列、行数

Grid1.Cols=20
Grid1.Rows=20'表中总的列、行数

HScroll1.Height=300
VScroll1.Width=300'水平和垂直滚动条的高、宽度

Grid1.Height=(inputrownum 1)'
(Grid1.RowHeight(0) Grid1.GridLineWidth*12)
12 HScroll1.Height
'800×600象素时为12;640×480为15
Grid1.Width=(inputcolnum 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-inputcolnum
VScroll1.Min=1
VScroll1.Max=Grid1.Rows-inputrownum
'水平和垂直滚动条的范围

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>inputrownum-1Then
IfGrid1.TopRow<Grid1.Rows-inputrownumThen
VScroll1.Value=VScroll1.Value 1
Else
IfVScroll1.Value=VScroll1.MaxAnd
Grid1.SelStartRow<Grid1.Rows-1Then
Grid1.SelStartRow=Grid1.SelStartRow 1
Else
Grid1.SelStartRow=Grid1.Rows-1
EndIf
EndIf
Else
IfGrid1.TopRow=Grid1.SelStartRow-inputrownumThen
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->

Tags:VB 实现 电子

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接