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
- using System;
- /// <summary>
- /// This Object represents an array of PictureBoxes
- /// </summary>
- public class PictureBoxArray: System.Collections.CollectionBase
- {
- private readonly System.Windows.Forms.Control myParent;
- private System.Collections.ArrayList tagInfo;
- private int selectedOne = -1;
- private System.Drawing.Bitmap selectedPic;
- /// <summary>
- /// Constructor
- /// </summary>
- /// <param name="theParent">The parent of this object</param>
- public PictureBoxArray(System.Windows.Forms.Control theParent)
- {
- myParent = theParent;
- tagInfo = new System.Collections.ArrayList();
- }
- /// <summary>
- /// Add A Picture to the Array
- /// </summary>
- /// <param name="width">x size of the picture</param>
- /// <param name="height">y size of the picture</param>
- /// <param name="filename">The filename and path of the file to be displayed</param>
- /// <returns></returns>
- public System.Windows.Forms.PictureBox Add(int width, int height, string filename, string tag)
- {
- System.Drawing.Image img = new System.Drawing.Bitmap(filename);
- return this.Add(width, height, img, tag);
- }
- /// <summary>
- /// Add A Picture to the Array
- /// </summary>
- /// <param name="width">Width</param>
- /// <param name="height">Height</param>
- /// <param name="thePic">The Image</param>
- /// <returns></returns>
- public System.Windows.Forms.PictureBox Add(int width, int height, System.Drawing.Image thePic, string tag)
- {
- System.Windows.Forms.PictureBox picBox = new System.Windows.Forms.PictureBox();
- try
- {
- myParent.Controls.Add(picBox);
- this.List.Add(picBox);
- this.tagInfo.Add(tag);
- picBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- picBox.Image = thePic;
- picBox.Size = new System.Drawing.Size(width,height);
- picBox.Tag = this.Count;
- picBox.Click += new EventHandler(this.clickEvent);
- picBox.DoubleClick += new EventHandler(this.doubleClickEvent);
- }
- catch
- {
- System.Windows.Forms.MessageBox.Show("Picturebox could not be added");
- }
- return picBox;
- }
- /// <summary>
- ///[] operator to access the the pictureboxes
- /// </summary>
- public System.Windows.Forms.PictureBox this [int Index]
- {
- get
- {
- if (Index < this.Count && Index > 0)
- return (System.Windows.Forms.PictureBox) this.List[Index];
- else
- return null;
- }
- }
- /// <summary>
- /// Remove the picture at index in the array
- /// </summary>
- /// <param name="Index">The index id of the picturebox to remove</param>
- public void Remove(int Index)
- {
- try
- {
- //Repaint the old selected to the original painting
- if (this.selectedOne != -1)
- this[selectedOne].Image = this.selectedPic;
- selectedOne = -1;
- this.selectedPic = null;
- this.List.RemoveAt(Index);
- this.tagInfo.RemoveAt(Index);
- }
- catch
- {
- System.Windows.Forms.MessageBox.Show("Could not remove Item:" + Index.ToString());
- }
- }
- /// <summary>
- /// Remove all the Pictures from this Array
- /// </summary>
- public void RemoveAll()
- {
- selectedOne = -1;
- this.selectedPic = null;
- this.List.Clear();
- this.tagInfo.Clear();
- }
- /// <summary>
- /// Returns the image of the selected item
- /// If no image is selected null is returned
- /// </summary>
- /// <returns></returns>
- public System.Drawing.Image getSelectedPicture()
- {
- return this.selectedPic;
- }
- /// <summary>
- /// Returns the tag of the picture that is selected
- /// empty string is returned if there is no picture selected
- /// </summary>
- /// <returns>tagInfo</returns>
- public string getSelectedPictureTag()
- {
- if (this.selectedOne != -1)
- return (string)this.tagInfo[this.selectedOne];
- else
- return "";
- }
- /// <summary>
- /// This is a function that points to the function(deligate) that will handle the ShowPictureEvent
- /// </summary>
- public delegate void ShowPictureEventHandler(string tag);
- /// <summary>
- /// The Event that fires when the Picture has been requested to be shown
- /// </summary>
- public event ShowPictureEventHandler ShowPictureEvent;
- /// <summary>
- /// The click event selects a picture and keeps track of the one clicked
- /// </summary>
- /// <param name="sender">The object representing which picturebox called it</param>
- /// <param name="e"></param>
- private void clickEvent(Object sender, System.EventArgs e)
- {
- // If the user selects the same image twice break out of this function
- if (selectedOne == Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1)
- return;
- //Repaint the old selected to the original painting
- if (this.selectedOne != -1)
- this[selectedOne].Image = this.selectedPic;
- // Set The new selected one and paint border around the picture and save the old pic before painting border
- System.Drawing.Image img =((System.Windows.Forms.PictureBox) sender).Image;
- this.selectedPic = new System.Drawing.Bitmap(img);
- System.Drawing.Graphics tmp = System.Drawing.Graphics.FromImage(img);
- tmp.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Black,3), 0,0, img.Width-2,img.Height-2);
- ((System.Windows.Forms.PictureBox) sender).Refresh();
- selectedOne = Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1;
- }
- /// <summary>
- /// The double click event showed be redirected to the ShowPictureEvent, which the user writes
- /// </summary>
- /// <param name="sender">The object representing which picturebox called it</param>
- /// <param name="e"></param>
- private void doubleClickEvent(Object sender, System.EventArgs e)
- {
- this.ShowPictureEvent(this.tagInfo[Convert.ToInt32(((System.Windows.Forms.PictureBox) sender).Tag)-1].ToString());
- }
- }