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 a control from a string

referencing a control from a string

Scheduled Pinned Locked Moved C#
comhelpquestion
10 Posts 3 Posters 3 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.
  • T Offline
    T Offline
    Tom Wright
    wrote on last edited by
    #1

    I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:

        private void OnCheckedChange(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked && rb.Text.Contains("Run"))
            {
                OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
            }
            else
            {
                OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
            }
        }
    
        private void OnLoadPictureImage(String picBox, Boolean ObjectStatus)
        {
            try
            {
                PictureBox pb = new PictureBox();
                pb = (PictureBox)this.Controls\[picBox\];
                if (ObjectStatus)
                {
                    Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open);
                    Image temp = Image.FromStream(s);
                    s.Close();
                    pb.Image = temp;
                }
                else
                {
                    Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open);
                    Image temp = Image.FromStream(s);
                    s.Close();
                    pb.Image = temp;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Error finding image: " + err.Message);
            }
        }
    

    Is this not possible? Thanks Tom

    Tom Wright tawright915@gmail.com

    J X 2 Replies Last reply
    0
    • T Tom Wright

      I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:

          private void OnCheckedChange(object sender, EventArgs e)
          {
              RadioButton rb = (RadioButton)sender;
              if (rb.Checked && rb.Text.Contains("Run"))
              {
                  OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
              }
              else
              {
                  OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
              }
          }
      
          private void OnLoadPictureImage(String picBox, Boolean ObjectStatus)
          {
              try
              {
                  PictureBox pb = new PictureBox();
                  pb = (PictureBox)this.Controls\[picBox\];
                  if (ObjectStatus)
                  {
                      Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open);
                      Image temp = Image.FromStream(s);
                      s.Close();
                      pb.Image = temp;
                  }
                  else
                  {
                      Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open);
                      Image temp = Image.FromStream(s);
                      s.Close();
                      pb.Image = temp;
                  }
              }
              catch (Exception err)
              {
                  MessageBox.Show("Error finding image: " + err.Message);
              }
          }
      

      Is this not possible? Thanks Tom

      Tom Wright tawright915@gmail.com

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      why not just store a reference to the PictureBox in the Tag? [edit] I may be making assumptions based on the limited scope of the code snippet, but shouldn't this

      OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);

      instead read

      OnLoadPictureImage(rb.Tag.ToString(), rb.Checked);

      Why create a new PictureBox inside of OnLoadPictureImage here:

      PictureBox pb = new PictureBox();

      pb is immediately overwritten to another PictureBox so wouldn't it be better to set it to null and not waste the time or resources creating the throw-away object?


      Last modified: 16mins after originally posted --

      :Badger:

      T 1 Reply Last reply
      0
      • J Jimmanuel

        why not just store a reference to the PictureBox in the Tag? [edit] I may be making assumptions based on the limited scope of the code snippet, but shouldn't this

        OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);

        instead read

        OnLoadPictureImage(rb.Tag.ToString(), rb.Checked);

        Why create a new PictureBox inside of OnLoadPictureImage here:

        PictureBox pb = new PictureBox();

        pb is immediately overwritten to another PictureBox so wouldn't it be better to set it to null and not waste the time or resources creating the throw-away object?


        Last modified: 16mins after originally posted --

        :Badger:

        T Offline
        T Offline
        Tom Wright
        wrote on last edited by
        #3

        I did it that way because when I reference the PictureBox.image property I received and error that the object had not been instantiated. Either way it's still null...almost like it cannot find it. Oh...and I did have it like this: OnLoadPictureImage(rb.Tag.ToString(), rb.Checked); Just doing some debugging when I thought I'd post this to the forum and I forgot to set it back like I had it. Thanks for the help. Tom

        Tom Wright tawright915@gmail.com

        T 1 Reply Last reply
        0
        • T Tom Wright

          I did it that way because when I reference the PictureBox.image property I received and error that the object had not been instantiated. Either way it's still null...almost like it cannot find it. Oh...and I did have it like this: OnLoadPictureImage(rb.Tag.ToString(), rb.Checked); Just doing some debugging when I thought I'd post this to the forum and I forgot to set it back like I had it. Thanks for the help. Tom

          Tom Wright tawright915@gmail.com

          T Offline
          T Offline
          Tom Wright
          wrote on last edited by
          #4

          The exact error was: Object reference not set to an instance of an object. T

          Tom Wright tawright915@gmail.com

          J 1 Reply Last reply
          0
          • T Tom Wright

            The exact error was: Object reference not set to an instance of an object. T

            Tom Wright tawright915@gmail.com

            J Offline
            J Offline
            Jimmanuel
            wrote on last edited by
            #5

            where? and in response to your original question,

            pb = (PictureBox)this.Controls[picBox];

            should work fine just fine as long as this.Controls[picBox] actually is a PictureBox. Doing a safe cast (this.Controls[picBox] as PictureBox) and then checking for null would be a safer way to do it.

            :Badger:

            T 1 Reply Last reply
            0
            • J Jimmanuel

              where? and in response to your original question,

              pb = (PictureBox)this.Controls[picBox];

              should work fine just fine as long as this.Controls[picBox] actually is a PictureBox. Doing a safe cast (this.Controls[picBox] as PictureBox) and then checking for null would be a safer way to do it.

              :Badger:

              T Offline
              T Offline
              Tom Wright
              wrote on last edited by
              #6

              pb.Image = temp; <----- Here....when I try and set the image.

              Tom Wright tawright915@gmail.com

              J 2 Replies Last reply
              0
              • T Tom Wright

                pb.Image = temp; <----- Here....when I try and set the image.

                Tom Wright tawright915@gmail.com

                J Offline
                J Offline
                Jimmanuel
                wrote on last edited by
                #7

                and pb is what's null? a safe cast (the as operator) and null check would be better than C-style cast and no null check, and as long as the object names are stored correctly in the Tags then it should work.

                :Badger:

                1 Reply Last reply
                0
                • T Tom Wright

                  I have a number of PictureBox controls that show a Street Stop Light. Each Picturebox control is controled by two radio buttons for a total of 8 picturebox control and 16 radio buttons. One for Run (turns the Stop Light Green) One for Stop (turns the light red). I am storing the name of the PictureBox control in the tag of it respective radio buttons. Can I take this string and use it to reference the Picturebox control and change the image? Here is me code:

                      private void OnCheckedChange(object sender, EventArgs e)
                      {
                          RadioButton rb = (RadioButton)sender;
                          if (rb.Checked && rb.Text.Contains("Run"))
                          {
                              OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
                          }
                          else
                          {
                              OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked);
                          }
                      }
                  
                      private void OnLoadPictureImage(String picBox, Boolean ObjectStatus)
                      {
                          try
                          {
                              PictureBox pb = new PictureBox();
                              pb = (PictureBox)this.Controls\[picBox\];
                              if (ObjectStatus)
                              {
                                  Stream s = File.Open("../images/traffic-light-green.jpg", FileMode.Open);
                                  Image temp = Image.FromStream(s);
                                  s.Close();
                                  pb.Image = temp;
                              }
                              else
                              {
                                  Stream s = File.Open("../images/traffic-light-red.jpg", FileMode.Open);
                                  Image temp = Image.FromStream(s);
                                  s.Close();
                                  pb.Image = temp;
                              }
                          }
                          catch (Exception err)
                          {
                              MessageBox.Show("Error finding image: " + err.Message);
                          }
                      }
                  

                  Is this not possible? Thanks Tom

                  Tom Wright tawright915@gmail.com

                  X Offline
                  X Offline
                  xcorporation
                  wrote on last edited by
                  #8

                  private void Form1_Load(object sender, EventArgs e)
                  {
                  for (int x = 0; x < this.Controls.Count; ++x)
                  {
                  for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
                  {
                  if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
                  {
                  RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];
                  _rdbtn.CheckedChanged += new EventHandler(_rdbtn_CheckedChanged);
                  }
                  }
                  }
                  }

                  void _rdbtn_CheckedChanged(object sender, EventArgs e)
                  {
                  for (int x = 0; x < this.Controls.Count; ++x)
                  {
                  for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
                  {
                  if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
                  {
                  // Founded the radio button
                  RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];

                       if (\_rdbtn.Checked == true)
                       {
                         for (int z = 0; z < this.Controls\[x\].Controls.Count; ++z)
                         {
                          if (this.Controls\[x\].Controls\[z\].GetType() == typeof(PictureBox))
                          {
                            // Change to green
                  
                            PictureBox \_pic = (PictureBox)this.Controls\[x\].Controls\[z\];
                  
                            if (\_pic.Tag == "Hi")
                            {
                             // this is the one you want to work with...
                  
                            }
                          }
                         }
                       }
                      }
                    }
                   }
                  

                  }

                  Take note... if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } will always execute irrelevant true or false. i assume one is 'Unchecked' This code should help only if you have the radio / image / etc. on panels. remove the 2nd layer of loops if you not using panels... Regards, X

                  Yeee :cool:

                  modified on Tuesday, May 19, 2009 5:17 PM

                  T 1 Reply Last reply
                  0
                  • T Tom Wright

                    pb.Image = temp; <----- Here....when I try and set the image.

                    Tom Wright tawright915@gmail.com

                    J Offline
                    J Offline
                    Jimmanuel
                    wrote on last edited by
                    #9

                    modification to my previous statement: in order for this to work:

                    pb = (PictureBox)this.Controls[picBox];

                    the picture box needs to be parented by the main form. If it exists on a child panel or a tab control or something like that then it won't exist in the main forms control collection. You'll have to recursively search the this.Control objects to find it. Like I said before, why not just store the picture box itself in the tag of the radio button? Then you'd have

                    PictureBox pb = rb.Tag as PictureBox;

                    and you'd be done.

                    :Badger:

                    1 Reply Last reply
                    0
                    • X xcorporation

                      private void Form1_Load(object sender, EventArgs e)
                      {
                      for (int x = 0; x < this.Controls.Count; ++x)
                      {
                      for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
                      {
                      if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
                      {
                      RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];
                      _rdbtn.CheckedChanged += new EventHandler(_rdbtn_CheckedChanged);
                      }
                      }
                      }
                      }

                      void _rdbtn_CheckedChanged(object sender, EventArgs e)
                      {
                      for (int x = 0; x < this.Controls.Count; ++x)
                      {
                      for (int y = 0; y < this.Controls[x].Controls.Count; ++y)
                      {
                      if (this.Controls[x].Controls[y].GetType() == typeof(RadioButton))
                      {
                      // Founded the radio button
                      RadioButton _rdbtn = (RadioButton)this.Controls[x].Controls[y];

                           if (\_rdbtn.Checked == true)
                           {
                             for (int z = 0; z < this.Controls\[x\].Controls.Count; ++z)
                             {
                              if (this.Controls\[x\].Controls\[z\].GetType() == typeof(PictureBox))
                              {
                                // Change to green
                      
                                PictureBox \_pic = (PictureBox)this.Controls\[x\].Controls\[z\];
                      
                                if (\_pic.Tag == "Hi")
                                {
                                 // this is the one you want to work with...
                      
                                }
                              }
                             }
                           }
                          }
                        }
                       }
                      

                      }

                      Take note... if (rb.Checked && rb.Text.Contains("Run")) { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } else { OnLoadPictureImage(rb.Tag.ToString(), radioButton1.Checked); } will always execute irrelevant true or false. i assume one is 'Unchecked' This code should help only if you have the radio / image / etc. on panels. remove the 2nd layer of loops if you not using panels... Regards, X

                      Yeee :cool:

                      modified on Tuesday, May 19, 2009 5:17 PM

                      T Offline
                      T Offline
                      Tom Wright
                      wrote on last edited by
                      #10

                      I see. I did not know that I had to iterate through all of the controls. Currently the Radio buttons are in group fields. I have 6 sets of Run/Stop radiobuttons on my form in 6 group fields. I ran into a problem where they were not in group fields and then only one radiobutton was allowed to be selected from the rest. Thanks for the help. Tom

                      Tom Wright tawright915@gmail.com

                      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