3

As an example of this issue, imagine you extend DataGridView to create a customized regularly reused table of some sort. And you'd like to add a ContextMenuStrip that appears on right clicking a cell.

It'd be very handy to be able to use the designer to layout this ContextMenuStrip. However, since DataGridView does not extend Form or UserControl, there is no designer support available.

There are a number of work-arounds here such as simply using a user control with nothing but the DataGridView, but it seems surely there ought be a way to force an empty designer view which can be used for adding/editing non-UI design components.

  • In the example case, the `SomeCellContextMenuStrip` can be a property of `ExtendedDataGridView`. Then you can drop an instace of `ContextMenuStrip` on the form, design it and assign it to `SomeCellContextMenuStrip` property. – Reza Aghaei May 09 '19 at 07:18
  • The issue is that I'd like to encapsulate all behavior within the control itself, since it's going to be reused. You can also create scenarios where we might like this functionality for classes with no connection to the UI whatsoever. – Russell Bearden May 09 '19 at 07:23
  • You even can drop a customized instance of `ContextMenuStrip` at the same time of dropping customized grid, or by right click and click on a verb like `Insert standard items` or some other solutions. Anyway, the requirement is not completely clear for me. Could you elaborate more? – Reza Aghaei May 09 '19 at 07:36
  • Sure. I prefer to encapsulate all standard behavior of a reused class within that class itself. A standard verb still has the same issue of actually designing all the 'standard items' without a designer interface, and also suffers from no encapsulation. And once again there are work arounds there - for instance, you could simply design the menus or whatever else in another form/usercontrol and then copy/paste the produced code. But I think all of this could be much more elegantly solved if we could simply force a designer view, and I expect there has to be a way to do this. Just no idea how! – Russell Bearden May 09 '19 at 07:45
  • "*A standard verb still has the same issue of actually designing all the 'standard items' without a designer interface*" → No, have you ever used `BindingNavigator`? The binding navigator is basically a `ToolStrip`. When you drop an instance of `BindingNavigator` on the form, it adds a few standard buttons to the component and setup some properties. Then you still can edit and change them. Also if you right click on the control, you can choose `Insert Standard Items` which inserts a few other standard buttons and allow you to design them on design surface. – Reza Aghaei May 09 '19 at 08:47
  • 1
    Also about enabling designer for children of a composite control, you can take a look at this post: [UserControl with header and content - Allow dropping controls in content panel and Prevent dropping controls in header at design time](https://stackoverflow.com/a/50772584/3110834). The trick is in calling `EnableDesignMode` which is applicable to non-UI components as well. – Reza Aghaei May 09 '19 at 08:50

0 Answers0