Reusing a Form's Toolbar and Menu

  • Thread starter Thread starter Terry McGinty
  • Start date Start date
T

Terry McGinty

I created a form with a toolbar and main menu. The form is basically
a text editor similar to WordPad. Now I have a second form that I
determined would benefit from having the toolbar and menu items (and
associated code) from the first form. The second form has additional
toolbars of its own and has a different editing area.

My question is: What is the best way to reuse toolbar and main menu
items (and associated code) in multiple forms?

I've thought of three approaches, but am not sure which one would be
the best approach:

1. Create a base form and then host that form within another form. In
this approach I would create a form with the toolbar, main menu, and
all associated code. I could then host this base form inside other
forms, by treating it as a control. I actually did do this with some
code that looks like this:

_editorForm = new EditorForm();
_editorForm.TopLevel = false;
_editorForm.Dock = DockStyle.Fill;
_editorForm.FormBorderStyle = FormBorderStyle.None;
_panelClientArea.Controls.Add(_editorForm); // This is a panel which
//fills up the client area of the host form
_editorForm.Show();
_editorForm.BringToFront();

2. Create a base UserControl and then host that control within another
form. This is the same approach as above, but instead of using a
Form, I would put the Toolbar and Menu code in a UserControl. (You
cannot put a MainMenu control on a UserControl, but you could generate
the MainMenu in code and then return it from a method of the
UserControl class).

3. Create a base form and then inherit from that form (visual
inheritance).

Or is there a better approach?

My goal is simply to avoid code duplication and to allow for reuse of
toolbars and main menus (and associated code). Any suggestions or
references would be appreciated.

Thanks.

Terry McGinty
 
Back
Top