473,472 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

KeyedCollection in use for GUI editor? Chicken in Egg problem...

So when you use VS's View Designer, each object included is keyed off
its Name. You cannot make the name the same as some other component.
You can change the name in a PropertyGrid. I want to do a similar
thing. I have a class (MissionManager with a bunch of static functions)
that contains a static KeyedCollection<Missionof my objects. Each
mission has a Name (and interface requring the name). That works fine.
What doesn't work is changing the name in the PropertyGrid; that
doesn't update the index in the KeyedCollection. How do other people
handle this issue?

Dec 12 '06 #1
3 1934
Hi,

1. By "VS View Designer" do you mean the Windows Forms Designer? The "View
Designer" is for databases.

2. It's not clear what you are doing, but I assume that you are setting the
PropertyGrid.SelectedObject property to an instance of your Mission class
that is also contained by your static KeyedCollection and you're wondering
why a change to the Name property using the PropertyGrid doesn't appear to
be updating the instance within the collection.

You should supply a short but complete example of the problem you are having
so that we can better assist you (see Jon Skeet's guidance on short but
complete programs here: http://www.yoda.arachsys.com/csharp/complete.html)

--
Dave Sexton

"Brannon" <br*********@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
So when you use VS's View Designer, each object included is keyed off
its Name. You cannot make the name the same as some other component.
You can change the name in a PropertyGrid. I want to do a similar
thing. I have a class (MissionManager with a bunch of static functions)
that contains a static KeyedCollection<Missionof my objects. Each
mission has a Name (and interface requring the name). That works fine.
What doesn't work is changing the name in the PropertyGrid; that
doesn't update the index in the KeyedCollection. How do other people
handle this issue?

Dec 13 '06 #2
Thanks for replying.
1. By "VS View Designer" do you mean the Windows Forms Designer? The "View
Designer" is for databases.
I do mean the Windows Forms Designer.
2. It's not clear what you are doing, but I assume that you are setting the
PropertyGrid.SelectedObject property to an instance of your Mission class
that is also contained by your static KeyedCollection
That's exactly correct.
and you're wondering
why a change to the Name property using the PropertyGrid doesn't appear to
be updating the instance within the collection.
I'm not wondering why the KeyedCollection key is not updated. I'm
wondering 1) what is the best way to make it automatically update the
KeyedCollection indexing, 2) what is the best way to make the
PropertyGrid disallow existing keys from the collection, and 3) what is
the best way to generate an instance name similar to the way the Forms
Designer generates names for instances.

Thanks again.

Dec 13 '06 #3
Hi,

<snip>
1) what is the best way to make it automatically update the
KeyedCollection indexing,
You have to update it yourself using the ChangeItemKey method when the Name
property changes. There are a few different ways to accomplish that. I've
posted code that uses an event, after my sig.
2) what is the best way to make the
PropertyGrid disallow existing keys from the collection
That behavior exists already in my example code below. The ChangeItemKey
method throws an ArgumentException stating, "An item with the same key has
already been added". I've caught and rethrown it with a more meaningful
message. The PropertyGrid suppresses the exception and displays a special
message box containing a predefined message with my wrapper message as
details.
3) what is
the best way to generate an instance name similar to the way the Forms
Designer generates names for instances.
Check out the MissionManager class after my sig. Specifically, the
CreateUniqueName method.

I haven't added comments because the code seems self-explanatory to me, but
if anything is unclear just let me know and I'll explain it.

--
Dave Sexton
static class MissionManager
{
public static readonly MissionCollection Missions = new
MissionCollection();

public static string CreateUniqueName()
{
string name = "Mission_";
int i = Missions.Count + 1;

while (Missions.Contains(name + i))
i++;

return name + i;
}
}

