Create custom winforms container - c#

I want to create a control in winforms with same behavior as the container controls.
I mean: in design mode, when I drop controls in it, it will group then, just like a groupbox.
This control I'm creating contains some other controls AND a GroupBox.
All I need is: when a control is droped in design mode over my custom control, I'll just put it inside the nested GroupBox.
But I can't figure out how make my control respond to that kind of action in design mode.

Maybe this is what you need, I found it at CodeProject a time ago:
Designing Nested Controls:
This article demonstrates how to allow a Control, which is a child of
another Control to accept having controls dropped onto it at design
time. It is not a large article, there is not much by way of code, and
this may not be either the 'official' or best way to do this. It does,
however, work, and is stable as far as I have been able to test it.

You need to add a Designer attribute to your control, and use a type that derives from or is the ParentControlDesigner Class (needs a reference to the System.Design.dll assembly), like this:
[Designer(typeof(MyCustomControlDesigner1))]
public partial class CustomControl1 : Control
{
public CustomControl1()
{
MyBox = new GroupBox();
InitializeComponent();
MyBox.Text = "hello world";
Controls.Add(MyBox);
}
public GroupBox MyBox { get; private set; }
}
public class MyCustomControlDesigner1 : ParentControlDesigner
{
// When a control is parented, tell the parent is the GroupBox, not the control itself
protected override Control GetParentForComponent(IComponent component)
{
CustomControl1 cc = (CustomControl1)Control;
return cc.MyBox;
}
}

Related

Please give basic example of class that derives from ScrollableControl and implements IContainerControl

I am trying to make a container that will scroll into view a control that has been appended to the container's Controls collection. I am starting with the ContainerControl class and following this example.
I've implemented the ActivateControl method and the ActiveControl property as required by the IContainerControl interface.
I have added controls to the container's Controls collection; there are no errors, but nothing is visible on the screen.
I'm instantiating the container like this:
MyContainer container1 = new MyContainer();
MyContainer.Name = "container1";
Here's the class declaration:
public class MyContainer : ScrollableControl, IContainerControl
{
public MyContainer() { }
}
EDIT: I've gotten the control to be visible by enabling the vertical scroll-bar, but although I've added a dozen controls (composite user controls) to the Controls collection, only one is visible. Do their physical positions need to be specified?

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.

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.

User control with other controls as properties

In my asp.net 4.0 application I have a standard set of tabs controls across the application that has one tab for Search, one for View and one for Reporting. I am in the process of updating those tabs and I was hoping to be able to wrap their functionality up into a single user control since they all function in pretty much similar ways. The issue I am having is how to create a property called SeacrhTab/ViewTab/ReportTab that allows me to assign a different user control to it depending on the area of the application (e.g. CompanySearchControl, ContactSearchControl, etc.) Anyone ever tried to accomplish something similar? How should I approach this?
You should just be able to create a regular property in the partial class definition in your code behind page. Make it public and of the type of generic class that includes all tab controls and you should be set.
I have not made other controls properties of a user control but I have certainly used all sort of other custom classes as properties of user controls, so I don't see why it wouldn't work.
Assuming CompanySearchControl and ContactSearchControl both inherit from GenericSearchTabControlClass, for instance:
public GenericSearchTabControlClass SearchTab {get;set;}
public GenericViewTabControlClass ViewTab {get;set;}
public GenericReportTabControlClass ReportTab {get;set;}
Create an enum for the specific layout, view or tab definition ie: SearchTab, ViewTab etc. specify that as a Property of your UserControl, then use that propery to create, or hide and show a specific PlaceHolder or Panel depending on the required layout, or functionality.
public enum ControlLayoutEnum
{
SearchTab = 0,
ViewTab = 1,
ReportTab = 2
}
public class TabViewControl: System.Web.UI.UserControl
{
public ControlLayoutEnum ControlLayout { get; set; }
protected override OnInit(object sender)
{
// Create controls required or Hide/Show PlaceHolder or Panel etc
switch (this.ControlLayout)
{
case ControlLayoutEnum.SearchTab: // Create Search Layout
break;
}
}

Own component with panel

I want to create my own component which consists two other panels. One of them has fixed contents (such as control buttons, etc.) and the other is standard panel, where I can add other components in designer (VS2008).
I know that I have to create UserControl, where I can place my two panels. Then I want to insert my component into the form. But I don't know how to create behavior where I can add other components (such as buttons, labels, etc.) only into second panel in my component.
Could anyone help me with creating this component?
Thank you.
Adam.
Here is an example (snippet of working code):
[Designer(typeof(NavigationalUserControl.Designer))]
public partial class NavigationalUserControl : UserControl
{
class Designer : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
var nc = component as NavigationalUserControl;
EnableDesignMode(nc.panel2, "ContainerPanel");
EnableDesignMode(nc.bottomPanel, "BottomPanel");
}
}
// rest of normal class
}
I have found the correct solution (I hope). I have added into my UserControl a property which returns the content panel with this specific Attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel PanelContent
{
get { return this.panel2; }
}
Thanks for your help leppie

Categories

Resources