欢迎来到山村网

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

2019-03-02 14:45:04浏览:69 来源:山村网   
核心摘要:  ----在编程中经常会遇到应用程序中要输入大量数据的问题,如果能在该程序中实现象电子表格那样的输入界面,就可以解决这个问

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

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

(责任编辑:豆豆)
下一篇:

配置Delphi工具菜单

上一篇:

Java 程序死锁问题原理及解决方案

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com