I'm currently working on a project involving DesignSurfaces.
In the Form_load event, I load a form with 3 textboxes, 3 labels and a button.
To manage these controls, I have a ContextMenuStrip with MenuItems containing the StandardCommands
Cut, Copy, Paste and Delete.
However, when I try any of these actions, It'll throw an InvalidOperationException
which says something like "The inherited component 'textBox1' cannot be deleted" (Unfortunately, I don't have the exception message in english, but through googling I found that it might be this one: "Cannot remove or destroy inherited component")
If I add a control runtime and try to delete it, everything works fine (Cut, Copy, Paste too).
Here's the code for the ContextMenuStrip:
public void ShowContextMenu( CommandID menuID, int x, int y ) {
ContextMenu contextMenu = new ContextMenu();
MenuCommand command = FindCommand( StandardCommands.Cut );
if( command != null ) {
MenuItem menuItem = new MenuItem( "Cut", new EventHandler( OnMenuClicked ) );
menuItem.Tag = command;
contextMenu.MenuItems.Add( menuItem );
}
command = FindCommand( StandardCommands.Copy );
if( command != null ) {
MenuItem menuItem = new MenuItem( "Copy", new EventHandler( OnMenuClicked ) );
menuItem.Tag = command;
contextMenu.MenuItems.Add( menuItem );
}
command = FindCommand( StandardCommands.Paste );
if( command != null ) {
MenuItem menuItem = new MenuItem( "Paste", new EventHandler( OnMenuClicked ) );
menuItem.Tag = command;
contextMenu.MenuItems.Add( menuItem );
}
command = FindCommand( StandardCommands.Delete );
if( command != null ) {
MenuItem menuItem = new MenuItem( "Delete", new EventHandler( OnMenuClicked ) );
menuItem.Tag = command;
contextMenu.MenuItems.Add( menuItem );
}
DesignSurface surface = (DesignSurface) _serviceProvider;
Control viewService = (Control) surface.View;
if( viewService != null ) {
contextMenu.Show( viewService, viewService.PointToClient( new Point( x, y ) ) );
}
}
private void OnMenuClicked( object sender, EventArgs e ) {
MenuItem menuItem = sender as MenuItem;
if( menuItem != null && menuItem.Tag is MenuCommand ) {
MenuCommand command = menuItem.Tag as MenuCommand;
command.Invoke();
}
}
I can't figure out what the problem is here.
Did someone here experience a similiar issue? Or any ideas on how to solve this?
I solved the problem by creating an empty form instead and manually adding the required components.