Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Scrollbar on Parent Form

I have this following issue. base on the image enter image description here 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 { }
            }
        }
    }
like image 887
Snippet Avatar asked Aug 01 '13 05:08

Snippet


People also ask

How do I enable the scroll bar in access form?

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.

How do you make a ScrollBar in HTML?

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.


1 Answers

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.

like image 57
Hans Passant Avatar answered Sep 18 '22 14:09

Hans Passant