// When the Properties dialog box is closed, // get rid of the control. void DialogOnClosed(object objSrc, EventArgs args) { PropertiesAndEventsDialog dlg = (PropertiesAndEventsDialog)objSrc; Control ctrl = (Control)dlg.SelectedObject; ctrl.Dispose(); }
void MenuItemOnClick(object objSrc, EventArgs args) { // Obtain the menu Item and the class it refers to. ToolStripMenuItem item = objSrc as ToolStripMenuItem; Type typ = (Type)item.Tag; // Get ready to create an object of that type. ConstructorInfo ci = typ.GetConstructor(System.Type.EmptyTypes); Control ctrl; // Try creating an object of that type. try { ctrl = (Control)ci.Invoke(null); } catch (Exception exc) { MessageBox.Show(exc.Message, Text); return; } // Create a dialog box that contains a PropertyGrid control. PropertiesAndEventsDialog dlg = new PropertiesAndEventsDialog(); dlg.Owner = this; dlg.Text = item.Text + " Property Grid"; dlg.SelectedObject = ctrl; dlg.Closed += new EventHandler(DialogOnClosed); dlg.Show(); // If the Parent property can't be assigned, it's probably // a Form, so just call Show for it. try { ctrl.Parent = pnl; } catch { ctrl.Show(); } }