TypeConverter, PropertyGrid and Events

M

Moises Cuellar Jr.

Hi Everyone:

I am having problems trying to make an event inside a
TypeConverter class work properly with the property grid
control.

I started debugging the code and I found that the
delegate reference is null when the
StringConverter.ConvertFrom code fires up.

Thanks in advance,
Moy

Here is the sample code:

/******* PactsCodeConverter ********/

using System;
using System.Globalization;
using System.ComponentModel;

namespace PactsEcm.Model {

public delegate void PactsCodeEventHandler(object
sender, PactsCodeEventArgs e);

public class PactsCodeConverter : StringConverter
{

public event PactsCodeEventHandler Lookup;

protected virtual void OnLookup
(PactsCodeEventArgs e) {
if( Lookup != null ) {
Lookup(this, e);
}
}

public override bool CanConvertFrom
(ITypeDescriptorContext context, Type t) {

if( t == typeof(string) ) {
return true;
}
return base.CanConvertFrom
(context, t);

}

public override object ConvertFrom
(ITypeDescriptorContext context, CultureInfo info, object
value) {

if( value is string ) {

PactsCodeType codeType =
PactsCodeType.NotInitialized;

for( int itemIndex = 0;
itemIndex < context.PropertyDescriptor.Attributes.Count;
itemIndex++ ) {
if(
context.PropertyDescriptor.Attributes[itemIndex] is
PactsCodeAttribute ) {
codeType
= ((PactsCodeAttribute)
(context.PropertyDescriptor.Attributes
[itemIndex])).CodeType;
break;
}
}

if( codeType !=
PactsCodeType.NotInitialized ) {

// Parse the
Pacts Code Fromat( CODE - Description )
string s =
(string)value;
int dash =
s.IndexOf('-');

string code =
s.Trim();
if( dash != -1 ) {
code =
s.Substring(0, dash).Trim();
}


PactsCodeEventArgs e = new PactsCodeEventArgs
(codeType);
OnLookup(e);
return e.CodeInfo;

}

}
return base.ConvertFrom(context,
info, value);

}

public override object ConvertTo
(ITypeDescriptorContext context, CultureInfo culture,
object value, Type destType) {

// Simple call the ToString
default implementation
if( destType == typeof(string) &&
value is PactsCodeInfo ) {
return ((PactsCodeInfo)
value).ToString();
}
return base.ConvertTo(context,
culture, value, destType);

}

}

}

/******* Test Program ********/

using System;
using System.ComponentModel;
using System.Windows.Forms;

using System.Reflection;
using PactsEcm.Model;

namespace ClientTest
{

public class PropertyTest :
System.Windows.Forms.Form {

private System.Windows.Forms.PropertyGrid
propInfo;

private System.ComponentModel.Container
components = null;

public PropertyTest() {
InitializeComponent();
}

protected override void Dispose( bool
disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose
();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated
code
/// <summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
/// </summary>
private void InitializeComponent()
{
this.propInfo = new
System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propInfo
//

this.propInfo.CommandsVisibleIfAvailable = true;
this.propInfo.Dock =
System.Windows.Forms.DockStyle.Fill;
this.propInfo.LargeButtons =
false;
this.propInfo.LineColor =
System.Drawing.SystemColors.ScrollBar;
this.propInfo.Location = new
System.Drawing.Point(0, 0);
this.propInfo.Name = "propInfo";
this.propInfo.Size = new
System.Drawing.Size(368, 342);
this.propInfo.TabIndex = 0;
this.propInfo.Text
= "propertyGrid1";
this.propInfo.ViewBackColor =
System.Drawing.SystemColors.Window;
this.propInfo.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// PropertyTest
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(368, 342);
this.Controls.Add(this.propInfo);
this.Name = "PropertyTest";
this.Text = "PropertyTest";
this.Load += new
System.EventHandler(this.PropertyTest_Load);
this.ResumeLayout(false);

}
#endregion

private void PropertyTest_Load(object
sender, System.EventArgs e) {

PactsCodeLookup lookup = new
PactsCodeLookup();
ClientInfo clientInfo = new
ClientInfo();
PactsCodeConverter x =
(PactsCodeConverter)TypeDescriptor.GetConverter
(clientInfo.Citizen);
x.Lookup +=new
PactsCodeEventHandler(x_Lookup);
propInfo.SelectedObject =
clientInfo;

}

private void x_Lookup(object sender,
PactsCodeEventArgs e) {
// Do Something : This Event is
never fired
}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top