Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DockStyle Fill for RunTime Generated Controls

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);
like image 200
ImGreg Avatar asked Mar 06 '12 21:03

ImGreg


5 Answers

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.

like image 186
ImGreg Avatar answered Oct 22 '22 17:10

ImGreg


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.

like image 26
Coltech Avatar answered Oct 22 '22 16:10

Coltech


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...

like image 21
Nick Avatar answered Oct 22 '22 16:10

Nick


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.

like image 38
Matt Avatar answered Oct 22 '22 18:10

Matt


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

like image 1
Colin Talbert Avatar answered Oct 22 '22 17:10

Colin Talbert