I have wrote a frame application, it's a windows form app as parent form.When it starts, it will find dlls in /modules and load them as extensions. And when I click the menuItem in the parent form, the specific dll will work.If the dll is a Form app, it will show.But when i try to use shortcuts(only build-ins,eg : CTRL-C...) in the childForm ,the hotkeys does not work. Anyone kindly tell me why and how can i fix the issue? Here's my code:
//parent.exe--ModuleEntrance.cs
public abstract class ModuleEntrance {
public abstract string[] GetMenuNames();
public abstract string[] GetMenuItemNames();
public abstract EventHandler[] GetEventHandlers();
}
//parent.exe--ParentForm.cs
public partial class MDIParent : Form {
public MDIParent() { //CTOR
InitializeComponent();
ModuleEntrance oneEntrance;
string oneMenuName, oneMenuItemName;
ToolStripMenuItem theMenu, theMenuItem;
for(){ //iterate dlls in /modules, if it implement ModuleEntrance, load it.
//And 1)load menu&menuItem.
//2) connect handler to menuItem.click through
//<code:theMenuItem.Click += new EventHandler(oneEntrance.GetEventHandlers()[i]);>
}
}
//--------------
//child.dll-- EntranceImp.cs //implement AC
public class EntranceImp : ModuleEntrance {
public override string[] GetMenuNames() {
return new string[] { "MEN开发者_StackOverflowU"};
}
public override string[] GetMenuItemNames() {
return new string[] { "OpenChildForm"};
}
public override EventHandler[] GetEventHandlers() {
return new EventHandler[]{
(EventHandler)delegate(object sender, EventArgs e) { //Anonymous method
childForm form = new childForm();
//find MDIparent and connect them
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
form.MdiParent = (Form)(mi.OwnerItem.Owner.Parent); //It works!
form.Show();
}
};
}
}
//child.dll--childForm.dll
//...
The child form has to notify the parent form about the key-down events somehow.
A way to do this is to have the child form expose key-down events that the parent form can listen to. Remember to remove the parent event handlers every time that you change child form, or you'll end up with a memory leak since objects are not garbage collected until you release all references to them, including event handlers.
class Parent
{
KeyEventHandler KeyDownHandler;
public Parent()
{
KeyDownHandler = new KeyEventHandler(form_TextBoxKeyDown);
}
void SetChildForm(Child form)
{
form.TextBoxKeyDown += KeyDownHandler;
}
void RemoveChildForm(Child form)
{
form.TextBoxKeyDown -= KeyDownHandler;
}
void form_TextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.C:
break;
case Keys.X:
break;
case Keys.V:
break;
}
}
}
}
class Child
{
TextBox txtBox;
public event KeyEventHandler TextBoxKeyDown;
internal Child()
{
txtBox.KeyDown += new KeyEventHandler(txtBox_KeyDown);
}
void txtBox_KeyDown(object sender, KeyEventArgs e)
{
if (TextBoxKeyDown != null)
TextBoxKeyDown(sender, e);
}
}
精彩评论