Loop ALL Properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Hart

    Loop ALL Properties

    I am going in circles trying to loop through properties on 3rd party
    controls. For example, I have a textbox that has its maximum length located
    at MyTextBox.Prope rties.MaxLength - instead of the dotnet textbox which is
    MyTextBox.MaxLe ngth. If I loop a built in dotnet control, it finds the
    property no problem. But looping through the 3rd party control,
    Properties.MaxL ength does not get listed. I was hoping to find how it names
    it using reflection, so I could use GetValue and SetValue to dyanmically set
    values of controls without knowing their type until runtime. How can I gain
    access to loop ALL the properties of a control?

    Dim t As Type = ctl.GetType
    For Each pp As PropertyInfo In t.GetProperties ()
    Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
    Next





  • Alex Meleta

    #2
    Re: Loop ALL Properties

    Hi Derek,

    Did I get it right that MyTextBox.Prope rties is sort of your own array of
    'properties', like dictionary? If so, that how it relates to 'real properties',
    'native' for a class?

    Regards, Alex Meleta
    [TechBlog] http://devkids.blogspot.com
    i
    >

    Comment

    • James Hahn

      #3
      Re: Loop ALL Properties

      You should be looping recursively.

      "Derek Hart" <derekmhart@yah oo.comwrote in message
      news:eI87BIKOJH A.1172@TK2MSFTN GP03.phx.gbl...
      >I am going in circles trying to loop through properties on 3rd party
      >controls. For example, I have a textbox that has its maximum length located
      >at MyTextBox.Prope rties.MaxLength - instead of the dotnet textbox which is
      >MyTextBox.MaxL ength. If I loop a built in dotnet control, it finds the
      >property no problem. But looping through the 3rd party control,
      >Properties.Max Length does not get listed. I was hoping to find how it names
      >it using reflection, so I could use GetValue and SetValue to dyanmically
      >set values of controls without knowing their type until runtime. How can I
      >gain access to loop ALL the properties of a control?
      >
      Dim t As Type = ctl.GetType
      For Each pp As PropertyInfo In t.GetProperties ()
      Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
      Next
      >
      >
      >
      >
      >

      Comment

      • Derek Hart

        #4
        Re: Loop ALL Properties

        3rd Party Developer Express textbox control. It has many properties. Can't
        figure out how to loop and get all of them.

        "Alex Meleta" <ameleta@gmail. comwrote in message
        news:df84a49ff6 758cb071e44cbb1 e0@news.microso ft.com...
        Hi Derek,
        >
        Did I get it right that MyTextBox.Prope rties is sort of your own array of
        'properties', like dictionary? If so, that how it relates to 'real
        properties', 'native' for a class?
        >
        Regards, Alex Meleta
        [TechBlog] http://devkids.blogspot.com
        >
        >i
        >>
        >
        >

        Comment

        • Derek Hart

          #5
          Re: Loop ALL Properties

          Do you have a sample of how I would do that from my code below?

          "James Hahn" <jhahn@yahoo.co mwrote in message
          news:%23BLj6FLO JHA.648@TK2MSFT NGP06.phx.gbl.. .
          You should be looping recursively.
          >
          "Derek Hart" <derekmhart@yah oo.comwrote in message
          news:eI87BIKOJH A.1172@TK2MSFTN GP03.phx.gbl...
          >>I am going in circles trying to loop through properties on 3rd party
          >>controls. For example, I have a textbox that has its maximum length
          >>located at MyTextBox.Prope rties.MaxLength - instead of the dotnet textbox
          >>which is MyTextBox.MaxLe ngth. If I loop a built in dotnet control, it
          >>finds the property no problem. But looping through the 3rd party control,
          >>Properties.Ma xLength does not get listed. I was hoping to find how it
          >>names it using reflection, so I could use GetValue and SetValue to
          >>dyanmically set values of controls without knowing their type until
          >>runtime. How can I gain access to loop ALL the properties of a control?
          >>
          >Dim t As Type = ctl.GetType
          >For Each pp As PropertyInfo In t.GetProperties ()
          > Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
          >Next
          >>
          >>
          >>
          >>
          >>
          >

          Comment

          • James Hahn

            #6
            Re: Loop ALL Properties

            This is taken straight from
            http://msdn.microsoft.com/en-us/libr...pertyinfo.aspx.
            It's not actually recursive - you have to repeat it for each member type of
            interest.

            I created a picture box with some scroll bars, which is similar (I think) to
            your custom controls, and sent the output to a multiline text box.

            Imports System.Reflecti on
            Public Class Form1
            Dim indent As Integer = 0
            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            System.EventArg s) Handles Button1.Click
            Dim t As Type = PictureBox1.Get Type
            For Each pp As PropertyInfo In t.GetProperties ()
            listproperties( pp)
            Next
            End Sub
            Sub listproperties( ByVal pp As PropertyInfo)
            Display(indent, pp.Name)
            indent += 1
            For Each mi As MethodInfo In pp.GetAccessors
            ListAccessors(m i)
            Next
            indent -= 1
            End Sub
            Sub ListAccessors(B yVal mi As MethodInfo)
            Display(indent, mi.Name)
            End Sub
            Sub Display(ByVal indent As Int32, ByVal s As String)
            textBox1.Text += New String(" "c, indent * 2)
            textBox1.Text += s & vbCrLf
            End Sub
            End Class

            "Derek Hart" <derekmhart@yah oo.comwrote in message
            news:udjvSXLOJH A.1960@TK2MSFTN GP04.phx.gbl...
            Do you have a sample of how I would do that from my code below?
            >
            "James Hahn" <jhahn@yahoo.co mwrote in message
            news:%23BLj6FLO JHA.648@TK2MSFT NGP06.phx.gbl.. .
            >You should be looping recursively.
            >>
            >"Derek Hart" <derekmhart@yah oo.comwrote in message
            >news:eI87BIKOJ HA.1172@TK2MSFT NGP03.phx.gbl.. .
            >>>I am going in circles trying to loop through properties on 3rd party
            >>>controls. For example, I have a textbox that has its maximum length
            >>>located at MyTextBox.Prope rties.MaxLength - instead of the dotnet textbox
            >>>which is MyTextBox.MaxLe ngth. If I loop a built in dotnet control, it
            >>>finds the property no problem. But looping through the 3rd party control,
            >>>Properties.M axLength does not get listed. I was hoping to find how it
            >>>names it using reflection, so I could use GetValue and SetValue to
            >>>dyanmicall y set values of controls without knowing their type until
            >>>runtime. How can I gain access to loop ALL the properties of a control?
            >>>
            >>Dim t As Type = ctl.GetType
            >>For Each pp As PropertyInfo In t.GetProperties ()
            >> Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
            >>Next
            >>>
            >>>
            >>>
            >>>
            >>>
            >>
            >
            >

            Comment

            • Derek Hart

              #7
              Re: Loop ALL Properties

              More on the right track, but I cannot seem to get to all the properties. In
              the dev express controls, on a textboxedit control, in the property sheet,
              there is a property called Properties. I expand that and see a MaxLength
              property. So the property would be textbox1.proper ties.MaxLength - cannot
              seem to loop through and get the names of all the properties, so I can find
              how this one is named, and use it at runtime to set values to it, because I
              will not know the type of control until runtime. And I don't want to convert
              the control because there are too many controls to do this for. So if the
              property exists, I want to do a setvalue on it. Any ideas on how to find all
              properties?

              "James Hahn" <jhahn@yahoo.co mwrote in message
              news:O3W2h3LOJH A.2912@TK2MSFTN GP03.phx.gbl...
              This is taken straight from
              http://msdn.microsoft.com/en-us/libr...pertyinfo.aspx.
              It's not actually recursive - you have to repeat it for each member type
              of interest.
              >
              I created a picture box with some scroll bars, which is similar (I think)
              to your custom controls, and sent the output to a multiline text box.
              >
              Imports System.Reflecti on
              Public Class Form1
              Dim indent As Integer = 0
              Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              System.EventArg s) Handles Button1.Click
              Dim t As Type = PictureBox1.Get Type
              For Each pp As PropertyInfo In t.GetProperties ()
              listproperties( pp)
              Next
              End Sub
              Sub listproperties( ByVal pp As PropertyInfo)
              Display(indent, pp.Name)
              indent += 1
              For Each mi As MethodInfo In pp.GetAccessors
              ListAccessors(m i)
              Next
              indent -= 1
              End Sub
              Sub ListAccessors(B yVal mi As MethodInfo)
              Display(indent, mi.Name)
              End Sub
              Sub Display(ByVal indent As Int32, ByVal s As String)
              textBox1.Text += New String(" "c, indent * 2)
              textBox1.Text += s & vbCrLf
              End Sub
              End Class
              >
              "Derek Hart" <derekmhart@yah oo.comwrote in message
              news:udjvSXLOJH A.1960@TK2MSFTN GP04.phx.gbl...
              >Do you have a sample of how I would do that from my code below?
              >>
              >"James Hahn" <jhahn@yahoo.co mwrote in message
              >news:%23BLj6FL OJHA.648@TK2MSF TNGP06.phx.gbl. ..
              >>You should be looping recursively.
              >>>
              >>"Derek Hart" <derekmhart@yah oo.comwrote in message
              >>news:eI87BIKO JHA.1172@TK2MSF TNGP03.phx.gbl. ..
              >>>>I am going in circles trying to loop through properties on 3rd party
              >>>>controls. For example, I have a textbox that has its maximum length
              >>>>located at MyTextBox.Prope rties.MaxLength - instead of the dotnet
              >>>>textbox which is MyTextBox.MaxLe ngth. If I loop a built in dotnet
              >>>>control, it finds the property no problem. But looping through the 3rd
              >>>>party control, Properties.MaxL ength does not get listed. I was hoping to
              >>>>find how it names it using reflection, so I could use GetValue and
              >>>>SetValue to dyanmically set values of controls without knowing their
              >>>>type until runtime. How can I gain access to loop ALL the properties of
              >>>>a control?
              >>>>
              >>>Dim t As Type = ctl.GetType
              >>>For Each pp As PropertyInfo In t.GetProperties ()
              >>> Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
              >>>Next
              >>>>
              >>>>
              >>>>
              >>>>
              >>>>
              >>>
              >>
              >>
              >

              Comment

              • James Hahn

                #8
                Re: Loop ALL Properties

                I don't think that .Properties and .Properties.Max Length can both be
                properties. How are these members defined within the class?

                "Derek Hart" <derekmhart@yah oo.comwrote in message
                news:%234ilosMO JHA.1148@TK2MSF TNGP05.phx.gbl. ..
                More on the right track, but I cannot seem to get to all the properties.
                In the dev express controls, on a textboxedit control, in the property
                sheet, there is a property called Properties. I expand that and see a
                MaxLength property. So the property would be
                textbox1.proper ties.MaxLength - cannot seem to loop through and get the
                names of all the properties, so I can find how this one is named, and use
                it at runtime to set values to it, because I will not know the type of
                control until runtime. And I don't want to convert the control because
                there are too many controls to do this for. So if the property exists, I
                want to do a setvalue on it. Any ideas on how to find all properties?
                >
                "James Hahn" <jhahn@yahoo.co mwrote in message
                news:O3W2h3LOJH A.2912@TK2MSFTN GP03.phx.gbl...
                >This is taken straight from
                >http://msdn.microsoft.com/en-us/libr...pertyinfo.aspx.
                >It's not actually recursive - you have to repeat it for each member type
                >of interest.
                >>
                >I created a picture box with some scroll bars, which is similar (I think)
                >to your custom controls, and sent the output to a multiline text box.
                >>
                >Imports System.Reflecti on
                >Public Class Form1
                >Dim indent As Integer = 0
                >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                >System.EventAr gs) Handles Button1.Click
                >Dim t As Type = PictureBox1.Get Type
                >For Each pp As PropertyInfo In t.GetProperties ()
                >listproperties (pp)
                >Next
                >End Sub
                >Sub listproperties( ByVal pp As PropertyInfo)
                >Display(indent , pp.Name)
                >indent += 1
                >For Each mi As MethodInfo In pp.GetAccessors
                >ListAccessors( mi)
                >Next
                >indent -= 1
                >End Sub
                >Sub ListAccessors(B yVal mi As MethodInfo)
                >Display(indent , mi.Name)
                >End Sub
                >Sub Display(ByVal indent As Int32, ByVal s As String)
                >textBox1.Tex t += New String(" "c, indent * 2)
                >textBox1.Tex t += s & vbCrLf
                >End Sub
                >End Class
                >>
                >"Derek Hart" <derekmhart@yah oo.comwrote in message
                >news:udjvSXLOJ HA.1960@TK2MSFT NGP04.phx.gbl.. .
                >>Do you have a sample of how I would do that from my code below?
                >>>
                >>"James Hahn" <jhahn@yahoo.co mwrote in message
                >>news:%23BLj6F LOJHA.648@TK2MS FTNGP06.phx.gbl ...
                >>>You should be looping recursively.
                >>>>
                >>>"Derek Hart" <derekmhart@yah oo.comwrote in message
                >>>news:eI87BIK OJHA.1172@TK2MS FTNGP03.phx.gbl ...
                >>>>>I am going in circles trying to loop through properties on 3rd party
                >>>>>controls . For example, I have a textbox that has its maximum length
                >>>>>located at MyTextBox.Prope rties.MaxLength - instead of the dotnet
                >>>>>textbox which is MyTextBox.MaxLe ngth. If I loop a built in dotnet
                >>>>>control, it finds the property no problem. But looping through the 3rd
                >>>>>party control, Properties.MaxL ength does not get listed. I was hoping
                >>>>>to find how it names it using reflection, so I could use GetValue and
                >>>>>SetValue to dyanmically set values of controls without knowing their
                >>>>>type until runtime. How can I gain access to loop ALL the properties of
                >>>>>a control?
                >>>>>
                >>>>Dim t As Type = ctl.GetType
                >>>>For Each pp As PropertyInfo In t.GetProperties ()
                >>>> Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
                >>>>Next
                >>>>>
                >>>>>
                >>>>>
                >>>>>
                >>>>>
                >>>>
                >>>
                >>>
                >>
                >
                >

                Comment

                • Jeff Johnson

                  #9
                  Re: Loop ALL Properties

                  "James Hahn" <jhahn@yahoo.co mwrote in message
                  news:uonwwHNOJH A.1744@TK2MSFTN GP06.phx.gbl...
                  >I don't think that .Properties and .Properties.Max Length can both be
                  >properties.
                  Why not? The Properties property probably just returns a class, which itself
                  has properties.


                  Comment

                  • James Hahn

                    #10
                    Re: Loop ALL Properties

                    That's why I believe recursion should work. But I can't see a way to create
                    a propertyinfo from a property type. If I could do that, then it would solve
                    the problem. Perhaps I should have said "Reflection apparently doesn't
                    support both Properties and Properties.Maxl ength as properties of the same
                    object" (which is the way that the user thinks of them). Some additional
                    process is required to get access to the properties of a property.

                    "Jeff Johnson" <i.get@enough.s pamwrote in message
                    news:evmk$NQOJH A.4088@TK2MSFTN GP02.phx.gbl...
                    "James Hahn" <jhahn@yahoo.co mwrote in message
                    news:uonwwHNOJH A.1744@TK2MSFTN GP06.phx.gbl...
                    >
                    >>I don't think that .Properties and .Properties.Max Length can both be
                    >>properties.
                    >
                    Why not? The Properties property probably just returns a class, which
                    itself has properties.
                    >

                    Comment

                    • Hauer W

                      #11
                      Re: Loop ALL Properties

                      Hi!

                      I also use in my apps the dx controls. I do in my loop over all controls a
                      simple "select case" to difference beetween the differnt controltypes and
                      then i set the properties.

                      That works pretty good.


                      HTH Wolfgang
                      "Derek Hart" <derekmhart@yah oo.comschrieb im Newsbeitrag
                      news:%234ilosMO JHA.1148@TK2MSF TNGP05.phx.gbl. ..
                      More on the right track, but I cannot seem to get to all the properties.
                      In the dev express controls, on a textboxedit control, in the property
                      sheet, there is a property called Properties. I expand that and see a
                      MaxLength property. So the property would be
                      textbox1.proper ties.MaxLength - cannot seem to loop through and get the
                      names of all the properties, so I can find how this one is named, and use
                      it at runtime to set values to it, because I will not know the type of
                      control until runtime. And I don't want to convert the control because
                      there are too many controls to do this for. So if the property exists, I
                      want to do a setvalue on it. Any ideas on how to find all properties?
                      >
                      "James Hahn" <jhahn@yahoo.co mwrote in message
                      news:O3W2h3LOJH A.2912@TK2MSFTN GP03.phx.gbl...
                      >This is taken straight from
                      >http://msdn.microsoft.com/en-us/libr...pertyinfo.aspx.
                      >It's not actually recursive - you have to repeat it for each member type
                      >of interest.
                      >>
                      >I created a picture box with some scroll bars, which is similar (I think)
                      >to your custom controls, and sent the output to a multiline text box.
                      >>
                      >Imports System.Reflecti on
                      >Public Class Form1
                      >Dim indent As Integer = 0
                      >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                      >System.EventAr gs) Handles Button1.Click
                      >Dim t As Type = PictureBox1.Get Type
                      >For Each pp As PropertyInfo In t.GetProperties ()
                      >listproperties (pp)
                      >Next
                      >End Sub
                      >Sub listproperties( ByVal pp As PropertyInfo)
                      >Display(indent , pp.Name)
                      >indent += 1
                      >For Each mi As MethodInfo In pp.GetAccessors
                      >ListAccessors( mi)
                      >Next
                      >indent -= 1
                      >End Sub
                      >Sub ListAccessors(B yVal mi As MethodInfo)
                      >Display(indent , mi.Name)
                      >End Sub
                      >Sub Display(ByVal indent As Int32, ByVal s As String)
                      >textBox1.Tex t += New String(" "c, indent * 2)
                      >textBox1.Tex t += s & vbCrLf
                      >End Sub
                      >End Class
                      >>
                      >"Derek Hart" <derekmhart@yah oo.comwrote in message
                      >news:udjvSXLOJ HA.1960@TK2MSFT NGP04.phx.gbl.. .
                      >>Do you have a sample of how I would do that from my code below?
                      >>>
                      >>"James Hahn" <jhahn@yahoo.co mwrote in message
                      >>news:%23BLj6F LOJHA.648@TK2MS FTNGP06.phx.gbl ...
                      >>>You should be looping recursively.
                      >>>>
                      >>>"Derek Hart" <derekmhart@yah oo.comwrote in message
                      >>>news:eI87BIK OJHA.1172@TK2MS FTNGP03.phx.gbl ...
                      >>>>>I am going in circles trying to loop through properties on 3rd party
                      >>>>>controls . For example, I have a textbox that has its maximum length
                      >>>>>located at MyTextBox.Prope rties.MaxLength - instead of the dotnet
                      >>>>>textbox which is MyTextBox.MaxLe ngth. If I loop a built in dotnet
                      >>>>>control, it finds the property no problem. But looping through the 3rd
                      >>>>>party control, Properties.MaxL ength does not get listed. I was hoping
                      >>>>>to find how it names it using reflection, so I could use GetValue and
                      >>>>>SetValue to dyanmically set values of controls without knowing their
                      >>>>>type until runtime. How can I gain access to loop ALL the properties of
                      >>>>>a control?
                      >>>>>
                      >>>>Dim t As Type = ctl.GetType
                      >>>>For Each pp As PropertyInfo In t.GetProperties ()
                      >>>> Console.WriteLi ne("{0} = {1}", pp.Name, pp.GetValue(ctl , Nothing))
                      >>>>Next
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>
                      >>>
                      >>>
                      >>
                      >
                      >

                      Comment

                      Working...