动态加载用户控件后不能调用其属性方法的问题。如解决可以用2G的代码做为报答。

qyh 2002-08-22 12:58:15
关于动态加载用户控件的问题,我担心我说得不是很清楚,所有把代码贴出来。
测试方法:将下面那个控件生成FileOcx.Ocx,然后新建一个工程,在Form1中加入一个按钮,加入以下代码。
问题是:在不将FileOcx.Ocx添加到VB的控件箱中的前提下,我怎么才能成功调用FileOcx的FileName属性??
我不是想实现一个功能,我只是想成功调用FileOcx的FileName属性就行了。谢谢大家。

Dim WithEvents oControl As VBControlExtender
Private Function LoadControl()
Dim MyOCX As Control
Licenses.Add "FileOcx.FileLoad"
If Not oControl Is Nothing Then
Controls.Remove ("FileView")
End If
Set MyOCX = Controls.Add("FileOcx.FileLoad", "FileView")
MyOCX.FileName="C:\test.txt" '这里将会出错。
Set oControl = MyOCX
oControl.Height = 13500
oControl.Width = 16500
oControl.Top = 100
oControl.Visible = True
End Function

Private Sub Command1_Click()
LoadControl
End Sub
'============================================================
'============================================================


控件代码:
FileOcx.vbp文件
'============================================================
Type=Control
UserControl=FileLoad.ctl
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\WINNT\System32\stdole2.tlb#OLE Automation
Object={3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0; richtx32.ocx
Startup="(None)"
Command32=""
Name="FileOcx"
HelpContextID="0"
CompatibleMode="1"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=1
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
ThreadingModel=1
'======================================================

FileLoad.ctl文件
'======================================================
VERSION 5.00
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"
Begin VB.UserControl FileLoad
ClientHeight = 3600
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
ScaleHeight = 3600
ScaleWidth = 4800
Begin RichTextLib.RichTextBox RichTextBox1
Height = 3375
Left = 0
TabIndex = 0
Top = 0
Width = 4695
_ExtentX = 8281
_ExtentY = 5953
_Version = 393217
Enabled = -1 'True
TextRTF = $"FileLoad.ctx":0000
End
End
Attribute VB_Name = "FileLoad"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Const m_def_FileName = ""
Dim m_FileName As String
Public Property Get FileName() As String
FileName = m_FileName
End Property
Public Property Let FileName(ByVal New_FileName As String)
m_FileName = New_FileName
PropertyChanged "FileName"
End Property
Private Sub UserControl_InitProperties()
m_FileName = m_def_FileName
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_FileName = PropBag.ReadProperty("FileName", m_def_FileName)
End Sub

Private Sub UserControl_Resize()
RichTextBox1.Height = UserControl.Height
RichTextBox1.Width = UserControl.Width
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("FileName", m_FileName, m_def_FileName)
End Sub
...全文
91 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qyh 2002-08-23
  • 打赏
  • 举报
回复
PFPF
ItSeeker 2002-08-23
  • 打赏
  • 举报
回复
'行啊
MyOCX.FileName="C:\test.txt"
该为
MyOCX.object.FileName = "C:\test.txt"
'呵呵
qyh 2002-08-22
  • 打赏
  • 举报
回复
还是不行,看来真的不行了
qyh 2002-08-22
  • 打赏
  • 举报
回复
非常感谢,我试一下。
ttyp 2002-08-22
  • 打赏
  • 举报
回复
可能是你得Licenses没有key,

看:Even if you clear the Require License Key option, a user control that contains third-party controls will still require a license key.

详细见下面:



Add Method (Licenses Collection)


Adds a license to the Licenses collection and returns the license key.

Syntax

object.Add (ProgID, LicenseKey)

The Add method syntax has these parts:

Part Description
object Required. An object expression that evaluates to an object in the Applies To list.
ProgID Required. A string that specifies the control for which a license key will be added.
LicenseKey Optional. A string that specifies the license key.


Remarks

Use the Add method whenever you want to dynamically add a control that requires a license key. For more information about controls that require license keys, see "Licensing Issues for Controls" in the See Also list.

When you compile a user control that requires a license key, and you want to dynamically add that control to an existing application, you must use the Add method for the Licenses collection in two different ways.

First, use the method to return the license key that is hard-coded into a user control. Second, use the method to add the same license key to the Licenses collection before adding the user control to the Controls collection.

In most cases, you will have to use the method in both ways in order to properly deploy a compiled user control. The steps for this are outlined below.

After you have compiled a user control that requires a license key, use the Add method to return the license key. Store that license key where it can be retrieved by the deployed application. For example, the example below writes the key to a file. You could also store it in a database, or in the Windows registry.

Private Sub GenerateLicenseKey()
Dim intFile As Integer
intFile = FreeFile
' Open a file to write the license key to.
Open "c:\Temp\Ctl_Licenses.txt" For Output As #intFile
Dim strLicense As String
strLicense = Licenses.Add("prjWeeks.WeeksCtl")
' Write the license key to the file.
Write #intFile, strLicense
Close #intFile
End Sub

When you deploy your control, the deployed application adds the license key to the Licenses collection before adding the control to the Controls collection. (Of course, the control must have been installed on the machine as well.) The code example below adds the license key, then adds the control:

Option Explicit
Dim WithEvents extObj As VBControlExtender

Private Sub LoadDynamicControl()
Dim intFile As Integer
intFile = FreeFile
Open "c:\Download\Ctl_Licenses.txt" For Input As #intFile
Dim strKey As String
' On the client machine, read the license key from the file.
Input #intFile, strKey
Licenses.Add "prjWeeks.WeeksCtl", strKey
Close #intFile
Set extObj = Controls.Add("prjWeeks.WeeksCtl", "ctl1")
With Controls("ctl1")
.Visible = True
End With
End Sub

When To Add a License Key
When you create a user control and you want to distribute the control for dynamic control addition, you must consider the following question: Does the user control contain only intrinsic controls? If the answer is yes, then ask, "Do I want to require that the end user have a license key in order to use the control?" If the answer to both questions is "yes," then be sure to check the Require License Key option on the General tab of the Project Properties dialog box.

Note Even if you clear the Require License Key option, a user control that contains third-party controls will still require a license key.

When No License Key Is Needed
There are two circumstances when you do not have to add a license key in order to add a control to the Controls collection:

When the control is an intrinsic control and you have not checked the Require License Key option.


When you add a control that is referenced in the project. In other words, if the control is present in the Toolbox.
Note When you do have a control in the Toolbox and you plan to add that control only at run time, be sure to clear the Remove Information About Unused ActiveX Controls option on the Make tab of the Project Properties dialog box; otherwise, trying to add the control will fail.



qyh 2002-08-22
  • 打赏
  • 举报
回复
天,老大,你没有测试过吧,这样是不行的。

哎,看来没有办法了。
zyl910 2002-08-22
  • 打赏
  • 举报
回复
Private Function LoadControl()
Dim MyOCX As Control
Licenses.Add "FileOcx.FileLoad"
If Not oControl Is Nothing Then
Controls.Remove ("FileView")
End If
Set MyOCX = Controls.Add("FileOcx.FileLoad", "FileView")

Dim TempObj as Object
Set TempObj=MyOCX
TempObj.FileName="C:\test.txt"

……

End Function
junwhj 2002-08-22
  • 打赏
  • 举报
回复
我也遇到过,没有解决,收藏此贴!
qyh 2002-08-22
  • 打赏
  • 举报
回复
总之,谢谢楼上的了。
qyh 2002-08-22
  • 打赏
  • 举报
回复
真是麻烦,像Listview之类的控件是可以成功的,第三方控件就是不行,该死的VB。
shawls 2002-08-22
  • 打赏
  • 举报
回复
我也做过,没有成功,用withevents看看如何,sorry,没有办法帮助你
rushing 2002-08-22
  • 打赏
  • 举报
回复
再见。
qyh 2002-08-22
  • 打赏
  • 举报
回复
你以为2G的代码这么容易拿???
qyh 2002-08-22
  • 打赏
  • 举报
回复
看看题好不好!!!
rushing 2002-08-22
  • 打赏
  • 举报
回复
用regsvr32 注册你的控件。
在工程-〉部件中添加。

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