Find control by name from Windows Forms controls











up vote
33
down vote

favorite
12












I have a list of my textbox names, and I want to find a control by name. How is it possible?










share|improve this question




























    up vote
    33
    down vote

    favorite
    12












    I have a list of my textbox names, and I want to find a control by name. How is it possible?










    share|improve this question


























      up vote
      33
      down vote

      favorite
      12









      up vote
      33
      down vote

      favorite
      12






      12





      I have a list of my textbox names, and I want to find a control by name. How is it possible?










      share|improve this question















      I have a list of my textbox names, and I want to find a control by name. How is it possible?







      c# .net winforms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 9 '15 at 9:17









      Shadow Wizard

      56.9k18107173




      56.9k18107173










      asked Oct 10 '10 at 1:21









      krunal shah

      10.4k2180128




      10.4k2180128
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          84
          down vote



          accepted










          Use Control.ControlCollection.Find.



          TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
          tbx.Text = "found!";


          EDIT for asker:



          Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
          if (tbxs != null && tbxs.Length > 0)
          {
          tbxs[0].Text = "Found!";
          }





          share|improve this answer























          • TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
            – krunal shah
            Oct 10 '10 at 1:37










          • Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
            – krunal shah
            Oct 10 '10 at 1:43












          • Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
            – bla
            Oct 10 '10 at 1:45












          • No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
            – krunal shah
            Oct 10 '10 at 1:49






          • 3




            TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
            – bla
            Oct 10 '10 at 1:56


















          up vote
          9
          down vote













          You can use:



          f.Controls[name];


          Where f is your form variable. That gives you the control with name name.






          share|improve this answer

















          • 5




            Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
            – Michael Petrotta
            Oct 10 '10 at 1:30










          • @Michael: That is correct.
            – CesarGon
            Oct 10 '10 at 1:32


















          up vote
          3
          down vote













          TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
          tbx.Text = "found!";


          If Controls.Find is not found "textBox1" => error. You must add code.



          If(tbx != null)


          Edit:



          TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
          If(tbx != null)
          tbx.Text = "found!";





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3898588%2ffind-control-by-name-from-windows-forms-controls%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            84
            down vote



            accepted










            Use Control.ControlCollection.Find.



            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            tbx.Text = "found!";


            EDIT for asker:



            Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
            if (tbxs != null && tbxs.Length > 0)
            {
            tbxs[0].Text = "Found!";
            }





            share|improve this answer























            • TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
              – krunal shah
              Oct 10 '10 at 1:37










            • Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
              – krunal shah
              Oct 10 '10 at 1:43












            • Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
              – bla
              Oct 10 '10 at 1:45












            • No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
              – krunal shah
              Oct 10 '10 at 1:49






            • 3




              TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
              – bla
              Oct 10 '10 at 1:56















            up vote
            84
            down vote



            accepted










            Use Control.ControlCollection.Find.



            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            tbx.Text = "found!";


            EDIT for asker:



            Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
            if (tbxs != null && tbxs.Length > 0)
            {
            tbxs[0].Text = "Found!";
            }





            share|improve this answer























            • TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
              – krunal shah
              Oct 10 '10 at 1:37










            • Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
              – krunal shah
              Oct 10 '10 at 1:43












            • Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
              – bla
              Oct 10 '10 at 1:45












            • No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
              – krunal shah
              Oct 10 '10 at 1:49






            • 3




              TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
              – bla
              Oct 10 '10 at 1:56













            up vote
            84
            down vote



            accepted







            up vote
            84
            down vote



            accepted






            Use Control.ControlCollection.Find.



            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            tbx.Text = "found!";


            EDIT for asker:



            Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
            if (tbxs != null && tbxs.Length > 0)
            {
            tbxs[0].Text = "Found!";
            }





            share|improve this answer














            Use Control.ControlCollection.Find.



            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            tbx.Text = "found!";


            EDIT for asker:



            Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
            if (tbxs != null && tbxs.Length > 0)
            {
            tbxs[0].Text = "Found!";
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 5 '13 at 17:52









            Carsten

            9,05353154




            9,05353154










            answered Oct 10 '10 at 1:27









            bla

            4,21711924




            4,21711924












            • TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
              – krunal shah
              Oct 10 '10 at 1:37










            • Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
              – krunal shah
              Oct 10 '10 at 1:43












            • Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
              – bla
              Oct 10 '10 at 1:45












            • No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
              – krunal shah
              Oct 10 '10 at 1:49






            • 3




              TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
              – bla
              Oct 10 '10 at 1:56


















            • TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
              – krunal shah
              Oct 10 '10 at 1:37










            • Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
              – krunal shah
              Oct 10 '10 at 1:43












            • Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
              – bla
              Oct 10 '10 at 1:45












            • No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
              – krunal shah
              Oct 10 '10 at 1:49






            • 3




              TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
              – bla
              Oct 10 '10 at 1:56
















            TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
            – krunal shah
            Oct 10 '10 at 1:37




            TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
            – krunal shah
            Oct 10 '10 at 1:37












            Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
            – krunal shah
            Oct 10 '10 at 1:43






            Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
            – krunal shah
            Oct 10 '10 at 1:43














            Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
            – bla
            Oct 10 '10 at 1:45






            Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
            – bla
            Oct 10 '10 at 1:45














            No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
            – krunal shah
            Oct 10 '10 at 1:49




            No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
            – krunal shah
            Oct 10 '10 at 1:49




            3




            3




            TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
            – bla
            Oct 10 '10 at 1:56




            TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
            – bla
            Oct 10 '10 at 1:56












            up vote
            9
            down vote













            You can use:



            f.Controls[name];


            Where f is your form variable. That gives you the control with name name.






            share|improve this answer

















            • 5




              Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
              – Michael Petrotta
              Oct 10 '10 at 1:30










            • @Michael: That is correct.
              – CesarGon
              Oct 10 '10 at 1:32















            up vote
            9
            down vote













            You can use:



            f.Controls[name];


            Where f is your form variable. That gives you the control with name name.






            share|improve this answer

















            • 5




              Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
              – Michael Petrotta
              Oct 10 '10 at 1:30










            • @Michael: That is correct.
              – CesarGon
              Oct 10 '10 at 1:32













            up vote
            9
            down vote










            up vote
            9
            down vote









            You can use:



            f.Controls[name];


            Where f is your form variable. That gives you the control with name name.






            share|improve this answer












            You can use:



            f.Controls[name];


            Where f is your form variable. That gives you the control with name name.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 10 '10 at 1:26









            CesarGon

            12k54678




            12k54678








            • 5




              Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
              – Michael Petrotta
              Oct 10 '10 at 1:30










            • @Michael: That is correct.
              – CesarGon
              Oct 10 '10 at 1:32














            • 5




              Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
              – Michael Petrotta
              Oct 10 '10 at 1:30










            • @Michael: That is correct.
              – CesarGon
              Oct 10 '10 at 1:32








            5




            5




            Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
            – Michael Petrotta
            Oct 10 '10 at 1:30




            Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
            – Michael Petrotta
            Oct 10 '10 at 1:30












            @Michael: That is correct.
            – CesarGon
            Oct 10 '10 at 1:32




            @Michael: That is correct.
            – CesarGon
            Oct 10 '10 at 1:32










            up vote
            3
            down vote













            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            tbx.Text = "found!";


            If Controls.Find is not found "textBox1" => error. You must add code.



            If(tbx != null)


            Edit:



            TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
            If(tbx != null)
            tbx.Text = "found!";





            share|improve this answer

























              up vote
              3
              down vote













              TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
              tbx.Text = "found!";


              If Controls.Find is not found "textBox1" => error. You must add code.



              If(tbx != null)


              Edit:



              TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
              If(tbx != null)
              tbx.Text = "found!";





              share|improve this answer























                up vote
                3
                down vote










                up vote
                3
                down vote









                TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
                tbx.Text = "found!";


                If Controls.Find is not found "textBox1" => error. You must add code.



                If(tbx != null)


                Edit:



                TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
                If(tbx != null)
                tbx.Text = "found!";





                share|improve this answer












                TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
                tbx.Text = "found!";


                If Controls.Find is not found "textBox1" => error. You must add code.



                If(tbx != null)


                Edit:



                TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
                If(tbx != null)
                tbx.Text = "found!";






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 30 '15 at 4:04









                Nguyen Ngoc Quyen

                391




                391






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3898588%2ffind-control-by-name-from-windows-forms-controls%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen