473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

I might have found a .NET Bug

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
0 1253

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

Similar topics

3
by: Rutger Claes | last post by:
Code and problem are from PHP5: When I execute the following piece of code, the DomException is thrown with a message: Not Found Exception. I assume this means that the node I extracted from the...
1
by: Michelle Hillard | last post by:
Hi guys, would appreciate if you can shed some light on this. Sorry to be a pain, can you tell me what is wrong with the following: for /F %%i in ('dir /b /on c:\bcp\pc*.txt') do bcp...
4
by: amywolfie | last post by:
I would like to put code behind a Find button on a form which: 1) Performs a find based on a field on the form 2) If NO RECORDS ARE FOUND, then displays a custom "No Records Found" message box. ...
2
by: aallee83 | last post by:
i'm new in asp.net after develop my solution i copied it on the server where I want it to run but something cares... WHAT?!?! thank you in advance File or assembly name System, or one of its...
2
by: ypul | last post by:
on my local server i am getting ".net error" of "page not found " but on my hosting server I am not getting .net error ...I am getting the normal page not found error what could be the reason ?...
1
by: Olav Tollefsen | last post by:
I get the included error message when trying to run my ASP.NET application under Windows Server 2003 (with all updates installed). How can I troubleshoot this? Olav File or assembly name...
1
by: solarin | last post by:
Hi, I've developed a program under VS 6.0. I can compile it and run it, but when I try to debbug , all my breakpoints are dissabled and I can see the following messages: Loaded...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
4
by: yogarajan | last post by:
The specified module could not be found. (Exception from HRESULT: 0x8007007E) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.