I have added hashtable(ID and Name) as custom property for each item (Node).
The user can add and update those properties with respect to each items.
My problem is that while updating the Hash Table value of a particular item, the updated value is assigned to all items (nodes) for example while editing the ID of a node to 2 then each node contains the same id value.
I think the problem is that the Hash Table values are not cloned for that particular item because other properties like int vertexcount are working fine.
How to resolve this problem and how to update the property with respect to that particular item? Thanks in advance..
Here is the code for serialization of properties:
[Serializable]
public class DynamicGroup : Group
{
#region Class members
private int clickCount;
#endregion
#region Class Initialize/Finalize methods
/// <summary>
/// Default constructor.
/// </summary>
public DynamicGroup()
{
}
/// <summary>
/// Initialize a new instance of DynamicGroup.
/// </summary>
/// <param name="nodes">The node collection.</param>
public DynamicGroup(NodeCollection nodes)
: base(nodes)
{ }
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="src"></param>
public DynamicGroup(DynamicGroup src)
: base(src)
{
this.Properties = src.Properties;
}
/// <summary>
/// Serialization constructor for the MySymbol class.
/// </summary>
/// <param name="info">Serialization state information</param>
/// <param name="context">Streaming context information</param>
protected DynamicGroup(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// The Serialization constructor is invoked during deserialization or during a drag & drop operation.
// If the MySymbol type has serializable members, then initialize them with the serialized data
// obtained from the SerializationInfo param
foreach (SerializationEntry entry in info)
{
switch (entry.Name)
{
case "properties":
properties = (Hashtable)entry.Value;
break;
case "clickCount":
clickCount = (int)entry.Value;
break;
}
}
}
#endregion
#region Class properties
/// <summary>
/// Get or set the dynamic properties collection.
/// </summary>
private Hashtable properties = null;
[Browsable(true), ReadOnly(false)]
public Hashtable Properties
{
get
{
if (properties == null)
{
properties = new Hashtable();
}
return properties;
}
set
{
properties = value;
}
}
[Browsable(true)]
public int ClickCount
{
get
{
return clickCount;
}
set
{
clickCount = value;
}
}
#endregion
[EventHandlerPriority(true)]
protected override void OnMouseClick(EventArgs e)
{
clickCount = clickCount + 1;
}
#region Class overrides
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>Copy of the object this method is invoked against</returns>
public override object Clone()
{
return new DynamicGroup(this);
}
/// <summary>
/// Populates a SerializationInfo with the data needed to serialize the target object.
/// </summary>
/// <param name="info">SerializationInfo object to populate.</param>
/// <param name="context">Destination streaming context.</param>
protected override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
//Add the serialization entry for dynamic properties collection
info.AddValue("properties", this.properties);
info.AddValue("clickCount", clickCount);
}
#endregion
}
Editing properties:
grp is a node:
private void button2_Click(object sender, EventArgs e)
{
DynamicGroup grp = diagram1.Controller.SelectionList[0] as DynamicGroup;
listBox1.Items.Add(grp.ClickCount.ToString());
listBox1.Items.Add(grp.Properties["Name"].ToString());
textBox1.Text = grp.Properties["ID"].ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DynamicGroup grp = diagram1.Controller.SelectionList[0] as DynamicGroup;
grp.Properties["ID"] = textBox1.Text;
}
The issue is raised while duplicating that is copying the already existed node and update the property of duplicated node..
Try changing your Constructor from
to
Otherwise the safe Hashtable will be referenced by both objects, the original and the “clone”.