472,958 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1920
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.