I have two forms in my project, form1 and form2. I have added a new class to my project. It has a method which accepts a form object and it should be able to do following things.
-
show the accepted form object (frm.ShowDialog())
-
When I press enterkey on the displayed form (frm) it should close. (without adding code to form2 s keydown event). it should be handled by the newly added class.
I tried to add new keyeventhandler but I don’t know how to proceed from here. (Dispose() doesnt work)Please help me to solve this. Thank you.
class Class1
{
public static void SearchResultBox(Form2 frm)
{
frm.KeyDown += new KeyEventHandler(frm_KeyDown);
frm.ShowDialog();
}
static void frm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
}
throw new NotImplementedException();
}
}
You need to keep a reference to the form passed in and then use that reference to close the form