Editing controls embedded in custom control (C#, WinForms) [duplicate] - c#

I'll try to explain what I'm after. I don't know the technical term for it, so here goes:
Example 1:
If I place a ListView on a Form and add some columns I am able, in Design-Time, to click-and-drag the columns to resize them.
Example 2:
Now, I place a ListView in a UserControl and name it "MyCustomListView" (and perhaps add some method to enhance it somehow).
If I now place the "MyCustomListView" on a Form I am unable to click-and-drag the column headers to resize them in Design-Time.
Is there any way to easily make that happen? Some form of "pass the click-and-drag event to the underlying control and let that control do its magic". Im not really looking to recode, just pass on the mouseclick (or whatever it is) and let the, in this case, ListView react as it did in the first example above.

The Windows Forms designer has dedicated designer classes for most controls. The designer for a ListView is System.Windows.Forms.Design.ListViewDesigner, an internal class in the System.Design.dll assembly. This class gives you the ability to drag the column headers.
A UserControl uses the System.Windows.Forms.Design.ControlDesigner designer class. It doesn't do anything special, just puts a rectangle around the control with drag handles. You can see where this is heading: after you put your user control on a form, it is ControlDesigner that is used to design the class, ListViewDesigner is not in the picture. You thus lose the ability to drag the column headers. Also note that ControlDesigner doesn't give access to the controls inside the UC.
That's fixable however by creating your own designer. Start with Projects + Add Reference, select System.Design. You'll need to add a public property to the UC to expose the list view and apply the [DesignerSerializationVisibility] attribute to allow changed properties to be saved. And apply the [Designer] attribute to the UC class to replace the default designer. It all should resemble this (using the default names and a ListView that displays "employees"):
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design; // Note: add reference required: System.Design.dll
namespace WindowsFormsApplication1 {
[Designer(typeof(MyDesigner))] // Note: custom designer
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
// Note: property added
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ListView Employees { get { return listView1; } }
}
// Note: custom designer class added
class MyDesigner : ControlDesigner {
public override void Initialize(IComponent comp) {
base.Initialize(comp);
var uc = (UserControl1)comp;
EnableDesignMode(uc.Employees, "Employees");
}
}
}
The list view in the user control can now be clicked and designed as normal.

Related

User control inheriting from GroupBox cant be edited in design mode

I want to extend a GroupBox by adding a button on the caption. If I do this
public partial class MyInheritedGroupBox : GroupBox
{
public MyInheritedGroupBox()
{
InitializeComponent();
}
}
Works at runtime, but the control itself can't be edited anymore in the designer. Double clicking on the control now shows this
Is there some magic attributes so it shows up in the designer?
I'm trying to avoid inheriting from UserControl because it then introduces other complexities like this
Not sure what you want, there are 2 things that could be possible.
1, After dropping your control on a form you are not able to edit it using the designer and the object inspector. If that is the case you can solve it like this :
[Designer(typeof(ControlDesigner))] //without this you cannot change properties in the designer
public partial class MyInheritedGroupBox : GroupBox
{
public MyInheritedGroupBox()
{
InitializeComponent();
}
}
2, You want to build your control visually by double clicking on the class in the solution explorer.
If that is the case than you are out of luck.
You will need to create a UserControl for that.

C# WinForms FlowLayoutPanel locking child controls

I have a base form containing some elements like this:
- pnlSearch of type Panel: search button
- pnlActions of type FlowLayoutPanel: add, edit, delete, export.. etc buttons
nothing is locked, both panels' modifiers are Private & buttons' modifiers are Protected
FlowLayoutPanel is used to customize options in the child forms (e.g. removing the delete option) without leaving empty spaces since the elements will flow accordingly.
In a child form, the search button only is accessible. Buttons in pnlActions are locked in the designer but by checking the properties Locked = False and Modifiers= Protected
Tried setting the pnlActions' modifiers to Protected but it's still the same.
Any idea what's causing this behavior?
what's the difference between Panel and FlowLayoutPanel other than inner controls layout?
I'd post code samples if I've hand-coded anything but it's all generated by designer
I'm using VS 2013 on Win7 if that would matter
Thanks in advance
this is a problem of the Designer. if you do your changes via code all work...
The problem won't be resolved because the platform is not mainteined by Microsoft anymore.
I know it's an old question, but I share a possible solution, in case someone needs it.
We will create a class that inherits FlowLayoutPanel, I name it FlowLayoutPanelHeritable. You can place it in the namespace that you consider appropriate, for this example the namespace is WindowsFormsApp.
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace WindowsFormsApp
{
[Designer(typeof(ParentControlDesigner))]
public class FlowLayoutPanelHeritable : FlowLayoutPanel
{
}
}
First we must set the modifiers of the FlowLayoutPanel that is in FatherForm, in Protected. Now we must make a modification by code in FatherForm, accessing FatherForm.designer.cs. We will replace each instance of FlowLayoutPanel, by FlowLayoutPanelHeritable (in the creation, and initialization of variables). Then we save, and recompile.
Now if you access ChildForm, you will see that you no longer have the modification restriction at design time.
Important: if you want to edit FatherForm, you will have to access FatherForm.designer.cs and replace all the FlowLayoutPanelHeritable to FlowLayoutPanel again; When you finish editing, perform the reverse process.

Could not find a way to fill controls inside UserControl C#

