VB6 如何让动态创建的控件响应动作
比如我在Form1中声明了DimWithEventsoCtrlAsVB.VBControlExtender'本来想声明为DimWithEventsoCtrlAsWebBr...
比如我在Form1中声明了
Dim WithEvents oCtrl As VB.VBControlExtender
'本来想声明为Dim WithEvents oCtrl As WebBrowser
在Form1中放置SStab1多页控件,
然后在程序运行中,动态添加SStab1的页数,每添加一页,就动态
Set oCtrl = Form1.Controls.Add("SHELL.EXPLORER.2", "WB" & Form1.SSTab1.Tab, Form1.SSTab1) '成功动态创建WebBrowser
With oCtrl '设置参数
Set .Container = Form1.SSTab1
.Left = 0
.Top = Form1.WB.Top 'WB为设计上添加的浏览器WebBrowse控件
.Width =Form1.WB.Width
.Height = Form1.WB.Height
.Silent = True
.Visible = True
.Navigate "12345"
End With
创建完毕之后,在另外一个按钮中,我需要动态使创建的浏览器控件上下滚动:
Select Case SSTab1.Tab
Case 0
Set oCtrl = WB
Case 1
On Error Resume Next
Set oCtrl = WB1
Case 2
On Error Resume Next
Set oCtrl = WB2
Case 3
On Error Resume Next
Set oCtrl = WB3
Case 4
On Error Resume Next
Set oCtrl = WB4
Case 5
On Error Resume Next
Set oCtrl = WB5
Case 5
On Error Resume Next
Set oCtrl = WB6
End Select
a= oCtrl.Document.body.scrollheight '此句能够获得动态创建的浏览器的高度
oCtrl.Document.parentwindow.scrollby 0, 300 '向下滚动3000, 但是,这句不起作用,不滚动呀。但是在设计时添加的浏览器控件WB, WB.Document.parentwindow.scrollby 0, 300 却能滚动呢。
如何让动态创建的浏览器控件,控件名分别是 WB1,WB2,WB3,WB4,WB5,WB6也能够响应动作如WBn.Document.parentwindow.scrollby 0, 300 呢? 展开
Dim WithEvents oCtrl As VB.VBControlExtender
'本来想声明为Dim WithEvents oCtrl As WebBrowser
在Form1中放置SStab1多页控件,
然后在程序运行中,动态添加SStab1的页数,每添加一页,就动态
Set oCtrl = Form1.Controls.Add("SHELL.EXPLORER.2", "WB" & Form1.SSTab1.Tab, Form1.SSTab1) '成功动态创建WebBrowser
With oCtrl '设置参数
Set .Container = Form1.SSTab1
.Left = 0
.Top = Form1.WB.Top 'WB为设计上添加的浏览器WebBrowse控件
.Width =Form1.WB.Width
.Height = Form1.WB.Height
.Silent = True
.Visible = True
.Navigate "12345"
End With
创建完毕之后,在另外一个按钮中,我需要动态使创建的浏览器控件上下滚动:
Select Case SSTab1.Tab
Case 0
Set oCtrl = WB
Case 1
On Error Resume Next
Set oCtrl = WB1
Case 2
On Error Resume Next
Set oCtrl = WB2
Case 3
On Error Resume Next
Set oCtrl = WB3
Case 4
On Error Resume Next
Set oCtrl = WB4
Case 5
On Error Resume Next
Set oCtrl = WB5
Case 5
On Error Resume Next
Set oCtrl = WB6
End Select
a= oCtrl.Document.body.scrollheight '此句能够获得动态创建的浏览器的高度
oCtrl.Document.parentwindow.scrollby 0, 300 '向下滚动3000, 但是,这句不起作用,不滚动呀。但是在设计时添加的浏览器控件WB, WB.Document.parentwindow.scrollby 0, 300 却能滚动呢。
如何让动态创建的浏览器控件,控件名分别是 WB1,WB2,WB3,WB4,WB5,WB6也能够响应动作如WBn.Document.parentwindow.scrollby 0, 300 呢? 展开
展开全部
您也可以通过使用 WithEvents 关键字声明一个对象变量,并且设置该方法返回的引用为该变量,从而编程一个动态添加控件的事件,如下所示。
Option Explicit
'声明对象变量为 CommandButton 。
Private WithEvents cmdObject As CommandButton
Private Sub Form_Load()
Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
cmdObject.Visible = True
cmdObject.Caption = "Dynamic CommandButton"
End Sub
Private Sub cmdObject_Click()
Print "This is a dynamically added control"
End Sub
Option Explicit
'声明对象变量为 CommandButton 。
Private WithEvents cmdObject As CommandButton
Private Sub Form_Load()
Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
cmdObject.Visible = True
cmdObject.Caption = "Dynamic CommandButton"
End Sub
Private Sub cmdObject_Click()
Print "This is a dynamically added control"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询