Pre-defined PictureBox[], now null?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    Pre-defined PictureBox[], now null?!

    I have 132 PictureBoxs in an array. 44 in 3 seperate panel controls.
    At the load up, all the PBs are defined, set to their appropriate places, and given their appropriate attributes. Then the panels are hidden for later use in the program.

    Later when I run my method to show the appropriate PBs based on user input. When though I have initilized all the PB and what not. They all return Null when I try modify them in my method, and cause my program to short-circuit...

    I don't understand why they are all Null after I declared them all, already...
    I've also tried to go through them all again with the = new PictureBox(); in a loop. But the all the PBs I set earlier don't change. And if I don't use the = new, then the program errors out.

    Any ideas why this is happening?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    When you make them the first time, are you adding them to a List<PictureBox > or a PictureBox[], that has a global scope so you can reference them elsewhere?

    When you try to access them based on user input are you trying to reach them through that List<> or Array?

    Maybe it would help if you could show the method where you make them, and the method where you later try to access them.

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      Code:
      namespace frm_main {
       class TCMain {
        public PictureBox[] TIcon = new PictureBox[132];
        TCMain() {
         for (int i; i<132; i++) {
          this.TIcon[i] = new PictureBox();
          // Loads X,Y Position / Names /  Border Style... Exc
         }
        }
        // Runs on User Click
        LoadFromUser() {
         for (int i; i<DataClass.Count(); i++) {
          // I have used this.TIcon, and just TIcon. Both with error.
          TIcon[i].Image = new Bitmap(DataClass[i]);
          // Hide or display some icons as well, and other such things... exc...
         }
        }
        static void Main() {
         Application.Run(new TCMain());
        }
       }
      My TCMain() is almost 300 lines long. Thats a good summary of it, since its kind of a static variable / control. The Click Method is in the same Class as TCMain, and where the definition is for TIcon.

      When I get the RunTime error, the Local Variable viewer Shows TIcon[] has 132 null instances.

      But the thing is is when I show the 3 panels that all these PictureBoxes are in... All 132 of them are there... Granted they are just outlines. They are in their appropriate positions, and sizes... So I don't understand why they are null?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Code:
        public [I]string[][/I] TIcon;
           for (int i; i<132; i++) {
            TIcon[i] = new PictureBox();
        WTF? You make a string array, then try to make an element of the string array a new PictureBox? Dude... Go get some sleep before you continue working on this: Obviously you are so sleep-deprived that you can't think straight anymore. I've been there MANY times where the simplest thing like this eludes me at 3 a.m.

        Each element of a string[] is a string, not a picturebox.

        You need to make a picturebox array to hold a bunch of pictureboxes.\

        Code:
        public [B]PictureBox[/B][] TIcon;
           for (int i; i<132; i++) {
            TIcon[i] = new PictureBox();

        Comment

        • Samishii23
          New Member
          • Sep 2009
          • 246

          #5
          Sorry sorry, after I posted string[] I fixed it about a minute after my original post...

          Thanks for the quick reply though!

          Comment

          Working...