?
Solved

An item with the same key has already been added error

Posted on 2007-10-15
20
Medium Priority
?
5,132 Views
Last Modified: 2013-12-17
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!
0
Comment
Question by:Abu Hamdan
19 Comments
 
LVL 9

Expert Comment

by:noulouk
ID: 20077054
Hi hammdan 1980,

Have you checked if your ButtonName doesn't exist ?
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20077123
Yes and also i chagned its default name to make sure ,, same error still there
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20077169
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 20077230
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
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20077403
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20077727
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
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20077768
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20077893
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
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20077972
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20078122
Sometimes the Resources.resx project-level resource file can get corrupted with junk entries.

Bob
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20078351
Okay can u explain the connection between the error and this junk entries?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20078452
Can you show me the XML text in the file?

Bob
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20078649
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20078729
Is that the new one created, or the old one you renamed?

Bob
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20078746
No this is the old one,, with the error
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20078795
There doesn't look like anything's wrong with that file (on the surface).

Bob
0
 
LVL 7

Author Comment

by:Abu Hamdan
ID: 20079023
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20079240
Delete/Refund or PAQ/Refund is OK with me.

Bob
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20295280
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question