C# Child Form Inheriting a Control from Parent Form - c#

The way my application is organized is I have a base form and multiple child forms that inherit from it. I also have a button defined as a UI control. I want to add the UI control to the base form so all the child forms can also have that control. However, I don't want to mess with all the individual child forms because the control should be the same for all of them.
What I have done so far:
Added the control to the designer of baseFrom.cs, which in turn created a InitializeComponent() function in the base form that listed all the properties of the control. I have tried calling the InitializeComponent in the base form's constructor but that still does not make the control appear in any of the child forms. What am I missing here?

after you add control to base form you need to build solution and then control will appear in inherited forms

Related

(C#) The WinForm designer is constantly generating "SetChildIndex" codes on derived forms. how do i prevent this?

I have a base form with a panel in it. When I inherit in another form, vs constantly adds "SetChildIndex" codes to the designer.cs file. The "btnexcel" control belongs to the base form. I added the others in derived form. How can I prevent these codes from being generated?
It only happens when the Modifiers of the button on the base form is set to protected or public. In this case you can modify the control at design-time in the derived form.
If you are interested to add controls to the panel of the base form, then the panel modifier should be protected or public. Then if you want to prevent modifying the existing children of the panel, set their modifiers to private. In this case, when you select one of those private children in designer in the derived form, a small lock icon appear on top-left of the control which shows you cannot modify it.
To learn more about access modifiers in visual inheritance see:
How to: Inherit Forms Using the Inheritance Picker
Add a button that inheritors of the base form can modify
Add a button that cannot be modified by inheritors of the base form

Can't add inherited controls to GroupBox

Problem:
I cannot add any controls to a groupbox, if the controls were declared in another form.
Background Information:
I have MainForm which inherits from Form. It provides some functionality which I need in all of my forms, and have also added some custom controls/images which I need in all of the other forms.
With my forms, I reposition the controls/images from the MainForm to wherever I need and all is well.
But for some reason, I cannot move these same controls/images into existing GroupBoxes.
When I say can't I mean that VS is not letting me; when I drag the control over the groupbox, my mouse cursor switches to this "error" sign:
If I understand correctly then you have two or more level of inheritance hierarchy with your form, base class have the GroupBox you're trying to modify it in designer via derived form. Am I correct?
In that case VS prevents you to move control to groupbox which is declared in base class?
If yes, There are couple of things to check.
Check whether your groupbox is atleast protected, so that you can access it in derived class.
If yes, While you're dragging the control just right click the mouse(holding left button), then drop it into groupbox. It should work.
If you have troubles yet make the groupbox as protected internal and give a try.
Hope this helps

How to edit parent form controls in my child forms designer

I have a baseview form which I added a grid. I have another view form that inherits my base view form. I would like to edit this grid in my derived class designer. I set the access modifier in my base view form to protected but this does not help.
Don't do it. It's a nightmare waiting to happen. Look the other way for five minutes and it will be eight levels deeps and sprawling across your entire application.
Abstract out the common behaviours in to classes, controls etc.

SplitContainer's panel as the MDI parent for other forms

I have got a control with a Splitcontainer added. I want to place
another forms on the second panel (Panel2). However, it is not possible to
set the MDIParent property of a brand new form to Panel2.
Thus, the question is - how can I set the SplitContainer's panel as the MDIParent for another controls?
Thank you in advance for the clues!
cheers
If you want to make Panel-Splitter-MdiClient Form see panel and MDI in c#
An MDIParent can only be another Form. What you need to do is set TopLevel to False on the child Form. Then you can add it to any control just like it was any other control (by adding it to the parent control's Controls collection). However, it won't work like it does in an MDI container (as in you won't be able to minimize or maximize it).
If your intent is to use the splitcontainer to load different subforms this may help. Instead of using WinForms, you could use classes derived from panels containing all the widgets that a normal WinForm would have. To display them, simply add them to your splitcontainer's Panel2 controls collection.
Some events and methods to keep in mind are:
subformPanel.ParentChanged (do some initialization and subscribe to any parent events)
subformPanel.ParentChanged (do some cleaning up and unsubscribe to parent events)
Parent.Controls.Remove (destroy the subformPanel)

WinForms designer: Add control and make it visible for the designer

I have a WinForms user control Host with a custom UI Editor.
Through that editor, a child control (Child) can be added to Host.
(The UI Editor creates Child and sets Child.Parent = Host)
Child is handled through a Holder<Child> helper class, which is set as Tag property of e.g. a ListViewItem.
The respective code - some of it, at least - gets added to the form: Holder is created, and set as Tag, which is enough to be created at runtime, too.
However, Child is not visible to the designer - it is displayed, but it can't be selected, nor does it occur in the drop down list with controls for the parent form.
I would like to:
see the Child control in the designer, so that I can modify properties
get notified if the control is removed
Is this possible?
[edit] Thanks all for your input. I've decided to skip the designer - I hoped to throw together something quickly, but apparently it requires more planning than I should allow myself to spend on it right now.
Usethis.Controls.Add(/*Instance of the child*/); on the host class. Then for the notification add event handler for the host's ControlRemoved event (this.ControlRemoved += new ControlEventHandler(Host_ControlRemoved);).
I can't say I fully understand exactly what you are trying to do.
If you are dealing with the problem of how a "child" Control of a UserControl placed on a Form at Design-Time can be made to function as a container onto which you can drag-and-drop other controls from the Toolbox : this CodeProject article by Henry Minute may be helpful : Designing Nested Controls. For example : you have a UserControl with a Panel inside it : an instance of the UserControl is placed on a Form : in the Design-time view of the Form : you want to be able to drag-drop controls onto the Panel in the UserControl and have them become child controls of the Panel : Henry's article will show you how to do that.
This from Microsoft : How to make a UserControl object acts as a control container design-time by using Visual C#
Perhaps might also be useful, although it seems like you already have this step accomplished.

Categories