Skip to content

Commit c5481ff

Browse files
committed
Resolve AsTypeNames for UserForm controls.
1 parent 0e4e331 commit c5481ff

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

Rubberduck.Parsing/Symbols/DeclarationSymbolsListener.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.InteropServices;
1010
using Rubberduck.VBEditor.SafeComWrappers;
1111
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
12+
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
1213

1314
namespace Rubberduck.Parsing.Symbols
1415
{
@@ -168,12 +169,13 @@ private void DeclareControlsAsMembers(IVBComponent form)
168169

169170
foreach (var control in form.Controls)
170171
{
172+
var typeName = control.TypeName();
171173
// The as type declaration should be TextBox, CheckBox, etc. depending on the type.
172174
var declaration = new Declaration(
173175
_qualifiedName.QualifyMemberName(control.Name),
174176
_parentDeclaration,
175177
_currentScopeDeclaration,
176-
"Control",
178+
string.IsNullOrEmpty(typeName) ? "Control" : typeName,
177179
null,
178180
true,
179181
true,

Rubberduck.VBEEditor/SafeComWrappers/Office.Core/Abstract/IControl.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Runtime.InteropServices.ComTypes;
24
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
35

46
namespace Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract
@@ -7,4 +9,66 @@ public interface IControl : ISafeComWrapper, IEquatable<IControl>
79
{
810
string Name { get; set; }
911
}
12+
13+
public static class ControlExtensions
14+
{
15+
public static string TypeName(this IControl control)
16+
{
17+
try
18+
{
19+
var dispatch = control.Target as IDispatch;
20+
if (dispatch == null)
21+
{
22+
return "Control";
23+
}
24+
ITypeInfo info;
25+
dispatch.GetTypeInfo(0, 0, out info);
26+
string name;
27+
string docs;
28+
int context;
29+
string help;
30+
info.GetDocumentation(-1, out name, out docs, out context, out help);
31+
return name;
32+
}
33+
catch
34+
{
35+
return "Control";
36+
}
37+
}
38+
39+
[ComImport]
40+
[Guid("00020400-0000-0000-C000-000000000046")]
41+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
42+
interface IDispatch
43+
{
44+
[PreserveSig]
45+
int GetTypeInfoCount(out int Count);
46+
47+
[PreserveSig]
48+
int GetTypeInfo(
49+
[MarshalAs(UnmanagedType.U4)] int iTInfo,
50+
[MarshalAs(UnmanagedType.U4)] int lcid,
51+
out ITypeInfo typeInfo);
52+
53+
[PreserveSig]
54+
int GetIDsOfNames(
55+
ref Guid riid,
56+
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
57+
string[] rgsNames,
58+
int cNames,
59+
int lcid,
60+
[MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
61+
62+
[PreserveSig]
63+
int Invoke(
64+
int dispIdMember,
65+
ref Guid riid,
66+
uint lcid,
67+
ushort wFlags,
68+
ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams,
69+
out object pVarResult,
70+
ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
71+
IntPtr[] pArgErr);
72+
}
73+
}
1074
}

0 commit comments

Comments
 (0)