动态生成Command控件后,我要想动态移动其中任意Command控件,我现在只能移动一个控件,要是想移动多个控件的话也可以,就是重复定义多个想同控件,有没有其它办法,一次性实现啊:
Option Explicit
Dim IsMove As Boolean
Dim Mx As Single, My As Single
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command1
If IsMove = True Then
.Left = .Left - Mx + X
.Top = .Top - My + Y
End If
End With
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = False
End Sub
Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command2
If IsMove = True Then
.Left = .Left - Mx + X
.Top = .Top - My + Y
End If
End With
End Sub
Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = False
End Sub
......
这样呢,我就可以预先定义command(1~~~n)个,也可实现,但是太多了,有没有办法只用写一次就实现的,请教了....
Option Explicit
Dim IsMove As Boolean
Dim Mx As Single, My As Single
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command1
If IsMove = True Then
.Left = .Left - Mx + X
.Top = .Top - My + Y
End If
End With
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = False
End Sub
Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command2
If IsMove = True Then
.Left = .Left - Mx + X
.Top = .Top - My + Y
End If
End With
End Sub
Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = False
End Sub
......
这样呢,我就可以预先定义command(1~~~n)个,也可实现,但是太多了,有没有办法只用写一次就实现的,请教了....
解决方案 »
- datagrid三列字段数据从相应数据库表的相同三个字段数据查找,以决定datagrid字段的数据是更新还是新增到数据库表中?
- 请教我的制作安装程序在有的机器上有求更新系统文件,而有的则不需要可以直接安装完成呢?
- 求两个控件
- (初试)Mscomm 控件的问题
- 我的程序打印是十几页的,但我怎样才能知道当前打印到哪一页?客户需要我打印页号
- 多线程调用控件时出错,为什么?????
- 数据库写入问题?
- 换个问法:程序运行屏幕截图时遇到屏保或省电设置(屏幕关闭)如何用代码解决?
- 今天也很高兴,送分!截止17:30。
- 简单问题:如何清空 listbox 里的内容
- 高分求解.两个问题???
- Excel.Diagram 找不到了
在VB编程中,经常要根据不同的情况在运行时向窗口中添加或者删除控件,而各个控件还要
响应各种事件。在一般的情况下是首先在设计时将控件加入到窗口中,在它们的各个事件中写入
代码,然后将它们的Visible属性设置为False。在运行时再使控件显示出来,但是这样既不方便
同时也因为在设计时在窗口中加入太多的控件而使得程序的运行速度变慢。下面我向大家介绍一
种通过编程在运行时动态添加和删除控件的方法
首先建立一个工程文件,然后在Form1中加入以下的代码:
Dim WithEvents cmdMyCommand As VB.CommandButton
Option Explicit
'在下面的定义中,程序定义了一个TextBox控件、一个CommandButton控件
'和一个附加控件。
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton
Dim WithEvents ctlCommandDel As VB.CommandButton
Private Sub ctlCommandDel_Click()
Dim i As Integer
'将控件的许可证信息删除
Licenses.Remove "MSComctlLib.TreeCtrl"
If MsgBox("是否删除所有控件", vbYesNo) = vbYes Then
For i = 1 To Form1.Controls.Count
Controls.Remove 0
Next i
End If
End Sub
Private Sub ctlCommand_Click()
ctlText.Text = "你点击的是控制按钮"
End Sub
Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
'当点击树形控件的某一个条目后,在ctlText中显示条目。
If Info.Name = "Click" Then
ctlText.Text = "你点击的条目是 " & _
ctlDynamic.object.selecteditem.Text
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
' 将树形控件的许可证信息加入到许可证集合中
' 如果许可证已经存在,则会返回错误信息732
Licenses.Add "MSComctlLib.TreeCtrl"
' 在Form中动态的加入一个树形控件,如果你想树形控件建立到不同的
'容器中,象一个Frame控件或者PictureBox控件,你只要将Controls.Add
'函数的第三个参数改为特定的容器名就可以了
Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
"myctl", Form1)
'设置树形控件的位置和尺寸
ctlDynamic.Move 1, 1, 2500, 3500
'在树形控件中加入10个节点
For i = 1 To 10
ctlDynamic.object.nodes.Add Key:="Test" & Str(i), _
Text:="Test" & Str(i)
ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
Relationship:=4, Text:="TestChild" & Str(i)
Next i
'使树形控件可见
ctlDynamic.Visible = True
'加入一个TextBox
Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
'设置TextBox的位置和尺寸
ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
1, 2500, 100
'将textBox的背景色设置为蓝色并将前景色设置为白色
ctlText.BackColor = vbBlue
ctlText.ForeColor = vbWhite
'使TextBox可见
ctlText.Visible = True
'加入一个CommandButton
Set ctlCommand = Controls.Add("VB.CommandButton", _
"ctlCommand1", Form1)
'设置CommandButton的位置和尺寸
ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
ctlText.Height + 50, 1500, 500
'设置CommandButton的标题
ctlCommand.Caption = "点击"
'使CommandButton可见
ctlCommand.Visible = True
'建立一个删除按钮
Set ctlCommandDel = Controls.Add("VB.CommandButton", _
"ctlCommand2", Form1)
ctlCommandDel.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
ctlText.Height + 650, 1500, 500
ctlCommandDel.Caption = "删除所有控件"
ctlCommandDel.Visible = True
End Sub
运行上面的程序,可以看到程序在窗口中加入了三个VB标准控件:一个TextBox和两个
CommandButton还加入了一个扩展的ActiveX控件:树形控件。这些控件还可以响应消息,
点击树形控件中的项目或者“点击”按钮就可以在文本框中显示相应的内容。点击“删除
所有控件”按钮就可以删除加入的所有控件了。
通过上面的程序可以看到,通过WithEvents可以定义带事件相应的控件,对于标准的VB
控件(例如CommandButton、TextBox等)可以通过VB.XXX来定义,其中XXX是控件的类的名称
,而对于扩展的ActiveX控件,可以通过VBControlExtender来定义,在装载扩展控件以前,
首先要使用Licenses对象加入控件的许可证信息。
上面的程序在VB6,WIN98中文版下运行通过。
Dim ctl As Control
For Each ctl In Form1.Controls
If TypeOf ctl Is CommandButton Then
ctl.Move ctl.Left + 120, ctl.Height + 120
End If
Next
IsMove = True
Mx = X
My = Y
End Sub
说白了,就是command1_mousedown中的command1是否可用变量,例:command&str$(i),也就是这个意思
然后:
Private Sub cmdMyCommand_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
。你要用哪个控件再用之前
set cmdMyCommand=****'(你要代替的控件)
不知符不符合你要求
Dim WithEvents cmdMyCommand As VB.CommandButton
Dim a() As CommandButton
Dim IsMove As Boolean
Dim Mx As Single, My As Single
Private Sub Form_click()
ReDim a(5)
Dim i As Integer
For i = 1 To 5
Set a(i) = Me.Controls.Add("VB.CommandButton", "command" & i)
a(i).Visible = True
a(i).Top = i * 500
a(i).Width = 500
a(i).Caption = i
Next i
End Sub Private Sub cmdMyCommand_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = True
Mx = X
My = Y
End Sub
Private Sub cmdMyCommand_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command1
If IsMove = True Then
.Left = .Left - Mx + X
.Top = .Top - My + Y
End If
End With
End Sub
Private Sub cmdMyCommand_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMove = False
End Sub
这样写不行,还是不能移动,你看看是什么问题~~~
'form1------------------------------------------------------------------
Option ExplicitPublic btn As New clsButtonPublic Sub Command1_Click()
Debug.Print Now
End SubPrivate Sub Command2_Click()
Dim btnobj As CommandButton
Set btnobj = Form1.Controls.Add("VB.CommandButton", "cmdObj1")
With Form1!cmdobj1
.Left = 100
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
btn.Bind btnobj
Set btnobj = Form1.Controls.Add("VB.CommandButton", "cmdObj2")
With Form1!cmdobj2
.Left = 3000
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
btn.Bind btnobj
End Sub'---------------------------------------------
'clsButton.cls classOption ExplicitPublic WithEvents cmdButton As CommandButton
Private Buttons() As New clsButton
Private nCount As Long
Public Sub Bind(button As CommandButton)
nCount = nCount + 1
ReDim Preserve Buttons(nCount) As New clsButton
Set Buttons(nCount).cmdButton = button
End Sub
Private Sub cmdButton_Click()
Call Form1.Command1_Click
End Sub
'
for i = 1 to n
load command1(i)
command1(i).visiabled = true
next