加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB文本框Text操作典型代码

(2019-11-15 15:55:11)
标签:

vb

文本框

text操作典型代码

分类: 程序设计_VB
 
’文本框操作典型代码,可以实现文本框回车就按顺序切换焦点,更改颜色。(前提是Text 控件要编成控件组)
’index是文本框的索引参数,把Text控件编成控件组就会有index参数可能用。
‘聚焦时的操作
Private Sub Text1_GotFocus(Index As Integer)
  Text1(Index).BackColor = &HFFFF00
  Text1(Index).SelStart = 0
  Text1(Index).SelLength = Len(Text1(Index))
End Sub
’其中4参数就是当前的文本框最后一个的参数。
Private Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
  If KeyCode = vbKeyReturn And Index < 4 Then Text1(Index + 1).SetFocus
  If KeyCode = vbKeyReturn And Index = 4 Then Command1.SetFocus
End Sub
‘失焦时操作
Private Sub Text1_LostFocus(Index As Integer)
  Text1(Index).BackColor = &H80000005
End Sub
 
************************************************
’通过FOR循环把Text控件内容清空,或给Text控件赋值(前提是Text控件组)
Private Sub Form_Load()
  If blnAddCP = True Then
     Me.Caption = "产品信息添加"
     rs1.Open "select * from 产品信息表 order by 产品编号", Cnn, adOpenKeyset
     If rs1.RecordCount > 0 Then
        rs1.MoveLast
        Text1(0) = Format(Val(rs1.Fields("产品编号")) + 1, "00000")
     Else
        Text1(0) = "00001"
     End If
     rs1.Close
     For i = 1 To Text1.UBound
       Text1(i) = ""
     Next i

  Else
     Me.Caption = "产品信息修改"
     With main_jbzl_cpgl.Adodc1.Recordset
     For i = 0 To Text1.UBound
       Text1(i) = .Fields(i)
     Next i

     End With
  End If
End Sub
-------------------------------------------- 
 
代码示例1:
Private Sub Form_Activate()
  If sql1 <> "" Then
     Adodc1.RecordSource = sql1
     Adodc1.Refresh
  End If
End Sub
Private Sub Form_Load()
  Me.Caption = text
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  sql1 = ""
End Sub
Private Sub DataGrid1_DblClick()
  If Adodc1.Recordset.RecordCount > 0 Then
     blnAddCP = False
     CPBookmark = DataGrid1.Bookmark
     Load main_jbzl_cpgl_lr
     main_jbzl_cpgl_lr.Show 1
   Else
     MsgBox "系统没有要修改的数据!", , "提示窗口"
   End If
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
  Select Case Button.Key
     Case "add"
       blnAddCP = True
       Load main_jbzl_cpgl_lr
       main_jbzl_cpgl_lr.Show 1
     Case "modify"
       DataGrid1_DblClick
     Case "delete"
       If Adodc1.Recordset.RecordCount > 0 Then
         Adodc1.Recordset.Delete
         Adodc1.Refresh
       Else
         MsgBox "系统没有要删除的数据!", , "提示窗口"
       End If
     Case "find"
       tb1 = "产品信息表"
       Load main_fzfind
       main_fzfind.Show 1
     Case "all"
       Adodc1.RecordSource = "select * from 产品信息表 order by 产品编号"
       Adodc1.Refresh
     Case "close"
       Unload Me
  End Select
End Sub
 
代码示例2:
Dim rs1 As New ADODB.Recordset
Private Sub Form_Activate()
  Text1(1).SetFocus
End Sub
Private Sub Form_Load()
  If blnAddCP = True Then
     Me.Caption = "产品信息添加"
     rs1.Open "select * from 产品信息表 order by 产品编号", Cnn, adOpenKeyset
     If rs1.RecordCount > 0 Then
        rs1.MoveLast
        Text1(0) = Format(Val(rs1.Fields("产品编号")) + 1, "00000")
     Else
        Text1(0) = "00001"
     End If
     rs1.Close
     For i = 1 To Text1.UBound
       Text1(i) = ""
     Next i
  Else
     Me.Caption = "产品信息修改"
     With main_jbzl_cpgl.Adodc1.Recordset
     For i = 0 To Text1.UBound
       Text1(i) = .Fields(i)
     Next i
     End With
  End If
End Sub
Private Sub Text1_GotFocus(Index As Integer)
  Text1(Index).BackColor = &HFFFF00
  Text1(Index).SelStart = 0
  Text1(Index).SelLength = Len(Text1(Index))
End Sub
Private Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
  If KeyCode = vbKeyReturn And Index < 4 Then Text1(Index + 1).SetFocus
  If KeyCode = vbKeyReturn And Index = 4 Then Command1.SetFocus
End Sub
Private Sub Text1_LostFocus(Index As Integer)
  Text1(Index).BackColor = &H80000005
End Sub
Private Sub Command1_Click()
  If IsNumeric(Text1(3)) = False Then
    MsgBox "请在“单价”文本框中输入数值型数据!"
    Exit Sub
  End If
  If Text1(1) = "" Then
    MsgBox "品名规格不允许为空!", , "提示"
    Exit Sub
  End If
  On Error GoTo SaveErr
  If blnAddCP = True Then
     Cnn.Execute ("insert into 产品信息表 values('" + Text1(0) + "','" + Text1(1) + "','" + Text1(2) + _
     "'," + Text1(3) + ",'" + Text1(4) + "')")
    main_jbzl_cpgl.Adodc1.Refresh
  Else
    Cnn.Execute ("update 产品信息表 set 品名规格 ='" + Text1(1) + "',单位 ='" + Text1(2) + _
    "',单价 =" + Text1(3) + ",备注='" + Text1(4) + "'where 产品编号 ='" + Text1(0) + "'")
     main_jbzl_cpgl.Adodc1.Refresh
     main_jbzl_cpgl.DataGrid1.Bookmark = CPBookmark
  End If
  Unload Me
  Exit Sub
SaveErr:
  MsgBox Err.Description
End Sub
Private Sub Command2_Click()
  Unload Me
End Sub
 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有