class Mission
{
public string Name
{
get
{
return name;
}
set
{
if (name == value)
return;

OnNameChanging(new NameChangingEventArgs(value));

name = value;
}
}

private string name;

public Mission(string name)
{
this.name = name;
}

private readonly object NameChangingEventLock = new object();
private EventHandler<NameChangingEventArgsNameChangingEven t;

/// <summary>
/// Event raised when the <see cref="Name" /property value is about to
be changed.
/// </summary>
public event EventHandler<NameChangingEventArgsNameChanging
{
add
{
lock (NameChangingEventLock)
{
NameChangingEvent += value;
}
}
remove
{
lock (NameChangingEventLock)
{
NameChangingEvent -= value;
}
}
}

/// <summary>
/// Raises the <see cref="NameChanging" /event.
/// </summary>
/// <param name="e"><see cref="NameChangingEventArgs" /object that
contains the new name for the property.</param>
protected virtual void OnNameChanging(NameChangingEventArgs e)
{
EventHandler<NameChangingEventArgshandler = null;

lock (NameChangingEventLock)
{
handler = NameChangingEvent;

if (handler == null)
return;
}

handler(this, e);
}
}

class NameChangingEventArgs : EventArgs
{
public string NewName { get { return newName; } }
private readonly string newName;

public NameChangingEventArgs(string newName)
{
this.newName = newName;
}
}

class MissionCollection : KeyedCollection<string, Mission>
{
protected override void InsertItem(int index, Mission item)
{
item.NameChanging += HandleNameChange;
base.InsertItem(index, item);
}

protected override void SetItem(int index, Mission item)
{
item.NameChanging += HandleNameChange;
base.SetItem(index, item);
}

protected override void RemoveItem(int index)
{
this[index].NameChanging -= HandleNameChange;
base.RemoveItem(index);
}

protected override void ClearItems()
{
foreach (Mission mission in this)
mission.NameChanging -= HandleNameChange;

base.ClearItems();
}

private void HandleNameChange(object sender, NameChangingEventArgs e)
{
try
{
base.ChangeItemKey((Mission) sender, e.NewName);
}
catch (ArgumentException ex)
{
throw new ArgumentException("The specified Name is already in use by
another Mission.", ex);
}
}

protected override string GetKeyForItem(Mission item)
{
return item.Name;
}
}
Dec 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adammitchell | last post by:
I'm trying to create a local copy of a popular CRM database called Salesforce.com. Many of the tables in the DB have FOREIGN KEY references that I want to preserve, but I've run into a chicken and...
0
by: James T. | last post by:
Hello! How I can update an object in KeyedCollection. whileItem propery is Read Only? Thank you! James
0
by: Craig Buchanan | last post by:
is there an easy way to serialize on the keys in a KeyedCollection, rather than the entire collection of objects? perhaps there is another option? thanks for your time.
3
by: Wiebe Tijsma | last post by:
Hi, I'm trying to serialize a KeyedCollection with the SoapFormatter, however I'm getting the exception: System.Runtime.Serialization.SerializationException: Soap Serializer does not support...
10
by: Chris Mullins [MVP] | last post by:
KeyedCollection is a very handy little class, that unforutnatly has a nasty bug in it. The bug (which I ran across) causes the following code to fail: if (!rooms.Contains(room))...
8
by: GS | last post by:
please bear with me, I already tried the built-in help, Google but not finding the solution. I want to use generic KeyedCollection first crack private class RegexHolder { String Name; bool...
9
by: bchirra | last post by:
Can I sort a KeyedCollection? If so how do I do it. Please, any help on this issue is very much appreciated. Thank you.
15
RMWChaos
by: RMWChaos | last post by:
In my ongoing effort to produce shorter, more efficient code, I have created a "chicken and egg" / "catch-22" problem. I can think of several ways to fix this, none of them elegant. I want my code...
8
by: Diggla | last post by:
I was asked to look into a performance problem on a newly migrated DB server. The db server was moved from a local-physical-nt4-sybase to remote (10 mb wan link), virtual, Windows 2003, SQL...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.