|
-
Aug 16th, 2007, 10:59 AM
#1
[RESOLVED] Identify whether an object has a named property
I'm iterating through the controls collection, so I'm using On Error Resume Next to handle when the property I'm looking at (eg: TabIndex) doesn't exist for that control.
Just wondering if there was a better or easier way to determine if a named property exists. Something like:
If HasProperty(ctl, "TabIndex") Then
...
-
Aug 16th, 2007, 02:05 PM
#2
Re: Identify whether an object has a named property
You can use the TypeLib Information library (TLBInf32.dll). I don't know about any documentation and have yet to search the net for more information. I just started with this library but using only the object browser for help was able to quickly write this code.
Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If ControlHasProperty(ctl, "TabIndex") Then
Debug.Print ctl.Name; " has the TabIndex Property"
Else
Debug.Print ctl.Name; " does not have a TabIndex property"
End If
Next
End Sub
Private Function ControlHasProperty(ctl As Control, PropName As String) As Boolean
Dim info As TLI.InterfaceInfo
Dim member As TLI.MemberInfo
Set info = InterfaceInfoFromObject(ctl)
For Each member In info.Members
ControlHasProperty = StrComp(member.Name, PropName, vbTextCompare) = 0
If ControlHasProperty Then Exit For
Next
End Function
There are many more objects in this library that I need to explore and so am not sure if there is a better method - such as a Property Exists method
Last edited by brucevde; Aug 16th, 2007 at 02:08 PM.
-
Aug 16th, 2007, 02:11 PM
#3
Re: Identify whether an object has a named property
That's pretty cool. I'm a bit ignorant of Type Libraries in general; could you sum up an overview of the concept behind type libraries in a few sentences or less?
-
Aug 16th, 2007, 02:46 PM
#4
Re: Identify whether an object has a named property
LOL. Describe the inner workings of COM in a few sentences! That is way beyond me...
-
Aug 16th, 2007, 06:11 PM
#5
Re: Identify whether an object has a named property
I think Bruce's code is quite neat and clear, for some more info about TypeLib see This Post (from yesterday), specially the Links inside it. The First Link shows how to bring all members from DLL/OCX files (here is where the Object Browser takes VB Project's types from) to List/Treeview, the second Link shows how to list the Object's properties/Subs/functions.
Maybe you can use the code in the 2nd link to ensure you found a property and not a method, or vice-versa.
Last edited by jcis; Aug 16th, 2007 at 06:25 PM.
-
Aug 16th, 2007, 07:02 PM
#6
Re: Identify whether an object has a named property
 Originally Posted by jcis
I think Bruce's code is quite neat and clear,
So do I; I'm just not clear on what a type library actually is.
-
Aug 16th, 2007, 07:07 PM
#7
Re: Identify whether an object has a named property
The answer of your question is not Here? (From the first Link in the Thread I Linked before)
TypeLib: "a set of COM objects designed to allow programmers to browse type libraries programmatically"
-
Aug 16th, 2007, 07:14 PM
#8
Re: Identify whether an object has a named property
 Originally Posted by jcis
The answer of your question is not Here? (From the first Link in the Thread I Linked before)
TypeLib: "a set of COM objects designed to allow programmers to browse type libraries programmatically"
Not so much. A type library lets you browse type libraries?
And this bit cracked me up:
If you're anything like me, you probably spend as much time using the Object Browser as you do actually writing code in Visual Basic®.
Heh, I can count the number of times I've opened the Object Browser in my life on one hand, and I've never found it helpful.
-
Aug 16th, 2007, 07:25 PM
#9
Re: Identify whether an object has a named property
Hmm, reading further gives a reasonable definition:
Type libraries are files that explicitly describe some or all of the contents of components. This includes information about the methods, properties, constants, and other members exposed by the component. Development tools such as Visual Basic make use of the information contained in the type library to help you, as a developer, access and use the component. In addition, type libraries provide a convenient way to include a simple level of descriptive documentation for component members.
If you develop COM components using Visual Basic, a type library is automatically created for you when you compile the component. In addition, you can create your own type libraries for your own components. There are a few different ways to do this, but for those who program in Visual Basic a favorite tool is MIDL. This tool allows you to compile IDL files into binary type libraries. In this way, you could, for example, build a type library to encapsulate and expose a set of API calls that you often use. Then, instead of using declare statements in your Visual Basic source code, all you would have to do is set a reference to your custom API type library.
This doesn't appeal to me in any way, though. In fairness, I'm not a real programmer, but rather a business applications developer. All my work is geared toward small database programs with relatively few users, so I rarely have need of any advanced stuff.
As for the example in that quote, wrapping up API calls into a type library seems counter-productive to me. From my perspective, the main advantage of API calls is that they reduce the number of dependencies in a given project. I don't like dependencies; in fact, I rather dislike them. I don't even like using user controls that I wrote, even if I include the user control source code directly in the project.
Classes I can get behind, but anything requiring dependencies leaves me cold.
-
Aug 16th, 2007, 07:26 PM
#10
Re: Identify whether an object has a named property
 Originally Posted by Ellis Dee
Not so much. A type library lets you browse type libraries?
This Type Library lets you browse trought any exposed Object members. These Objects can be in the VB Project/OCXs/Dlls/Registry..)
Example on how VB uses it: You go to Components and check "Microsoft Flexgrid .." then VB uses this library to Open de Flexgrid's OCX file and starts searching for members (exposed Constants/Types/Enums/properties/methods), you can see this info in the Object Browser.
The same way you can use the TypeLib yourself to browse into any COM Object.
-
Aug 16th, 2007, 07:30 PM
#11
Re: Identify whether an object has a named property
 Originally Posted by jcis
Example on how VB uses it: You go to Components and check "Microsoft Flexgrid .." then VB uses this library to Open de Flexgrid's OCX file and starts searching for members (exposed Constants/Types/Enums/properties/methods), you can see this info in the Object Browser.
So a type library in effect gives you intellisense information without requiring an IDE?
-
Aug 16th, 2007, 07:30 PM
#12
Re: Identify whether an object has a named property
 Originally Posted by Ellis Dee
Classes I can get behind, but anything requiring dependencies leaves me cold.
You don't like to create package Installers don't you?
-
Aug 16th, 2007, 07:32 PM
#13
Re: Identify whether an object has a named property
 Originally Posted by Ellis Dee
So a type library in effect gives you intellisense information without requiring an IDE?
Exactly that is And that's how Static used it in the StaticToolz thread, in the first Link of my post.
-
Aug 16th, 2007, 07:32 PM
#14
Re: Identify whether an object has a named property
 Originally Posted by jcis
You don't like to create package Installers don't you? 
I can't stand installation period.
-
Aug 16th, 2007, 07:43 PM
#15
Re: Identify whether an object has a named property
Sorry guys, I got the "you must spread some reputation around" notice on both of you. So here's a public thanks for your help.
-
Aug 17th, 2007, 05:15 AM
#16
Re: [RESOLVED] Identify whether an object has a named property
Did someone say "Installation"?....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|