Reputation: 1560
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 { }
}
}
}
Upvotes: 7
Views: 3708
Reputation: 941545
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.
Upvotes: 4
Reputation: 335
why dont you try to put all you child form controls inside the panel and put a scrollbar inside the panel i think it is much easier
Upvotes: 0
Reputation: 11577
so you are putting a child form on a parent form, where the child form is bigger then the parent. the first thing i'll tell you is you should not create a child form which is bigger then the parent. it's always complicated. second, i would recommend something simpler then a scroll bar. you can register on the child form to the events of mouse down and mouse up and do:
private Point p;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
p = new Point(e.X, e.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
int distance = e.Y - p.Y;
// so the window won't move with every click
if (distance > 10)
{
Location = new Point(Location.X, Location.Y + distance);
}
}
it's easier then a scroll bar and convenient to the user
Upvotes: 0