Fire custom event in form from a method in a module

M

moondaddy

I have a combo which I want to have a custom event do something with the
selected item in the combo (This could replace the SelectedIndexChanged
event).

1) The combo's key up event calls a public method in a module which does
some things such as do an autocomplete for the combo.



2) If a certain condition in this public method is met, I want to fire a
custom event back in the form where the combo is located. This event would
then call code unique to this form and combo such as writing the combo's
value to the db or something.



How can I call an event in a form (possibly attached to the combo control)
from a method in a module? Would I attach the event to the combo using
"AddHandler" and then pass a reverence to the combo into this public method
and then from with in this method do something like: "call
myCombo.myCustomEvent" the certain condition is met.



I need this to be generic and not reference the form or combo specifically
by hard coding their names because this is a generic method that is used by
any combo in any form.



Any good ideas?
 
Y

Ying-Shen Yu[MSFT]

Hi ,
You may try this way,
1 first derive to create your own ComboBox class.
2 define your own event , maybe you would like to define a delegate type
other than EventHandler, but you should at least pass-in the reference of
the combobox.
3 because we can't fire an event outside the class, you need define another
public method.
Here is a code skeleton,
<code>
class MyComboBox : ComboBox
{
public delete MyEvent(object sender,Args e);
event MyEvent OnMyEvent;
public FireOnMyEvent(Args e)
{
//pass-in the reference of current combobox,then the event
handler
// will get to know that which combobox fired this event.
OnMyEvent(this,e);
}
}
...
void AMethodInAnotherModule(MyComboBox cmb)
{
Args e;
....
cmb.FireOnMyEvent(e);
}
</code>
Does this meet your needs?


Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security


This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 

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