How to prevent usercontrol fill option from extending too far - c#

I have a usercontrol that I'm adding as a control of a main form dynamically. The Mainform is basically empty, except it has a large status bar on bottom.
Problem is, when I set the Dockstyle.Fill option on my usercontrol, the size of the loaded usercontrol extends beyond the statusbar (It fills the entire main form as if the status bar wasn't there).
How do I prevent this behavior? This is an example of how I dynamically load my form
logicForm = new LogicForm();
this.Controls.Add(logicForm);
logicForm.Dock = DockStyle.Fill;

I think you need to set the DockStyle to None and use the Anchor property instead.
Set the anchor to Top,Bottom,Left,Right and size your control to fill all the space up to the status bar.
You should find when you run that the user control will then resize with the form.

I just found the solution
I need to bring the form to front in order to properly dock it if I already have some other controls on the main form:
logicForm.BringToFront();
Found here: http://dotnetref.blogspot.kr/2008/08/using-dock-fill-on-control-when-you.html
-________________________-

Related

Windows forms, how can I add controls to a form post-initialization without overlapping with adjacent controls in the same form?

Adding a control that has it's Dock property set to DockStyle.Fill outside of the form's InitializeComponent() function doesn't consider the adjacent controls in the form.
In this case my DataGridView control is being underlapped by my MenuStrip control.
I assume it has something to do with SuspendLayout() ,ResumeLayout(false), and PerformLayout() and I've tried mimicking the function calls done in InitializeComponent() as so:
mainForm = new Form1();
//add post-init control
mainForm.menuStrip1.SuspendLayout();
mainForm.SuspendLayout();
mainForm.TableMainGridView = mainForm.GetGridView();
((System.ComponentModel.ISupportInitialize)(mainForm.TableMainGridView)).BeginInit();
mainForm.Controls.Add(mainForm.TableMainGridView);
((System.ComponentModel.ISupportInitialize)(mainForm.TableMainGridView)).EndInit();
mainForm.menuStrip1.ResumeLayout(false);
mainForm.menuStrip1.PerformLayout();
mainForm.ResumeLayout(false);
mainForm.PerformLayout();
But it doesn't change anything.
After some more digging it seems that SuspendLayout() and ResumeLayout() are only used for the performance of forms with many controls.
PerformLayout() is supposed to "force the control to apply layout logic to all its child controls." but as to what this "layout logic" is remains a mystery to me.

UserControl inside Panel inside UserControl not working

I am having some trouble creating the template of my application :
I am making some dynamic parts of forms and I use a panel in which I include my User Control according to the choice of the user. The panel has the same size as the user control but it doesn't work at runtime. The panel in which the UC is included is inside another UC also.
I tried autoSize = true, fixed size and many things but I don't know why it goes like this. Any idea?
Some parts of my code to load the User Control :
UC_panel.Controls.Clear();
UC_ADDRESS uca = new UC_ADDRESS();
uca.Dock = DockStyle.Fill;
uca.AutoSize = true;
UC_panel.Controls.Add(uca);
My UserControl for the address
At runtime it goes bigger
Since you set
uca.Dock = DockStyle.Fill;
and also
uca.AutoSize = true;
(which is redundant)
it is possible that some other container has its AutoScaleMode set to Font and its font size is bigger.

How to fill MDIForm with default content in c#?

I have created MDIForm that contains some child forms. Now i want to load some default content in MDIForm because without any content it is not looks good while opening MDIForm just like
.
When i tried to make some contents in MDIForm then the child form are behind the controls like
.
I tried load a child form in MDIParent form but it occurs some issues like i set form border style none, but it display minimize, maximize and close buttons. It affects on my designing & controls position too, but i can manage it using dock/anchor but the problem is form border style(as you can see in image2).
So please someone tell me that what is the right way to do this?
I hope you understand what i want to say. If you have doubt then you can ask me about that.

c# Close a form opened from and instance class from the main form that created the class

I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?

Is their something like iframe in C#?

Is their something like iframe in c#?
I want to build an application with one form that its content changes according to the actions I do, like messenger live. The part when I log in and log out its too different "windows" but it happens in the same form.
There is no iframe equivalent in winforms or wpf, but there are ways to deal with it.
For either winforms or wpf, what you want to do is have a Panel which you change the content of.
A panel is a container which holds/encapsulates other controls.
If you have two different views you want to toggle between, create two panels at the same position with the content you need. Then you will show one and hide the other. When the user executes some action which requires you to change the view, then simply hide the displaying panel, and unhide/show the other one.
Think of it as layers, where you will only show one at a time.
You can also dynamically load user controls into a panel, much like an iframe, but I find it easier to have the content in the form, and hide/show as needed.
Assuming WinForms, how about using UserControls? Place as many as you want in a single/multiple forms, and interact as you do in forms. See, UserControl class.
You can load a form into a Panel or tabPage from a TabControl:
Form f = new Form();
f.TopLevel = false;
panel1.Controls.Add(f);
f.Show();
f.Dock = DockStyle.Fill;

Categories