I have this following issue. base on the image
Some of the controls on the child form cannot be shown properly. also i cannot use Autoscroll set to true
on child form because some its controls are anchored bottom
.
The fix i was thinking is to have a scrollbar on parent form when the height of child form overlaps. What should i add on my code to make the parent form have a scroll bar?
I use this code to show the child form inside the parent form.
void ParentButtonClickNew(){
ChildForm entry = new ChildForm();
LoadChildForm(entry, this);
}
public void LoadChildForm(object childForm, object container)
{
System.Windows.Forms.Form xForm = (System.Windows.Forms.Form)childForm;
System.Windows.Forms.Control control = (System.Windows.Forms.Control)container;
xForm.TopLevel = false;
if (control.Controls.Count == 0)
{
xForm.Parent = control;
xForm.StartPosition = FormStartPosition.CenterScreen;
//xForm.WindowState = System.Windows.Forms.FormWindowState.Maximized;
xForm.Show();
xForm.BringToFront();
}
else
{
bool isFound = false;
for (int i = 0; i <= control.Controls.Count - 1; i++)
{
try
{
System.Windows.Forms.Form myForm = (System.Windows.Forms.Form)control.Controls[i];
if (myForm.Name == xForm.Name)
{
isFound = true;
xForm.StartPosition = FormStartPosition.CenterScreen;
myForm.Show();
myForm.BringToFront();
}
else
{
myForm.SendToBack();
}
}
catch { }
}
if (!isFound)
{
try
{
xForm.Parent = control;
System.Windows.Forms.Form myForm = (System.Windows.Forms.Form)control.Controls[xForm.Name];
xForm.StartPosition = FormStartPosition.CenterScreen;
myForm.Show();
myForm.BringToFront();
}
catch { }
}
}
}
Click the Format tab, and in the ScrollBars property box, do one of the following: To specify that a scroll bar for a text box never appears, click None. To specify that a scroll bar for a form never appears, click Neither. To specify that a vertical scroll bar for a text box always appears, click Vertical.
Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.
There are some very serious bugs in this code, it is raining nullref and cast exceptions. You really need to stop hiding those bugs with try/catch. It is the core reason you are asking this question, you just can't figure out what's going on inside the code anymore.
The biggest reason you are having a problem is because of the way you designed the method. You must always create an instance of a form and pass it as the first argument. Trouble is, if the form already exists then you never actually use that instance. So trying to set properties like AutoScroll = true on that instance just doesn't have any effect.
You need something fundamentally different. Like a Type
argument. That could look like this:
public void LoadChildForm(Type childForm, Control container) {
foreach (Control child in container.Controls) {
if (child.GetType() == childForm) {
// Found it, bring to front
child.BringToFront();
return;
}
}
// Doesn't exist yet, create a new instance
Form xForm = (Form)Activator.CreateInstance(childForm);
xForm.TopLevel = false;
xForm.Visible = true;
container.Controls.Add(xForm);
xForm.BringToFront();
// Show scrollbar
xForm.AutoScrollMinSize = new Size(0, 2000);
}
}
Note how much cleaner and understandable the code gets when you design it right. You'd call it like this:
void ParentButtonClickNew(){
LoadChildForm(typeof(ChildForm), this);
}
Do beware the real problem, this will never be an emulation of MDI. It looks like a MDI child form but it just isn't. You cannot activate the window, the titlebar always will have the "not activated" colors. If you actually want the equivalent of the MDI client window with the scrollbar then you must create an extra container window, a Panel will do. With AutoScroll = true.
The result is however never going to resemble MDI and won't be very usable. Consider a docking window layout instead as an alternative for MDI. Well done in Weifenluo's DockPanel Suite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With