vb控件获取主程序的路径
我编了个可以生成文本文档的vb控件,如果在控件中写如下语句,则生成文件和控件在一个路径:
abcfile = App.Path + "\ " + "abc.txt "
Open abcfile For Random As #100
Put #100, , "abcdefg "
Close #100
如果我控件放在system32目录下,则该abc.txt也在system32目录下。
但是,我现在想在我另一个程序中引用这个控件,而且想让控件生成这个文件的时候,就放在这个程序的目录下,不要放在system32下,该怎么办呢?
[解决办法]
给控件加个属性就行了,如SavePath,把应用程序的App.path值给它就可以了
[解决办法]
给你的控件添加一个属性,如ParentPath
在form_load的时候给该属性赋值,你取得时候就用ParentPath来取
abcfile = ParentPath + "\ " + "abc.txt "
控件代码中添加
Dim m_ParentPath As String
Public Property Get ParentPath() As String
ParentPath = m_ParentPath
End Property
Public Property Let ParentPath(ByVal New_ParentPath As String)
m_ParentPath = New_ParentPath
PropertyChanged "ParentPath "
End Property
在读写属性过程中添加
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_ParentPath = PropBag.ReadProperty( "ParentPath ", m_def_Value)
end sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty( "ParentPath ", m_ParentPath, App.Path)
end sub
最后将你的生成文本的代码改成
abcfile = m_ParentPath + "\ " + "abc.txt "
.............
[解决办法]
Private Sub Form_Load()
MyCmd1.ParentPath = App.Path & "\test "
End Sub