By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

I might have found a .NET Bug

P: 1
Here is a class i have expanded off of microsoft picturebox array in msdn.

I have found a problem and I am thinking its not my fault.
Here is how to reproduce the error
1. Create an instance of this object.
2. Add 2-9 images to the object.
3. Click on object 2. (It should select using a rectangler box)
4. Click on picturebox 1. (It should select using a rectangler box)
5. Click on picturebox 2. (it should select using a rectangler box, but it says object reference not set. (why would the object be destroyed))

This bug will repeat after you select the first picture and then another one. But never in any other case.

Can anyone figure out why? If you do please let me know why its not working or if its a bug within .net

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. /// <summary>
  4. /// This Object represents an array of PictureBoxes
  5. /// </summary>
  6. public class PictureBoxArray: System.Collections.CollectionBase
  7. {
  8.     private readonly System.Windows.Forms.Control myParent;
  9.     private System.Collections.ArrayList tagInfo;
  10.     private int selectedOne = -1;
  11.     private System.Drawing.Bitmap selectedPic; 
  12.  
  13.     /// <summary>
  14.     /// Constructor
  15.     /// </summary>
  16.     /// <param name="theParent">The parent of this object</param>
  17.     public PictureBoxArray(System.Windows.Forms.Control theParent)
  18.     {
  19.         myParent = theParent;
  20.         tagInfo = new System.Collections.ArrayList();
  21.     }
  22.  
  23.     /// <summary>
  24.     /// Add A Picture to the Array
  25.     /// </summary>
  26.     /// <param name="width">x size of the picture</param>
  27.     /// <param name="height">y size of the picture</param>
  28.     /// <param name="filename">The filename and path of the file to be displayed</param>
  29.     /// <returns></returns>
  30.     public System.Windows.Forms.PictureBox Add(int width, int height, string filename, string tag)
  31.     {
  32.         System.Drawing.Image img = new System.Drawing.Bitmap(filename);
  33.         return this.Add(width, height, img, tag);
  34.     }
  35.  
  36.     /// <summary>
  37.     ///  Add A Picture to the Array
  38.     /// </summary>
  39.     /// <param name="width">Width</param>
  40.     /// <param name="height">Height</param>
  41.     /// <param name="thePic">The Image</param>
  42.     /// <returns></returns>
  43.     public System.Windows.Forms.PictureBox Add(int width, int height, System.Drawing.Image thePic, string tag)
  44.     {
  45.         System.Windows.Forms.PictureBox picBox = new System.Windows.Forms.PictureBox();
  46.         try 
  47.         {
  48.             myParent.Controls.Add(picBox);
  49.             this.List.Add(picBox);
  50.             this.tagInfo.Add(tag);
  51.             picBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  52.             picBox.Image = thePic;
  53.             picBox.Size = new System.Drawing.Size(width,height);
  54.             picBox.Tag = this.Count;
  55.             picBox.Click += new EventHandler(this.clickEvent);
  56.             picBox.DoubleClick += new EventHandler(this.doubleClickEvent);
  57.         }
  58.         catch
  59.         {
  60.             System.Windows.Forms.MessageBox.Show("Picturebox could not be added");
  61.         }
  62.             return picBox;
  63.     }
  64.  
  65.  
  66.     /// <summary>
  67.     ///[] operator to access the the pictureboxes
  68.     /// </summary>
  69.     public System.Windows.Forms.PictureBox this [int Index]
  70.     {
  71.     get
  72.         {    
  73.             if (Index < this.Count && Index > 0)
  74.                 return (System.Windows.Forms.PictureBox) this.List[Index];
  75.             else
  76.                 return null;
  77.         }
  78.     }
  79.  
  80.     /// <summary>
  81.     /// Remove the picture at index in the array
  82.     /// </summary>
  83.     /// <param name="Index">The index id of the picturebox to remove</param>
  84.     public void Remove(int Index)
  85.     {
  86.         try 
  87.         {
  88.             //Repaint the old selected to the original painting
  89.             if (this.selectedOne != -1)
  90.                 this[selectedOne].Image = this.selectedPic;
  91.  
  92.             selectedOne = -1;
  93.             this.selectedPic = null;
  94.             this.List.RemoveAt(Index);
  95.             this.tagInfo.RemoveAt(Index);
  96.         }
  97.         catch
  98.         {
  99.             System.Windows.Forms.MessageBox.Show("Could not remove Item:" + Index.ToString());
  100.         }
  101.     }
  102.  
  103.     /// <summary>
  104.     /// Remove all the Pictures from this Array
  105.     /// </summary>
  106.     public void RemoveAll()
  107.     {
  108.         selectedOne = -1;
  109.         this.selectedPic = null;
  110.         this.List.Clear();
  111.         this.tagInfo.Clear();
  112.     }
  113.  
  114.     /// <summary>
  115.     /// Returns the image of the selected item
  116.     /// If no image is selected null is returned
  117.     /// </summary>
  118.     /// <returns></returns>
  119.     public System.Drawing.Image getSelectedPicture()
  120.     {
  121.         return this.selectedPic;
  122.     }
  123.  
  124.     /// <summary>
  125.     /// Returns the tag of the picture that is selected
  126.     /// empty string is returned if there is no picture selected
  127.     /// </summary>
  128.     /// <returns>tagInfo</returns>
  129.     public string getSelectedPictureTag()
  130.     {
  131.         if (this.selectedOne != -1)
  132.             return (string)this.tagInfo[this.selectedOne];
  133.         else
  134.             return "";
  135.     }
  136.  
  137.  
  138.     /// <summary>
  139.     /// This is a function that points to the function(deligate) that will handle the ShowPictureEvent
  140.     /// </summary>
  141.     public delegate void ShowPictureEventHandler(string tag);
  142.  
  143.     /// <summary>
  144.     /// The Event that fires when the Picture has been requested to be shown
  145.     /// </summary>
  146.     public event ShowPictureEventHandler ShowPictureEvent;
  147.  
  148.     /// <summary>
  149.     /// The click event selects a picture and keeps track of the one clicked
  150.     /// </summary>
  151.     /// <param name="sender">The object representing which picturebox called it</param>
  152.     /// <param name="e"></param>    
  153.     private void clickEvent(Object sender, System.EventArgs e)
  154.     {    
  155.         // If the user selects the same image twice break out of this function
  156.         if (selectedOne == Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1)
  157.             return;
  158.  
  159.         //Repaint the old selected to the original painting
  160.         if (this.selectedOne != -1)
  161.             this[selectedOne].Image = this.selectedPic;
  162.  
  163.         // Set The new selected one and paint border around the picture and save the old pic before painting border
  164.         System.Drawing.Image img =((System.Windows.Forms.PictureBox) sender).Image;
  165.         this.selectedPic = new System.Drawing.Bitmap(img);
  166.         System.Drawing.Graphics tmp = System.Drawing.Graphics.FromImage(img);
  167.         tmp.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Black,3), 0,0, img.Width-2,img.Height-2);
  168.         ((System.Windows.Forms.PictureBox) sender).Refresh();
  169.         selectedOne = Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1;
  170.     }
  171.  
  172.     /// <summary>
  173.     /// The double click event showed be redirected to the ShowPictureEvent, which the user writes
  174.     /// </summary>
  175.     /// <param name="sender">The object representing which picturebox called it</param>
  176.     /// <param name="e"></param>
  177.     private void doubleClickEvent(Object sender, System.EventArgs e)
  178.     {
  179.         this.ShowPictureEvent(this.tagInfo[Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1].ToString());        
  180.     }
  181. }
  182.  
  183.  
  184.  
Apr 27 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.