DockStyle Fill for RunTime Generated Controls

14,798

Solution 1

Turns out, you have to wait until the TabPage has been viewed already (you have to programatically call yourtabpage.select()), then search through the controls on that tabpage, find the chart, and call "BringToFront" on it. You may have the Dock.Fill set before adding the control to the page.

You cannot setup its z-index until the tabpage is rendered.

Solution 2

Don't dock it. Anchor it instead:

Chart.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;

Edit:

as Jon pointed out calling:

Chart.BringToFront();
Chart.Dock = DockStyle.Fill;

Should allow the doc to play nice with the other controls on the form.

Solution 3

I had a similar problem with the chart control where it crashed if the height was set to zero. The error message was "height must be greater than 0px". Changing the docking from Fill to None and setting the anchor properties instead fixed it. Looks like a bug in the chart control, but finding any more information is proving difficult...

Solution 4

We had problems with "height must be greater than 0px" also. It turns out that the problem/solution is the display settings. Setting the display size to anything greater than 100% resulted in DockStyle.Fill of certain elements filling the entire available space leaving the chart with a height of 0px on initialization. Setting Anchors instead of using Fill worked around the problem, but this is really a bug within chart control.

Solution 5

I was able to solve this and keep my dock set to fill by setting the chart minimum size to 10,10.

Share:
14,798
ImGreg
Author by

ImGreg

I am a Computer Engineer at the University of Waterloo. CANADA!

Updated on July 25, 2022

Comments

  • ImGreg
    ImGreg almost 2 years

    I'm trying to do something very simple that is giving me huge problems in C# Winforms. I have two group boxes on a TabPage. One docked right and one docked bottom. I also have a Chart on the page (System.Windows.Forms.DataVisualization.Charting). This Chart is to Dock.Fill the remaining space on the page.

    I first encountered the problem of the chart hiding behind both group boxes and still dock filling the entire page. However, I found I could solve this by using "BringToFront" (or reordering the Document Outline order) and then the Chart docked properly and didn't overlap any other controls on the page.

    However, I am trying to add a Chart to the page at runtime and it again fills the entire page and hides behind the other controls. How can I go about making this work?

    EDIT: Forgot to mention, calling "BringToFront" will throw an exception "Width must be greater than 0px".

    chart_TapChart = new Chart();
    chart_TapChart.Dock = DockStyle.Fill;
    chart_TapChart.BringToFront();
    GroupBox gp1 = new GroupBox();
    gp1.Dock = DockStyle.Right;
    GroupBox gp2 = new GroupBox();
    gp2.Dock = DockStyle.Bottom;
    this.Controls.Add(chart_TapChart);    <--this refers to tabpage
    this.Controls.Add(gp1);
    this.Controls.Add(gp2);