用VB实现类电子表格的数据录入
用VB实现类电子表格的数据录入
发布时间:2015-10-28 来源:查字典编辑
摘要:----在编程中经常会遇到应用程序中要输入大量数据的问题,如果能在该程序中实现象电子表格那样的输入界面,就可以解决这个问题。----VB虽提...

----在编程中经常会遇到应用程序中要输入大量数据的问题,如果能在该程序中实现象电子表格那样的输入界面,就可以解决这个问题。

----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

'800600象素时为12;640480为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.SelStartRow2Then

IfGrid1.TopRow1Then

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.SelStartRowin 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-

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新编程语言综合学习
热门编程语言综合学习
编程开发子分类