Accessing a control if its name is in a string variable

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

    #1

    Accessing a control if its name is in a string variable

    I have the name of a control in a string variable and I want to change one
    of the controls properties. Right now I recursively scan all the controls
    on the form until I get one whose name matches the name in my string
    variable, then I know I have the correct control and can proceed to change
    the property.
    However all that searching seems like overkill. Does anyone know of a way
    to directly access the control if it's name is in a string variable?
    Something like an indirect access...
    Thanks for any advice...


  • Jan Hyde (VB MVP)

    #2
    Re: Accessing a control if its name is in a string variable

    "JohnR" <JohnR104@hotma il.com>'s wild thoughts were released
    on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
    fruit:
    >I have the name of a control in a string variable and I want to change one
    >of the controls properties. Right now I recursively scan all the controls
    >on the form until I get one whose name matches the name in my string
    >variable, then I know I have the correct control and can proceed to change
    >the property.
    >However all that searching seems like overkill. Does anyone know of a way
    >to directly access the control if it's name is in a string variable?
    >Something like an indirect access...
    >Thanks for any advice...

    Me.Controls("Te xtEdit1").Text = "Hello"
    --
    Jan Hyde


    Comment

    • Petar Atanasov

      #3
      Re: Accessing a control if its name is in a string variable

      JohnR wrote:
      I have the name of a control in a string variable and I want to change one
      of the controls properties. Right now I recursively scan all the controls
      on the form until I get one whose name matches the name in my string
      variable, then I know I have the correct control and can proceed to change
      the property.
      However all that searching seems like overkill. Does anyone know of a way
      to directly access the control if it's name is in a string variable?
      Something like an indirect access...
      Thanks for any advice...
      >
      >
      try Controls.Find [http://msdn2.microsoft.com/en-us/library/ms404295.aspx]
      Althought the MSDN sample is about keyed collectios, in the begining of
      the page is shown how to focus on control specified by it's name.

      Regards,
      Petar Atansov
      Daftar Situs Slot Gacor dengan RTP Tertinggi untuk Pemain Baru

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Accessing a control if its name is in a string variable

        Jan,

        Cylix is in my opinion doing it correct recursively, I assume he wants to
        find the controls on a panel to.

        Your me.controls shows in my idea only the controls on the top of the form.

        Although I did not check it, the Find is also to find in a collection, not
        to find a child in a collection.

        However I am not 100% sure about the last.

        Cor



        "Jan Hyde (VB MVP)" <StellaDrinker@ REMOVE.ME.uboot .comschreef in bericht
        news:a0ap83du5u ulloqj6b0mqauqh 43cbqpsa1@4ax.c om...
        "JohnR" <JohnR104@hotma il.com>'s wild thoughts were released
        on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
        fruit:
        >
        >>I have the name of a control in a string variable and I want to change one
        >>of the controls properties. Right now I recursively scan all the controls
        >>on the form until I get one whose name matches the name in my string
        >>variable, then I know I have the correct control and can proceed to change
        >>the property.
        >>However all that searching seems like overkill. Does anyone know of a way
        >>to directly access the control if it's name is in a string variable?
        >>Something like an indirect access...
        >>Thanks for any advice...
        >
        >
        Me.Controls("Te xtEdit1").Text = "Hello"
        --
        Jan Hyde
        >
        https://mvp.support.microsoft.com/profile/Jan.Hyde

        Comment

        • rowe_newsgroups

          #5
          Re: Accessing a control if its name is in a string variable

          On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
          wrote:
          Jan,
          >
          Cylix is in my opinion doing it correct recursively, I assume he wants to
          find the controls on a panel to.
          >
          Your me.controls shows in my idea only the controls on the top of the form.
          >
          Although I did not check it, the Find is also to find in a collection, not
          to find a child in a collection.
          >
          However I am not 100% sure about the last.
          >
          Cor
          >
          "Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in berichtnews:a0a p83du5uulloqj6b 0mqauqh43cbqpsa 1@4ax.com...
          >
          "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
          on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
          fruit:
          >
          >I have the name of a control in a string variable and I want to change one
          >of the controls properties. Right now I recursively scan all the controls
          >on the form until I get one whose name matches the name in my string
          >variable, then I know I have the correct control and can proceed to change
          >the property.
          >However all that searching seems like overkill. Does anyone know of a way
          >to directly access the control if it's name is in a string variable?
          >Something like an indirect access...
          >Thanks for any advice...
          >
          Me.Controls("Te xtEdit1").Text = "Hello"
          --
          Jan Hyde
          >
          Although I did not check it, the Find is also to find in a collection, not
          to find a child in a collection.
          Controls.Find has a searchAllChildr en bool parameter that you can use
          to specify a recursive search.

          Thanks,

          Seth Rowe

          Comment

          • JohnR

            #6
            Re: Accessing a control if its name is in a string variable

            Hi all,

            Thanks for the great replies. Looks like Control.find will do what I want,
            but that method was introduced in .NET 2.0 and we have not upgraded yet.
            So, I'll continue doing it manually (at least it works), until we upgrade
            later this year.
            Thanks again...

            "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
            news:1183634860 .118905.314130@ g4g2000hsf.goog legroups.com...
            On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
            wrote:
            >Jan,
            >>
            >Cylix is in my opinion doing it correct recursively, I assume he wants to
            >find the controls on a panel to.
            >>
            >Your me.controls shows in my idea only the controls on the top of the
            >form.
            >>
            >Although I did not check it, the Find is also to find in a collection,
            >not
            >to find a child in a collection.
            >>
            >However I am not 100% sure about the last.
            >>
            >Cor
            >>
            >"Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in
            >berichtnews:a0 ap83du5uulloqj6 b0mqauqh43cbqps a1@4ax.com...
            >>
            "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
            on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
            fruit:
            >>
            >>I have the name of a control in a string variable and I want to change
            >>one
            >>of the controls properties. Right now I recursively scan all the
            >>controls
            >>on the form until I get one whose name matches the name in my string
            >>variable, then I know I have the correct control and can proceed to
            >>change
            >>the property.
            >>However all that searching seems like overkill. Does anyone know of a
            >>way
            >>to directly access the control if it's name is in a string variable?
            >>Something like an indirect access...
            >>Thanks for any advice...
            >>
            Me.Controls("Te xtEdit1").Text = "Hello"
            --
            Jan Hyde
            >>
            >
            >
            >Although I did not check it, the Find is also to find in a collection,
            >not
            >to find a child in a collection.
            >
            Controls.Find has a searchAllChildr en bool parameter that you can use
            to specify a recursive search.
            >
            Thanks,
            >
            Seth Rowe
            >

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Accessing a control if its name is in a string variable

              Seth

              I had the plan to check it when I was home, but now you did it for me.

              Thanks

              Cor

              "rowe_newsgroup s" <rowe_email@yah oo.comschreef in bericht
              news:1183634860 .118905.314130@ g4g2000hsf.goog legroups.com...
              On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
              wrote:
              >Jan,
              >>
              >Cylix is in my opinion doing it correct recursively, I assume he wants to
              >find the controls on a panel to.
              >>
              >Your me.controls shows in my idea only the controls on the top of the
              >form.
              >>
              >Although I did not check it, the Find is also to find in a collection,
              >not
              >to find a child in a collection.
              >>
              >However I am not 100% sure about the last.
              >>
              >Cor
              >>
              >"Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in
              >berichtnews:a0 ap83du5uulloqj6 b0mqauqh43cbqps a1@4ax.com...
              >>
              "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
              on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
              fruit:
              >>
              >>I have the name of a control in a string variable and I want to change
              >>one
              >>of the controls properties. Right now I recursively scan all the
              >>controls
              >>on the form until I get one whose name matches the name in my string
              >>variable, then I know I have the correct control and can proceed to
              >>change
              >>the property.
              >>However all that searching seems like overkill. Does anyone know of a
              >>way
              >>to directly access the control if it's name is in a string variable?
              >>Something like an indirect access...
              >>Thanks for any advice...
              >>
              Me.Controls("Te xtEdit1").Text = "Hello"
              --
              Jan Hyde
              >>
              >
              >
              >Although I did not check it, the Find is also to find in a collection,
              >not
              >to find a child in a collection.
              >
              Controls.Find has a searchAllChildr en bool parameter that you can use
              to specify a recursive search.
              >
              Thanks,
              >
              Seth Rowe
              >

              Comment

              • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

                #8
                Re: Accessing a control if its name is in a string variable

                JohnR,

                If you need to do this for several controls, then you could scan all the
                controls on the form once, adding the control to a hashtable and using the
                control's name as the key.

                Once all the controls are in the hashtable, you could access them by the
                hashtable's key, the name of the control.

                But this won't gain you any performance benefit if you just need to access
                one control by name one time in your app.

                Kerry Moorman


                "JohnR" wrote:
                Hi all,
                >
                Thanks for the great replies. Looks like Control.find will do what I want,
                but that method was introduced in .NET 2.0 and we have not upgraded yet.
                So, I'll continue doing it manually (at least it works), until we upgrade
                later this year.
                Thanks again...
                >
                "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                news:1183634860 .118905.314130@ g4g2000hsf.goog legroups.com...
                On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                wrote:
                Jan,
                >
                Cylix is in my opinion doing it correct recursively, I assume he wants to
                find the controls on a panel to.
                >
                Your me.controls shows in my idea only the controls on the top of the
                form.
                >
                Although I did not check it, the Find is also to find in a collection,
                not
                to find a child in a collection.
                >
                However I am not 100% sure about the last.
                >
                Cor
                >
                "Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in
                berichtnews:a0a p83du5uulloqj6b 0mqauqh43cbqpsa 1@4ax.com...
                >
                "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
                on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
                fruit:
                >
                >I have the name of a control in a string variable and I want to change
                >one
                >of the controls properties. Right now I recursively scan all the
                >controls
                >on the form until I get one whose name matches the name in my string
                >variable, then I know I have the correct control and can proceed to
                >change
                >the property.
                >However all that searching seems like overkill. Does anyone know of a
                >way
                >to directly access the control if it's name is in a string variable?
                >Something like an indirect access...
                >Thanks for any advice...
                >
                Me.Controls("Te xtEdit1").Text = "Hello"
                --
                Jan Hyde
                >
                https://mvp.support.microsoft.com/profile/Jan.Hyde
                Although I did not check it, the Find is also to find in a collection,
                not
                to find a child in a collection.
                Controls.Find has a searchAllChildr en bool parameter that you can use
                to specify a recursive search.

                Thanks,

                Seth Rowe
                >
                >
                >

                Comment

                • JohnR

                  #9
                  Re: Accessing a control if its name is in a string variable

                  Hi Kerry,

                  Ya know, that's a great idea. That's exactly what I'll do.

                  Thanks, John

                  "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.comwrote in message
                  news:2A36B691-1932-4CF1-9558-DB0BE08E58D0@mi crosoft.com...
                  JohnR,
                  >
                  If you need to do this for several controls, then you could scan all the
                  controls on the form once, adding the control to a hashtable and using the
                  control's name as the key.
                  >
                  Once all the controls are in the hashtable, you could access them by the
                  hashtable's key, the name of the control.
                  >
                  But this won't gain you any performance benefit if you just need to access
                  one control by name one time in your app.
                  >
                  Kerry Moorman
                  >
                  >
                  "JohnR" wrote:
                  >
                  >Hi all,
                  >>
                  >Thanks for the great replies. Looks like Control.find will do what I
                  >want,
                  >but that method was introduced in .NET 2.0 and we have not upgraded yet.
                  >So, I'll continue doing it manually (at least it works), until we upgrade
                  >later this year.
                  >Thanks again...
                  >>
                  >"rowe_newsgrou ps" <rowe_email@yah oo.comwrote in message
                  >news:118363486 0.118905.314130 @g4g2000hsf.goo glegroups.com.. .
                  On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                  wrote:
                  >Jan,
                  >>
                  >Cylix is in my opinion doing it correct recursively, I assume he wants
                  >to
                  >find the controls on a panel to.
                  >>
                  >Your me.controls shows in my idea only the controls on the top of the
                  >form.
                  >>
                  >Although I did not check it, the Find is also to find in a
                  >collection,
                  >not
                  >to find a child in a collection.
                  >>
                  >However I am not 100% sure about the last.
                  >>
                  >Cor
                  >>
                  >"Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in
                  >berichtnews:a0 ap83du5uulloqj6 b0mqauqh43cbqps a1@4ax.com...
                  >>
                  "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
                  on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
                  fruit:
                  >>
                  >>I have the name of a control in a string variable and I want to
                  >>change
                  >>one
                  >>of the controls properties. Right now I recursively scan all the
                  >>controls
                  >>on the form until I get one whose name matches the name in my string
                  >>variable, then I know I have the correct control and can proceed to
                  >>change
                  >>the property.
                  >>However all that searching seems like overkill. Does anyone know of
                  >>a
                  >>way
                  >>to directly access the control if it's name is in a string variable?
                  >>Something like an indirect access...
                  >>Thanks for any advice...
                  >>
                  Me.Controls("Te xtEdit1").Text = "Hello"
                  --
                  Jan Hyde
                  >>
                  >https://mvp.support.microsoft.com/profile/Jan.Hyde
                  >
                  >
                  >Although I did not check it, the Find is also to find in a
                  >collection,
                  >not
                  >to find a child in a collection.
                  >
                  Controls.Find has a searchAllChildr en bool parameter that you can use
                  to specify a recursive search.
                  >
                  Thanks,
                  >
                  Seth Rowe
                  >
                  >>
                  >>
                  >>

                  Comment

                  • rowe_newsgroups

                    #10
                    Re: Accessing a control if its name is in a string variable

                    On Jul 5, 3:52 pm, Kerry Moorman
                    <KerryMoor...@d iscussions.micr osoft.comwrote:
                    JohnR,
                    >
                    If you need to do this for several controls, then you could scan all the
                    controls on the form once, adding the control to a hashtable and using the
                    control's name as the key.
                    >
                    Once all the controls are in the hashtable, you could access them by the
                    hashtable's key, the name of the control.
                    >
                    But this won't gain you any performance benefit if you just need to access
                    one control by name one time in your app.
                    >
                    Kerry Moorman
                    >
                    "JohnR" wrote:
                    Hi all,
                    >
                    Thanks for the great replies. Looks like Control.find will do what I want,
                    but that method was introduced in .NET 2.0 and we have not upgraded yet.
                    So, I'll continue doing it manually (at least it works), until we upgrade
                    later this year.
                    Thanks again...
                    >
                    "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                    news:1183634860 .118905.314130@ g4g2000hsf.goog legroups.com...
                    On Jul 5, 6:52 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                    wrote:
                    >Jan,
                    >
                    >Cylix is in my opinion doing it correct recursively, I assume he wants to
                    >find the controls on a panel to.
                    >
                    >Your me.controls shows in my idea only the controls on the top of the
                    >form.
                    >
                    >Although I did not check it, the Find is also to find in a collection,
                    >not
                    >to find a child in a collection.
                    >
                    >However I am not 100% sure about the last.
                    >
                    >Cor
                    >
                    >"Jan Hyde (VB MVP)" <StellaDrin...@ REMOVE.ME.uboot .comschreef in
                    >berichtnews:a0 ap83du5uulloqj6 b0mqauqh43cbqps a1@4ax.com...
                    >
                    "JohnR" <JohnR...@hotma il.com>'s wild thoughts were released
                    on Thu, 05 Jul 2007 05:02:56 GMT bearing the following
                    fruit:
                    >
                    >>I have the name of a control in a string variable and I want to change
                    >>one
                    >>of the controls properties. Right now I recursively scan all the
                    >>controls
                    >>on the form until I get one whose name matches the name in my string
                    >>variable, then I know I have the correct control and can proceed to
                    >>change
                    >>the property.
                    >>However all that searching seems like overkill. Does anyone know of a
                    >>way
                    >>to directly access the control if it's name is in a string variable?
                    >>Something like an indirect access...
                    >>Thanks for any advice...
                    >
                    Me.Controls("Te xtEdit1").Text = "Hello"
                    --
                    Jan Hyde
                    >>
                    >Although I did not check it, the Find is also to find in a collection,
                    >not
                    >to find a child in a collection.
                    >
                    Controls.Find has a searchAllChildr en bool parameter that you can use
                    to specify a recursive search.
                    >
                    Thanks,
                    >
                    Seth Rowe
                    You might also want to subscribe to the form's controladded event and
                    add any dynamically created controls that were created after your scan
                    to your hashtable.

                    Thanks,

                    Seth Rowe

                    Comment

                    Working...