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. accessing objects in form by index or somthing like index

accessing objects in form by index or somthing like index

Scheduled Pinned Locked Moved C#
questiondatabasedata-structures
23 Posts 4 Posters 2 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.
  • L luisnike19

    use an ImageList, it's better http://msdn.microsoft.com/en-us/library/fat9bdzd(VS.80).aspx[^][^] :cool:

    luisnike19

    S Offline
    S Offline
    Sajjad Izadi
    wrote on last edited by
    #7

    thank you but isn't the ImageSize of pictures limited (256 * 256)? what is the solution? how can i have something like ImageList but with much bigger size of image? thanks again

    L 1 Reply Last reply
    0
    • S Sajjad Izadi

      thank you but isn't the ImageSize of pictures limited (256 * 256)? what is the solution? how can i have something like ImageList but with much bigger size of image? thanks again

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #8

      Do you need to display the images or just keep track of them inside a list? If you need to display them, then you could add PictureBox controls dynamically to the form at runtime, something like

      PictureBox pic = new PictureBox();
      pic.Image = ...; // load your image instance from where ever you wish
      Controls.Add(pic);

      If you just need a list then this will do the trick:

      List<Image> images = new List<Image>();
      images.Add(...); // add images

      regards

      S 1 Reply Last reply
      0
      • L Lost User

        Do you need to display the images or just keep track of them inside a list? If you need to display them, then you could add PictureBox controls dynamically to the form at runtime, something like

        PictureBox pic = new PictureBox();
        pic.Image = ...; // load your image instance from where ever you wish
        Controls.Add(pic);

        If you just need a list then this will do the trick:

        List<Image> images = new List<Image>();
        images.Add(...); // add images

        regards

        S Offline
        S Offline
        Sajjad Izadi
        wrote on last edited by
        #9

        i can solve my problem now but it is not optimized though :-D: i named my pictureboxes as: picturebox1, picturebox2, ... and picturebox16. now i want them in a list. i wrote the followings:

        PictureBox pic;
        for (int i = 1; i < 16; i++)
        {
        foreach (Control cont in this.Controls)
        {
        if (cont is PictureBox)
        {
        pic = cont as PictureBox;
        if (pic.Name.EndsWith(i.ToString()))
        images.Add(pic);
        }
        }
        }

        it works :) but do you suggest any thing more optimized? :doh: thanks

        L 1 Reply Last reply
        0
        • S Sajjad Izadi

          i can solve my problem now but it is not optimized though :-D: i named my pictureboxes as: picturebox1, picturebox2, ... and picturebox16. now i want them in a list. i wrote the followings:

          PictureBox pic;
          for (int i = 1; i < 16; i++)
          {
          foreach (Control cont in this.Controls)
          {
          if (cont is PictureBox)
          {
          pic = cont as PictureBox;
          if (pic.Name.EndsWith(i.ToString()))
          images.Add(pic);
          }
          }
          }

          it works :) but do you suggest any thing more optimized? :doh: thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #10

          Optimized? This is plain wrong ;) Right now your code will be executed 16 * Controls.Length times. The inner foreach loop will be executed for each i in the outer loop. Better keep a counter in the foreach loop like this:

          int picCounter = 1;
          foreach (Control cont in this.Controls)
          {
               if (cont is PictureBox)
               {
                     pic = cont as PictureBox;
                     if (pic.Name.EndsWith(picCounter.ToString()))
                            images.Add(pic);
          
                     picCounter++;
               }
          }
          
          S 1 Reply Last reply
          0
          • L Lost User

            Optimized? This is plain wrong ;) Right now your code will be executed 16 * Controls.Length times. The inner foreach loop will be executed for each i in the outer loop. Better keep a counter in the foreach loop like this:

            int picCounter = 1;
            foreach (Control cont in this.Controls)
            {
                 if (cont is PictureBox)
                 {
                       pic = cont as PictureBox;
                       if (pic.Name.EndsWith(picCounter.ToString()))
                              images.Add(pic);
            
                       picCounter++;
                 }
            }
            
            S Offline
            S Offline
            Sajjad Izadi
            wrote on last edited by
            #11

            imagine the first control which is surveyed in foreach loop is picturebox16 (foreach is surveying the controls from picturebox16 to picturebox1 in order). then every time, the if condition fails. because picCounter is 1 and non of the name of picture boxes ends with "1" (11 is exception here). so only picturebox1 (last survey of foreach) is added to the List (because if condition does not fail). and after that, the foreach loop is finished (because there is no other control in foreach to survey) and only picturebox1 is added to the list. so i think it is better to change the value of picCounter and then start foreach from the first. isn't it right? please guide me. thanks

            L 1 Reply Last reply
            0
            • S Sajjad Izadi

              imagine the first control which is surveyed in foreach loop is picturebox16 (foreach is surveying the controls from picturebox16 to picturebox1 in order). then every time, the if condition fails. because picCounter is 1 and non of the name of picture boxes ends with "1" (11 is exception here). so only picturebox1 (last survey of foreach) is added to the List (because if condition does not fail). and after that, the foreach loop is finished (because there is no other control in foreach to survey) and only picturebox1 is added to the list. so i think it is better to change the value of picCounter and then start foreach from the first. isn't it right? please guide me. thanks

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #12

              Ah, I see. Why do you actually need the pic.Name.EndsWith check? For me it seems like you just want to store each PictureBox control into a list. Or do you have more picture boxes that do not end with a number 1-16? If this is the case, then I'd store the picture box number into the Tag property, so you only have to check this property if you want to add the PictureBox to the list. regards

              S 1 Reply Last reply
              0
              • L Lost User

                Ah, I see. Why do you actually need the pic.Name.EndsWith check? For me it seems like you just want to store each PictureBox control into a list. Or do you have more picture boxes that do not end with a number 1-16? If this is the case, then I'd store the picture box number into the Tag property, so you only have to check this property if you want to add the PictureBox to the list. regards

                S Offline
                S Offline
                Sajjad Izadi
                wrote on last edited by
                #13

                as you said, tag is more proper here. isn't it any way more optimized than those tow loops? where they correct or not at last? how can i make it optimized (with consideration that i use tags instead of EndsWith) thanks again

                L 1 Reply Last reply
                0
                • S Sajjad Izadi

                  as you said, tag is more proper here. isn't it any way more optimized than those tow loops? where they correct or not at last? how can i make it optimized (with consideration that i use tags instead of EndsWith) thanks again

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #14

                  I think the Tag solution with just the foreach loop is the correct way to do this. As you said yourself, your solution will add some controls twice or some none at all (depending whether you continue out of the loop or not), because EndsWith("1") would trigger for both pictureBox1 and pictureBox11, depending on the order in which the picture boxes are returned in the foreach loop. regards

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    I think the Tag solution with just the foreach loop is the correct way to do this. As you said yourself, your solution will add some controls twice or some none at all (depending whether you continue out of the loop or not), because EndsWith("1") would trigger for both pictureBox1 and pictureBox11, depending on the order in which the picture boxes are returned in the foreach loop. regards

                    S Offline
                    S Offline
                    Sajjad Izadi
                    wrote on last edited by
                    #15

                    except the Tag solution, do you offer anything else? why just 'foreach'? you mean without the extern 'for'? i can't find out why. i wonder if you explain more about the reason of ommiting the extern 'for'. thanks again and again :)

                    L 1 Reply Last reply
                    0
                    • S Sajjad Izadi

                      except the Tag solution, do you offer anything else? why just 'foreach'? you mean without the extern 'for'? i can't find out why. i wonder if you explain more about the reason of ommiting the extern 'for'. thanks again and again :)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      Sajjad Izadi wrote:

                      except the Tag solution, do you offer anything else?

                      I'd say it's the best solution for your problem.

                      Sajjad Izadi wrote:

                      why just 'foreach'? you mean without the extern 'for'? i can't find out why.

                      Because using the Tag, the foreach loop will already find all the controls you are looking for.

                      foreach (Control cont in this.Controls)
                      {
                           if (cont is PictureBox)
                           {
                                 pic = cont as PictureBox;
                                 if(pic != null)
                                 {
                                    string tag = pic.Tag as string;
                      	  int number = int.Parse(tag); // this might fail, add a check here
                      	  if (tag != null && number >= 1 && number <= 16)
                      	     Console.WriteLine(number);
                                 }
                           }
                      }
                      
                      S 1 Reply Last reply
                      0
                      • L Lost User

                        Sajjad Izadi wrote:

                        except the Tag solution, do you offer anything else?

                        I'd say it's the best solution for your problem.

                        Sajjad Izadi wrote:

                        why just 'foreach'? you mean without the extern 'for'? i can't find out why.

                        Because using the Tag, the foreach loop will already find all the controls you are looking for.

                        foreach (Control cont in this.Controls)
                        {
                             if (cont is PictureBox)
                             {
                                   pic = cont as PictureBox;
                                   if(pic != null)
                                   {
                                      string tag = pic.Tag as string;
                        	  int number = int.Parse(tag); // this might fail, add a check here
                        	  if (tag != null && number >= 1 && number <= 16)
                        	     Console.WriteLine(number);
                                   }
                             }
                        }
                        
                        S Offline
                        S Offline
                        Sajjad Izadi
                        wrote on last edited by
                        #17

                        but i want the pictures in order of their position at form. does it response for the order? and a non related question: what is that 'int?' ?

                        L 1 Reply Last reply
                        0
                        • S Sajjad Izadi

                          but i want the pictures in order of their position at form. does it response for the order? and a non related question: what is that 'int?' ?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #18

                          It's a Nullable Type[^]. But I modified the sample a bit, because internally Control will cast the tag value to a string, so the int solution will not work here. See my modified post. regards

                          S 1 Reply Last reply
                          0
                          • L Lost User

                            It's a Nullable Type[^]. But I modified the sample a bit, because internally Control will cast the tag value to a string, so the int solution will not work here. See my modified post. regards

                            S Offline
                            S Offline
                            Sajjad Izadi
                            wrote on last edited by
                            #19

                            what if i want the in order of their position in screen? should i ommit the outer 'for' still?

                            L 1 Reply Last reply
                            0
                            • S Sajjad Izadi

                              what if i want the in order of their position in screen? should i ommit the outer 'for' still?

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #20

                              Depends on how you define "position". The tag value, the (x,y) position, the order of how they were added to the form?

                              S 1 Reply Last reply
                              0
                              • L Lost User

                                Depends on how you define "position". The tag value, the (x,y) position, the order of how they were added to the form?

                                S Offline
                                S Offline
                                Sajjad Izadi
                                wrote on last edited by
                                #21

                                how is it if i want them in (x,y) position? how is it if i want to add them in order of tag value? :) and what if i want them in order of they were added to form? :-D you will help me if multiple aspects if you answer these threes. thanks in advanced ;)

                                L 1 Reply Last reply
                                0
                                • S Sajjad Izadi

                                  how is it if i want them in (x,y) position? how is it if i want to add them in order of tag value? :) and what if i want them in order of they were added to form? :-D you will help me if multiple aspects if you answer these threes. thanks in advanced ;)

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #22

                                  You need to figure these ones out by yourself now ;P

                                  S 1 Reply Last reply
                                  0
                                  • L Lost User

                                    You need to figure these ones out by yourself now ;P

                                    S Offline
                                    S Offline
                                    Sajjad Izadi
                                    wrote on last edited by
                                    #23

                                    ok man ..., you're right :-D: i have to write them myself. thanks for your really hale answers :)

                                    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