Autosize hides base UserControl - c#

I want a Form to be resized proportionaly to it's content, so I have set AutoSize property to true in GrowAndShrink mode.
I have a base Form that contains a panel in which there is two bttons:
And another Form that inherits from it:
The problem is that panel containing the two buttons in BaseServiceWindow seems not to be taken in account when using the Autosize property. The result is as you can see: I cannot use the buttons from BaseServiceWindow. Any advice would be appreciated.

In your BaseServiceWindow form, create a second panel that fills the area above the panel with the two buttons. You will use this new panel to contain a UserControl.
Then, instead of creating forms that inherit from BaseServiceWindow, make them UserControls instead. Create an base UserControl class for your UserControls to inherit from and include virtual methods for the common behavior.
Finally, inject the UserControl into BaseServiceWindow. You could have the BaseServiceWindow constructor accept the BaseUserControl type each of your UserControls inherits.

Related

C# Child Form Inheriting a Control from Parent Form

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

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.

Custom GroupBox control with other controls inside

I didn't like any available solutions so I started to make my own wizard interface. I use a GroupBox for each step of the wizard but since every step (every GroupBox) must have the same structure and style, I decided to use custom controls.
Now I need a custom GroupBox which has these elements:
A FlowLayoutPanel so the developer can put desired controls in it using the designer. (For example two TextBox controls so the user can enter a username and a password.)
A Label in that FlowLayoutPanel to describe the step.
A Button to go back.
Another FlowLayoutPanel that user can put custom buttons in.
Here is a preview:
Problem #1
When I create a new UserControl and make it inherit the GroupBox, I can't get a GroupBox that I can put and position stuff in it. All I see in the designer is this message:
Problem #2
Since I couldn't make it inherit GroupBox like I wanted it to, I tried doing it by putting a GroupBox in the custom UserControl. (I don't want this. I just did this so I could provide some screenshots.) After I did that, I had to EnableDesignMode on the FlowLayoutPanels so the developer could add controls in them by using the designer. The problem is that they also became movable and resizable (I don't want this. They are anchored properly and they should not be moved nor resized.) and when you try to move them you get "Object reference not set to an instance of an object." which is making it uglier:
Problem #3
I want elements in the main FlowLayoutPanel to be centered. To do this, I had to put a control (the description label) and resize it to the width of the FlowLayoutPanel so the controls after it would be centered. (Messy workaround if you ask me. Doing this with TableLayoutPanel looked easier but their cells can only hold one element. You can add a Panel to make the cell hold more elements but then you lose centering.) The problem is that I have to set Anchor to None for every control I add. Can I hook something (like OnDesignerControlAdded???) to automatically set added control's Anchor to None?
All of your answers will help me build the open source project Magician on GitHub and many other open source projects powered by it. Thanks in advance for all of your efforts.
Quick answer for problem #1:
Design your groupbox (GroupBox1) on a Form or a UserControl (it doesn't matter).
Go to InitializeComponent() in the designer.cs file and copy all code related to the groupbox and its child controls.
Add a new Custom Control called CustomGroupBox to the project.
Change it to inherit from GroupBox:
public partial class CustomGroupBox : GroupBox
Paste all the copied code into the constructor of CustomGroupBox (or into a new method, which you call from the constructor after InitializeComponent(), if you want to be neat).
Remove all occurrences of this. from the pasted code. Replace all occurences of GroupBox1 with this.
For any child controls you want the developer to have access to, add a public property for that control.
E.g.
public FlowLayoutPanel FLP
{
get { return flowLayoutPanel1; }
}
And of course add your CustomDesigner
public class CustomGroupBoxDesigner : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
var c = component as CustomGroupBox;
EnableDesignMode(c.FLP, "FLP");
}
}
And apply the attribute
[Designer(typeof(CustomGroupBoxDesigner))]
public partial class CustomGroupBox : GroupBox

Inherited Control from base Form class not working properly

I have FormBase and MainForm : FormBase.
FormBase Contains a DataGridView dgv. in designer view of FormBase dgv font sizes are fine but in designer view and runtime in MainForm they are default values. why and what should I do??!
The designer for DVG doesn't support designing the control in an inherited form, a limitation of many designers of controls that were added in .NET 2.0. The Font property is an 'ambient' property, it gets the same value as the parent unless you've explicitly selected one (shown in bold in the Properties window). So either set the form's Font property to also change the DGV font or change the Font property of the DGV in the base form.
To clarify my comment: putting UI (together with DataGridView) from BaseForm in custom user control and using it separately on previously inheriting forms will surely fix the font problem.
If your base form is only "container" for commonly used controls (so you don't have to put 3 buttons, datagrid and label on every window) or defines some basic layout - go with custom user control.
Here's decent starting point if you want to explore this topic - Custom User Controls.
Have you tried this approach: http://dotnetengineer.wordpress.com/2008/06/27/visual-inheritance-using-datagridview/

C# Template Forms Get panel in parent form

I have a form that I have created that contains a panel - panel1.
I have then created a template form that inherits the first form.
How can I add something to the panel that I inherited from my template?
You need to set the panel's Modifiers to Protected in the property grid.
Make panel1 visible using protected keyword so it will be visible in your inherited class.

Categories