An item with the same key has already been added error

Hi,

I have created custom button control that behave as image button for my Windows C# app, however the control added successfully to all forms except one form that when adding to it and try to select the image for it from properties windows at design time,, i recieve this message:

"An item with the same key has already been added"

Anyone have an idea why this happens with this form only!
LVL 7
Abu HamdanEnterprise Architect, PM ExpertAsked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
nouloukCommented:
Hi hammdan 1980,

Have you checked if your ButtonName doesn't exist ?
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Yes and also i chagned its default name to make sure ,, same error still there
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Also i discovered that any form within same project generate same error when the control added to,, but the other projects in same solution works ,, the project output type is class library..

0
 
Bob LearnedCommented:
Do you have the complete stack trace for the exception?  This appears to be an exception generated from the framework, and not your code, but it is difficult to say with any certainty.  This is not an easy problem to debug without information.

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
How can i get the stack trace,, there is no exception happens at runtime,, its only message displayed when i place the control on the form, and then try to set the image for it ..
0
 
Bob LearnedCommented:
Unfortunately, I don't think you can get the stack trace at design time.  Does your control have any internal structures, like Hashtable, SortedList, Dictionary, etc.?

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
This is the custom control code:

 public class ImageButton : Control
    {
        private Image _image;
        private bool bPushed;
        private Bitmap m_bmpOffscreen;

        public Image Image
        {
            get
            {
                return _image;
            }
            set
            {
                _image = value;
            }
        }

        public ImageButton()
        {
            bPushed = false;
            //default minimal size
           // this.Size = new Size(21, 21);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics gxOff;         //Offscreen graphics
            Rectangle imgRect; //image rectangle
            Brush backBrush;   //brush for filling a backcolor

            if (m_bmpOffscreen == null) //Bitmap for doublebuffering
            {
                m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
            }

            gxOff = Graphics.FromImage(m_bmpOffscreen);

            gxOff.Clear(this.BackColor);

            if (!bPushed)
                backBrush = new SolidBrush(Parent.BackColor);
            else //change the background when it's pressed
                backBrush = new SolidBrush(Color.LightGray);

            gxOff.FillRectangle(backBrush, this.ClientRectangle);

            if (_image != null)
            {
                //Center the image relativelly to the control
                int imageLeft = (this.Width - _image.Width) / 2;
                int imageTop = (this.Height - _image.Height) / 2;

                if (!bPushed)
                {
                    imgRect = new Rectangle(imageLeft, imageTop, _image.Width, _image.Height);
                }
                else //The button was pressed
                {
                    //Shift the image by one pixel
                    imgRect = new Rectangle(imageLeft + 1, imageTop + 1, _image.Width, _image.Height);
                }
                //Set transparent key
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(BackgroundImageColor(_image), BackgroundImageColor(_image));
                //Draw image
                gxOff.DrawImage(_image, imgRect, 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel, imageAttr);
            }

            if (bPushed) //The button was pressed
            {
                //Prepare rectangle
                Rectangle rc = this.ClientRectangle;
                rc.Width--;
                rc.Height--;
                //Draw rectangle
                gxOff.DrawRectangle(new Pen(Color.Black), rc);
            }

            //Draw from the memory bitmap
            e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

            base.OnPaint(e);
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            //Do nothing
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            bPushed = true;
            this.Invalidate();
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            bPushed = false;
            this.Invalidate();
        }

        private Color BackgroundImageColor(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            return bmp.GetPixel(0, 0);
        }

    }
0
 
Bob LearnedCommented:
You could comment out this line to see if it is the culprit:

imageAttr.SetColorKey(BackgroundImageColor(_image), BackgroundImageColor(_image));

You are doing this every time the control is painted, and that can be the problem.

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
I comment as suggested,, but same problem still there ,, but actually in this project i have resource file called Resources.resx,, and when i renamed this resource to Resources11.resx ,,, it worked now never give same exception when i select the images??

But i dont understand why??
0
 
Bob LearnedCommented:
Sometimes the Resources.resx project-level resource file can get corrupted with junk entries.

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Okay can u explain the connection between the error and this junk entries?
0
 
Bob LearnedCommented:
Can you show me the XML text in the file?

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Its tooooo long but this is it,

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!--
    Microsoft ResX Schema
   
    Version 2.0
   
    The primary goals of this format is to allow a simple XML format
    that is mostly human readable. The generation and parsing of the
    various data types are done through the TypeConverter classes
    associated with the data types.
   
    Example:
   
    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>
               
    There are any number of "resheader" rows that contain simple
    name/value pairs.
   
    Each data row contains a name, and value. The row also contains a
    type or mimetype. Type corresponds to a .NET class that support
    text/value conversion through the TypeConverter architecture.
    Classes that don't support this are serialized and stored with the
    mimetype set.
   
    The mimetype is used for serialized objects, and tells the
    ResXResourceReader how to depersist the object. This is currently not
    extensible. For a given mimetype the value must be set accordingly:
   
    Note - application/x-microsoft.net.object.binary.base64 is the format
    that the ResXResourceWriter will generate, however the reader can
    read any of the formats listed below.
   
    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with
            : System.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.
   
    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
</root>
0
 
Bob LearnedCommented:
Is that the new one created, or the old one you renamed?

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
No this is the old one,, with the error
0
 
Bob LearnedCommented:
There doesn't look like anything's wrong with that file (on the surface).

Bob
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
I dont know what is happenning, but the problem is solved since i changed the the Respource file name from Resources to mainResources.resx ,,

What is your recommendation for this question ,,, how should be closed?
0
 
Bob LearnedCommented:
Delete/Refund or PAQ/Refund is OK with me.

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.