Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Referencing an object - TextBox - ComboBox - by its name storeded as string

Referencing an object - TextBox - ComboBox - by its name storeded as string

Scheduled Pinned Locked Moved C#
questiondatabase
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fracalifa
    wrote on last edited by
    #1

    Hi all, I want to store the contents changes of my windows controls in a database. Therefore I determined the name of the object in the control_enter_method and save the name and value as string. Later in the case of canceling the changes I want to reassign the stored values from database back to the controls. How can I reference the control object by it's name (stored as string) ? My idea was

    object obj; //
    ((TextBox)obj).Name = stored_obj_name; //
    ((TextBox)obj).Text = stored_value; //

    but this does not work. what's to do ? Tnx in advance Frank

    G 1 Reply Last reply
    0
    • F fracalifa

      Hi all, I want to store the contents changes of my windows controls in a database. Therefore I determined the name of the object in the control_enter_method and save the name and value as string. Later in the case of canceling the changes I want to reassign the stored values from database back to the controls. How can I reference the control object by it's name (stored as string) ? My idea was

      object obj; //
      ((TextBox)obj).Name = stored_obj_name; //
      ((TextBox)obj).Text = stored_value; //

      but this does not work. what's to do ? Tnx in advance Frank

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.

      Giorgi Dalakishvili #region signature my articles #endregion

      M F 2 Replies Last reply
      0
      • G Giorgi Dalakishvili

        If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.

        Giorgi Dalakishvili #region signature my articles #endregion

        M Offline
        M Offline
        mark_w_
        wrote on last edited by
        #3

        just to put an example with Giorgi's reply :)

        this.Controls["textBox1"].Text = "Hello World";

        1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.

          Giorgi Dalakishvili #region signature my articles #endregion

          F Offline
          F Offline
          fracalifa
          wrote on last edited by
          #4

          Hi Giorgi, thanks for the reply. Is that the right solution to get the index ? I tried this

          int index = this.Controls.IndexOf((TextBox)sender);

          but get always index = -1; Where is my fault ? Tnx Frank

          M 1 Reply Last reply
          0
          • F fracalifa

            Hi Giorgi, thanks for the reply. Is that the right solution to get the index ? I tried this

            int index = this.Controls.IndexOf((TextBox)sender);

            but get always index = -1; Where is my fault ? Tnx Frank

            M Offline
            M Offline
            mark_w_
            wrote on last edited by
            #5

            int index = this.Controls.IndexOf(textBox1);

            works for me, where is your code, what is the sender?

            int index = this.Controls.IndexOf(null);

            gives me -1, so it sounds like you are not passing in a textbox that is in the Controls collection

            F 1 Reply Last reply
            0
            • M mark_w_

              int index = this.Controls.IndexOf(textBox1);

              works for me, where is your code, what is the sender?

              int index = this.Controls.IndexOf(null);

              gives me -1, so it sounds like you are not passing in a textbox that is in the Controls collection

              F Offline
              F Offline
              fracalifa
              wrote on last edited by
              #6

              The sender comes from the Textbox control enter method The Enter_Control method is in the Form class where also the Textbox exists.

              private void Enter_Control(object sender, System.EventArgs e)
              {
              try
              {
              switch(sender.GetType().ToString())
              {
              case ("System.Windows.Forms.TextBox"):
              int index = this.Controls.IndexOf((TextBox)sender);
              this.Controls[index].BackColor= Color.Blue;
              break;
              }
              }
              catch{}
              }

              Tnx Frank

              G 1 Reply Last reply
              0
              • F fracalifa

                The sender comes from the Textbox control enter method The Enter_Control method is in the Form class where also the Textbox exists.

                private void Enter_Control(object sender, System.EventArgs e)
                {
                try
                {
                switch(sender.GetType().ToString())
                {
                case ("System.Windows.Forms.TextBox"):
                int index = this.Controls.IndexOf((TextBox)sender);
                this.Controls[index].BackColor= Color.Blue;
                break;
                }
                }
                catch{}
                }

                Tnx Frank

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                You can do it like this:

                private void Enter_Control(object sender, System.EventArgs e)
                {
                if (sender is TextBox)
                {
                ((TextBox)sender).BackColor = Color.Blue;
                }
                }

                Giorgi Dalakishvili #region signature my articles #endregion

                F 1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  You can do it like this:

                  private void Enter_Control(object sender, System.EventArgs e)
                  {
                  if (sender is TextBox)
                  {
                  ((TextBox)sender).BackColor = Color.Blue;
                  }
                  }

                  Giorgi Dalakishvili #region signature my articles #endregion

                  F Offline
                  F Offline
                  fracalifa
                  wrote on last edited by
                  #8

                  Hi Giorgi, I had to break the discussion yesterday ... My code should only test that I have got the right index of the selected control and set the backcolor in this "long way" as test that it works. But the problem already exists, that I don't get an index != -1. This is main funcion of my method

                  int index = this.Controls.IndexOf((TextBox)sender);

                  tnx Frank

                  G 1 Reply Last reply
                  0
                  • F fracalifa

                    Hi Giorgi, I had to break the discussion yesterday ... My code should only test that I have got the right index of the selected control and set the backcolor in this "long way" as test that it works. But the problem already exists, that I don't get an index != -1. This is main funcion of my method

                    int index = this.Controls.IndexOf((TextBox)sender);

                    tnx Frank

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #9

                    Is the textbox on the form itself or is it on the panel or groupbbox or any other container? If yes, then instead of this.Controls you should have the container that contains your textbox.

                    Giorgi Dalakishvili #region signature my articles #endregion

                    F 1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      Is the textbox on the form itself or is it on the panel or groupbbox or any other container? If yes, then instead of this.Controls you should have the container that contains your textbox.

                      Giorgi Dalakishvili #region signature my articles #endregion

                      F Offline
                      F Offline
                      fracalifa
                      wrote on last edited by
                      #10

                      Oh yes, that's the problem. Due to the hierarchical structure of my Form each control is a container of controls. I tested my method and it works, for one container, but it doesn't help me with the problem. my intention was to reference a control by its name (as a string) independant of the location of the control. But the index is not unique and the prerequisite is the known of the hierarchical structure of my form. Changing the form would change the indices. Isn't there an other way ? txn Frank

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups