Validating an item added in CollectionEditor  
Author Message
Mohsen Kokabi





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

Hello

I have a propertygrid that is showing my collection. If user click on add button and enter an invalid data in one of properties of a new item, in setter of that property I'll show a messagebox, but how can I prohabite user to continue.

private int x;

public int X

{

set { if (value < 0)

{

MessageBox.Show("Enter a value greated than 0");

return;

}

x = value;

}

get { return x; }

After closing my messagebox the new item is in the collection and user can add another item or click ok button. I am validating agin later but I am looking for a way to force user to fix this item before continuing. I tried OnInsert and OnInsertComplete events of CollectionBase and also CreateInstance of CollectionEditor but all of them are happening before user enter any value and at that time all of values are defaults.

Thanks in advance



.NET Development10  
 
 
TaylorMichaelL





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

The property grid is set up such that if your property throws an exception then the user can't change the value and will get a message. This makes sense since the user could programmatically set the value as well and you definitely don't want a message box popping up. Therefore throw an exception like so:

public int X
{
get { ... }
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException(...);

x = value;
}
}

This is good design and completely supported by the designer.

Michael Taylor - 11/27/06


 
 
Mohsen Kokabi





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

I changed my messagebox to throw ArgumentOutOfRangeException, the only differnce is instead of standard messagebox with proper message it will show a messagebox with "Property is not valid" message and my message will be available after clicking on details button. Still, after pressing ok or cancel on this messagebox he can press Add button or OK button on collectioneditor. Is there any way to disable these buttons.


 
 
TaylorMichaelL





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

If an exception is thrown while editing a property the grid will not allow the user to lose focus on the property. However with the collection editor you either add an entire object or you don't. Editing the properties of a collection element is a two step process. To better understand the problem you are having can you provide the definition of the object that you are storing in the collection

Michael Taylor - 11/28/06


 
 
Mohsen Kokabi





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

Hi Micheal

It was my mistake. My sample is going to show the value in TextBox and it works by the way that you said. But in my real project its an enum so its going to get it in a dropdown and then user can change the focus and pass my messagebox.

private MyEnum myEnum;

public MyEnum MyEnum {

get { return myEnum; }

set { if (value == MyEnum.C)

{ throw new ArgumentOutOfRangeException("Dont use C"); } myEnum = value; } } }

public enum MyEnum { A, B, C }

Regards

Mohsen


 
 
TaylorMichaelL





PostPosted: .NET Base Class Library, Validating an item added in CollectionEditor Top

I assume then that this code is housed in a class and the collection you are editing is a collection of this class. In this case you should be able to add new instance of the class to the collection as no validation is done (or needed). However if the user attempts to change the element's MyEnum value to C then the error dialog will appear and the value will remain what it originally was.

public class MyClass
{
private Collection<YourClass> m_Coll = new Collection<YourClass>();

public Collection<YourClass> Items
{
get { return m_Coll; }
}
}

public class YourClass
{
private MyEnum myEnum;

public MyEnum MyEnum
{
get { return myEnum; }
set
{
if (value == MyEnum.C)
throw new ArgumentOutOfRangeException("value", "Dont use C");

myEnum = value;
}
}
}

//In your form's load or somewhere
propertyGrid1.SelectedObject = new MyClass();

Michael Taylor - 11/28/06