Hello!!
I have a derived class called StringClassEditor which inherit from
UITypeEditor listed below.
Now to my question in method EditValue in this class I have this statement
lb.KeyDown += new KeyEventHandler(ListBox_KeyDown);
I have set a breakpoint in the event handler method ListBox_KeyDown but
this method is never called. So this event KeyDown for control ListBox is
never occuring.
I just wonder when is this event occurring?
Can I simply remove statement lb.KeyDown += new
KeyEventHandler(ListBox_KeyDown);
and the method ListBox_KeyDown.
The other event handling methods which are ListBox_SelectedIndexChanged and
ListBox_SelectedIndexChanged
is called so these work as they should.
I have removed code that is irrelevant for my question.
public class StringClassEditor : UITypeEditor
{
...
...
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
Type type = value.GetType(); //get type for value
if (StringClassHasValueList(value as StringClass)) // make
ArrayList if not exist
{
wse = (IWindowsFormsEditorService )
(provider.GetService(typeof(IWindowsFormsEditorSer vice)));
if (wse != null)
{
ListBox lb = new ListBox();
editedSc = value as StringClass;
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);
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;
wse.CloseDropDown();
}
}
private void ListBox_Click(object sender, EventArgs e)
{
wse.CloseDropDown();
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs
e)
{
string test = sender.ToString();
ListBox lb = (ListBox) sender;
editedSc.StrValue = (string)lb.SelectedItem;
}
}
//Many thanks!
//Tony