I'm making such a tabbed "Product Version Control", using an UserControl which encloses fields, checkboxes, text areas, grids, and so on. At runtime, when I add a new tab (like we have on browsers), I instantiate a new UserControl (with all components inside). The issue is: I can get all internal component values and fill an object to persist, but not the other way. I can take all values from Object and set them to EditValue (or Text/Value properties) properties of internal components, but they are not showing up to the user.
Consider:
All my internal components have public set/get methods like shown below
public class TabVersion : UserControl
{
...
public DevExpress.XtraEditors.SpinEdit seRevision
{
get
{
return _seRevision;
}
set
{
_seRevision = value;
}
}
...
}
Inside TabVersion class, I have a "fill" method which receives an "Version" object and set all internal components "EditValue" properties like shown below:
public class TabVersion : UserControl
{
...
public void FillTab(Sheet sheet)
{
...
this.seRevision.EditValue = sheet.NumRevision;
...
}
...
}
At runtime, all EditValue component properties are filled but not showing to the user. Do I have to set "Enabled" or "Visible" properties for each component inside my usercontrol?
Any Ideas?
Thank you in advance.
Well... After googling everywhere with no goal, I wrote a "Fill" method that fills all DevExpress controls inside my user control, and called it inside the "Shown" event at the parent form. I think that's a bug from DevExpress, because all Windows default components works all right when I give them their respective values. That's it.

How prevent container control from being moved in user control during design-time?

I have a custom control that contains 6 panel controls that act like containers for other controls that are dropped in during design time. This was done by creating a custom designer inheriting from ParentControlDesign. In the designer, I use EnableDesignMode to enable design time functionality for each panel control. The problem is that when I use the control, I can move the panels around. What can I do to prevent them from moving while in design time?
I couldn't find an ideal solution for this problem, just a partial one. I'll assume you enabled design mode for the panels following the approach in this answer.
Altering the design behavior of the panels requires given them their own designer. Here's a sample class that does this:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(MyPanelDesigner))]
public class MyPanel : Panel {
private class MyPanelDesigner : ScrollableControlDesigner {
public override SelectionRules SelectionRules {
get { return SelectionRules.None; }
}
}
}
Replace the panels in your UC with MyPanel. The custom SelectionRules property ensures that the user cannot easily drag the panel into another position with the mouse. The Location and Size properties are however still editable in the property grid. To get rid of that I think you'll need to override PreFilterProperties().

How to force a certain UserControl design

I am writing a Base UserControl, that will be inherited by a bunch of other UserControls. I need to enforce a certain design for all these descendant controls (e.g. a couple of buttons must be on the top along with a label or two).
The rest of the descendant UserControl area is free to have whatever on it.
Initially, I thought that I could just plop a Panel onto the Base UserControl, set the Dock=Fill and the designer of the descendant control would be forced to add all the UI into this said panel. Then, I could resize the panel to my content.
But that is not the case - when you drop a control (say a GridView) onto the descendant UserControl, it adds it to the .Controls collection of the descendant user control, not the Panel I added.
Is there a way to force a certain layout from the Base user control?
The short answer is "Yes"... However to get this behavior delves into the ugly world of writing your own designers which you have to associate with the each control which will need to inherit the special placement into the content panels..
The following link should be a good starting point as it gives an overview of what needs to be done as well as provides sample code.
http://support.microsoft.com/?id=813808
Be warned, however, this is not a trivial task. And there is a lot of code (30+ files) to sift through in order to understand how to implement a designer. It's been about 2 years since I have tried this.
On the other hand, have you considered changing your design to have a single parent control which has the labels and buttons, and which populates the content area with the appropriate child control(s)? Perhaps even having your child controls for the content area implement a specific interface to guarantee a contract so that the parent can interact with them without having to know a specific class name?
AngryHacker -
I went off thinking this was to be very easy, and found myself embarking on an interesting journey into WinForm design-time land. This is definitely not as easy as it was in VB6 :-) .
Anyhow, after a bit of research, I found a number of references to a method called EnableDesignMode(). However, this is not directly accessible from a WinForm component. This has to be called from a class that subclasses ParentControlDesigner, which is injected into the UserControl via a Designer attribute. In this project, I have called the subclass ButtonBarDesigner, and have overridden the Initialize() method, which allows me to tell the designer that the component ButtonBar has a child component fillPanel which can be accessed via the public property "FillPanel".
At this point, it seemed to be working. I managed to drop a control onto the ButtonBar and it was appearing in the fill panel. However, if you saved the form, and then reloaded it, it turned out that the control was instantiated, but not placed in the ButtonBar control. It seems that there was one sneaky last bit which the documentation for EnableDesignMode() conveniently leaves out. You need to have the DesignerSerializationVisibility attribute on the FillPanel property. Adding this attribute makes this work.
You need a reference to System.Design in your project for the design time stuff to work.
The following code is for the putative base class, ButtonBar:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Threading;
namespace ForceUserControl
{
[Designer(typeof(ButtonBarDesigner))]
public partial class ButtonBar : UserControl
{
public ButtonBar()
{
InitializeComponent();
}
/// <summary>
/// Returns inner panel.
/// </summary>
/// <remarks>Should allow persistence.</remarks>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel FillPanel
{
get { return fillPanel; }
}
}
private class ButtonBarDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
Panel fillPanel = ((ButtonBar)component).FillPanel;
// The name should be the same as the public property used to return the inner panel control.
base.EnableDesignMode(fillPanel, "FillPanel");
}
}
}

Categories

Resources