T
tony
Hello!!
Hello Victor!
I use a product called flygrid to create grid tables.
In many of my forms I create such grid tables.
Some columns in these grid tables is of type drop down list where I can
select a value from the list.
Below I have some code which exist in a file named StringClass.cs
There are two classes in the code StringClass and StringClassEditor.
When the drop down list is about to open method EditValue is called and a
ListBox is created and filled with values see the code below.
When a value is selected from the drop down list method SelectedIndexChanged
is called see code below.
Now to my problem I don't want to put all code that exist in StringClass.cs
in every form I use.
What I do want to do if it's possible is to have the event method in this
case SelectedIndexChanged to be put in the form class.
So for example when I have form named form1 and select a value from a drop
down list that exist in a flygrid an event metod that exist in the form1
should be called. For example method SelectedIndexChanged.
One of my problems here is how do I modify class StringClassEditor so the
event metod can be called which exist in my form.
[TypeConverter(typeof(StringClass.StringClassConverter))]
[Editor(typeof(StringClass.StringClassEditor), typeof(UITypeEditor))]
public class StringClass
{
private string strValue;
protected ArrayList valuesList;
public StringClass(string value)
{ strValue = value; }
public StringClass()
{ strValue = null; }
public StringClass(string value, string[] values) : this(value)
{ valuesList = new ArrayList(values); }
public StringClass(string value, ArrayList values) : this(value)
{ valuesList = values; }
public class StringClassEditor : UITypeEditor
{
private IWindowsFormsEditorService wse = null;
private StringClass editedSc = null;
private string originalValue = null;
private bool StringClassHasValueList(StringClass sc)
{ return sc != null && sc.ValuesList.Count > 0; }
public override System.Drawing.Design.UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{ return
System.Drawing.Design.UITypeEditorEditStyle.DropDown; }
public override object EditValue(ITypeDescriptorContext
context, IServiceProvider provider, object value)
{
Type type = value.GetType(); //get type for value
if (StringClassHasValueList(( StringClass ) value))
{
wse = (IWindowsFormsEditorService )
(provider.GetService(typeof(IWindowsFormsEditorService)));
if (wse != null)
{
ListBox lb = new ListBox();
editedSc = (StringClass) value;
originalValue = editedSc.StrValue;
string[] values = (string [])
(editedSc.ValuesList.ToArray(typeof(string)));
lb.Items.AddRange(values);
int selectedIndex = editedSc.StrValue !=
null ? lb.Items.IndexOf(editedSc.StrValue) : -1;
if (selectedIndex != -1)
lb.SelectedIndex = selectedIndex;
lb.KeyDown += new
KeyEventHandler(ListBox_KeyDown); //anger en delegate
lb.SelectedIndexChanged += new
EventHandler(ListBox_SelectedIndexChanged);
lb.Click += new
EventHandler(ListBox_Click);
wse.DropDownControl(lb);
wse = null;
return editedSc;
}
}
return base.EditValue (context, provider, value);
}
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (wse != null && (e.KeyCode == Keys.Escape ||
e.KeyCode == Keys.Return))
{
if (e.KeyCode == Keys.Escape)
editedSc.StrValue = originalValue;//return
to original value
wse.CloseDropDown();
}
}
private void ListBox_Click(object sender, EventArgs e)
{ wse.CloseDropDown(); }
private void ListBox_SelectedIndexChanged(object sender,
EventArgs e)
{
ListBox lb = (ListBox) sender;
editedSc.StrValue = (string)lb.SelectedItem;
}
}
//Tony
Hello Victor!
I use a product called flygrid to create grid tables.
In many of my forms I create such grid tables.
Some columns in these grid tables is of type drop down list where I can
select a value from the list.
Below I have some code which exist in a file named StringClass.cs
There are two classes in the code StringClass and StringClassEditor.
When the drop down list is about to open method EditValue is called and a
ListBox is created and filled with values see the code below.
When a value is selected from the drop down list method SelectedIndexChanged
is called see code below.
Now to my problem I don't want to put all code that exist in StringClass.cs
in every form I use.
What I do want to do if it's possible is to have the event method in this
case SelectedIndexChanged to be put in the form class.
So for example when I have form named form1 and select a value from a drop
down list that exist in a flygrid an event metod that exist in the form1
should be called. For example method SelectedIndexChanged.
One of my problems here is how do I modify class StringClassEditor so the
event metod can be called which exist in my form.
[TypeConverter(typeof(StringClass.StringClassConverter))]
[Editor(typeof(StringClass.StringClassEditor), typeof(UITypeEditor))]
public class StringClass
{
private string strValue;
protected ArrayList valuesList;
public StringClass(string value)
{ strValue = value; }
public StringClass()
{ strValue = null; }
public StringClass(string value, string[] values) : this(value)
{ valuesList = new ArrayList(values); }
public StringClass(string value, ArrayList values) : this(value)
{ valuesList = values; }
public class StringClassEditor : UITypeEditor
{
private IWindowsFormsEditorService wse = null;
private StringClass editedSc = null;
private string originalValue = null;
private bool StringClassHasValueList(StringClass sc)
{ return sc != null && sc.ValuesList.Count > 0; }
public override System.Drawing.Design.UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{ return
System.Drawing.Design.UITypeEditorEditStyle.DropDown; }
public override object EditValue(ITypeDescriptorContext
context, IServiceProvider provider, object value)
{
Type type = value.GetType(); //get type for value
if (StringClassHasValueList(( StringClass ) value))
{
wse = (IWindowsFormsEditorService )
(provider.GetService(typeof(IWindowsFormsEditorService)));
if (wse != null)
{
ListBox lb = new ListBox();
editedSc = (StringClass) value;
originalValue = editedSc.StrValue;
string[] values = (string [])
(editedSc.ValuesList.ToArray(typeof(string)));
lb.Items.AddRange(values);
int selectedIndex = editedSc.StrValue !=
null ? lb.Items.IndexOf(editedSc.StrValue) : -1;
if (selectedIndex != -1)
lb.SelectedIndex = selectedIndex;
lb.KeyDown += new
KeyEventHandler(ListBox_KeyDown); //anger en delegate
lb.SelectedIndexChanged += new
EventHandler(ListBox_SelectedIndexChanged);
lb.Click += new
EventHandler(ListBox_Click);
wse.DropDownControl(lb);
wse = null;
return editedSc;
}
}
return base.EditValue (context, provider, value);
}
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (wse != null && (e.KeyCode == Keys.Escape ||
e.KeyCode == Keys.Return))
{
if (e.KeyCode == Keys.Escape)
editedSc.StrValue = originalValue;//return
to original value
wse.CloseDropDown();
}
}
private void ListBox_Click(object sender, EventArgs e)
{ wse.CloseDropDown(); }
private void ListBox_SelectedIndexChanged(object sender,
EventArgs e)
{
ListBox lb = (ListBox) sender;
editedSc.StrValue = (string)lb.SelectedItem;
}
}
//Tony