首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB add方法添加控件有关问题

2012-05-24 
VB add方法添加控件问题Dim S As LongPrivate WithEvents B1 As CommandButton, WithEvents T1 As TextBox

VB add方法添加控件问题
Dim S As Long
Private WithEvents B1 As CommandButton, WithEvents T1 As TextBox
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  Dim KT, KB1, nTName, nB1Name As String
  If Button = 2 Then '右击添加控件
  S = S + 1
  '添加一个文本框
  nTName = "MyText" & S
  Set KT = Controls.Add("vb.textbox", nTName)
  'Controls(nTName).Visible = True '也可以:KT.Visible = True
  Controls(nTName).Text = nTName
  Controls(nTName).Move 0, (S - 1) * 900, 4600, 800
  If S = 1 Then Set T1 = KT
   
  '添加一个按钮
  nB1Name = "按钮1" & S
  Set KB1 = Controls.Add("vb.CommandButton", nB1Name)
  Controls(nB1Name).Visible = True
  Controls(nB1Name).Caption = nB1Name
  Controls(nB1Name).Move 0, (S - 1) * 900, 4600, 800
  If S = 1 Then Set B1 = KB1
  End If
End Sub

Private Sub B1_Click()
'按钮的单击事件
T1.Visible = True
B1.Visible = False
End Sub
Private Sub T1_DblClick()
'文本框的双击事件
B1.Caption = T1.Text
T1.Visible = False
B1.Visible = True
B1.Caption = T1.Text
End Sub
Private Sub Timer1_Timer()
  Timer1.Enabled = True
  Dim nName As String
  On Error Resume Next
  nName = ActiveControl.Name
  If T1.Name <> nName Then
  Set T1 = ActiveControl
  Else
  End If
  If B1.Name <> nName Then
  Set B1 = ActiveControl
  End If
End Sub
本程序的目的是:添加几个command和text控件后,单击command控件后,出现text,把输入text的内容后返回到相应的command caption属性,本人不才,求指教

[解决办法]
使用controls.add添加多个控件,如果没有为每个添加的控件赋值给withevents变量,则必须使用特别的技术才能使用事件,建议使用控件数组添加控件 Load Command1(index)

热点排行