0

I have an xml with control definitions

 <?xml version="1.0" encoding="utf-8"?>
  <Controls>
   <TabControl>
     <Properties>
      <Dock>5</Dock>
     </Properties>
     <TabPage>
      <TextBox>
       <Properties>
        <Text>Id:</Text>
        <Location>106,12</Location>
        <Size>113,20</Size>
        <BorderStyle>2</BorderStyle>
       </Properties>
      </TextBox>
      <MyControl>
       <Properties>
        <Visible>True</Visible>
        <Location>106,33</Location>
        <Size>113,20</Size>
        <Enabled>True</Enabled>
        <BorderStyle>2</BorderStyle>
        <Text>Action:</Text>
        <TabIndex>0</TabIndex>
       </Properties>
      </MyControl>
     <Properties>
      <Text>Details</Text>
     </Properties>
    </TabPage>
   </TabControl>
  </Controls>

Ok. As you can see I have in this example One tabControl , with one TabPage. The TabPage has one TextBox and one MyControl.

I am able to read the xml and add all the controls except the MyControl. The reason is that I cannot find the type. Explain: in order to run through the xml and add the controls I wan tot find what type it is. So I use this line of code:

   Dim oType As Type = FindType("System.Windows.Forms." & elem.Name.ToString)

FindType is a function I found here: Best way to get a Type object from a string in .NET

Unfortunately I cannot figure out what to add in this function to find MyControl. MyControl is just a custom control added to my solution.

I know that I can use an if inside the FindType function

    if base is Nothing then
      if name.Contains("MyControl")Then
            base = GetType(MyControl)
        End If
    End If
      If base IsNot Nothing Then Return base

My problem is that I have 3 custom Controls and maybe in the future I will add more. Is any way to have something generic?

Another question is that in FindType function I have to use "System.Windows.Forms." for the name. and I discovered that without it the function doesn't return anything. I am thinking that this is happened because I call that function when I am building the form , so not everything is loaded yet?

Thanks for your time

Community
  • 1
  • 1
Nianios
  • 1,391
  • 3
  • 20
  • 45

3 Answers3

2

System.Windows.Forms is a namespace. It's used to group classes that belong together. For example, all stuff that is related to Windows Forms is contained in the System.Windows.Forms namespace. Your classes are in your project namespace, which is why they are not found when prefixed with System.Windows.Forms.

To learn more about namespaces, have a look at this old, but still valid MSDN article.

Now, let's get back to your problem. A simple solution would be to first look into the System.Windows.Forms namespace and then look in your own namespace:

Function FindTypeEx(typeName As String) As Type
    Dim type = FindType("System.Windows.Forms." & typeName)
    If type Is Nothing Then
        type = FindType(typeName)   ' Without the prefix
    End If
    Return type
End Function
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Heinzi thanks for your time. Actually it doesn't. I don't know why. I tried also to add the application name "appName.MyControl" but nothing again – Nianios Oct 22 '12 at 14:08
  • 2
    Verify that the namespace is correct by looking into your project properties ("Root namespace"). In addition, make sure that you use *the same capitalization* (the reflection methods used by FindType might be case-sensitive!). – Heinzi Oct 22 '12 at 14:36
1

if FindType("System.Windows.Forms." & elem.Name.ToString) doesn't find anything, just try FindType(elem.Name.ToString)

it looks like the code for FindType in the question you linked should find it if you just pass only the name, since it looks at all the types in the executing assembly.

Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49
  • Mike thanks for your time. Actually it doesn't. I don't know why. I tried also to add the application name "appName.MyControl" but nothing again – Nianios Oct 22 '12 at 14:06
  • try `AssemblyName.Namespace.MyControl` as well, where assembly name is the name of your assembly, and namespace is the namespace that MyControl lives in. – Mike Corcoran Oct 22 '12 at 14:10
  • Could you please give me more details for the namespace MyControl lives in. I found the assembly name (is the same as application name), but MyControl has no Namespace (Custom Tool Namespace="") – Nianios Oct 22 '12 at 14:29
  • 1
    look for the designer.cs file for whatever form the control was added to, and you'll see the namespace listed in there. if this is just a one form application, it's probably the same namespace that all your other code lives in. – Mike Corcoran Oct 22 '12 at 14:30
0

That's because there is no System.Windows.Forms.MyControl class.
You need to determine the correct namespace for each class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964