7

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);
ImGreg
  • 2,946
  • 16
  • 43
  • 65
  • This is the gist of the code. Literally just plain and simple stuff. This code is contained within a TabPage. The TabPage gets added to a TabController. – ImGreg Mar 06 '12 at 22:12
  • http://msdn.microsoft.com/en-us/library/ms404360(v=vs.80).aspx Gives some good advice... – poy Jul 10 '13 at 19:21

6 Answers6

5

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.

ImGreg
  • 2,946
  • 16
  • 43
  • 65
  • Just had a similar problem. A panel programmatically set to DockStyle.Fill in a TabPage does not behave as intended unless the DockStyle is set after the TabPage is set as the SelectedTab property on the TabControl. – Gareth Aug 12 '13 at 13:14
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.

arrowd
  • 33,231
  • 8
  • 79
  • 110
Coltech
  • 1,670
  • 3
  • 16
  • 31
  • I have tried this, it does not fill the blank space as I want it to. Dock.Fill would be much more ideal. – ImGreg Mar 06 '12 at 22:07
  • Dock.Fill will not work because it will always fill its parent contianer and overrun the other controls. Set the left, top, width and height to the space you want it to use and then anchor it. – Coltech Mar 06 '12 at 22:14
  • 1
    Coltech - Dock.Fill can play nice with other controls docked Top/Bottom/Left/Right. The Z-order determines how this works. If the chart is on top, it will not fill the entire container. – Jon B Mar 06 '12 at 22:15
  • 1
    John, I did not know that! So just call Chart.BringToFront() and the DockStyle.Fill will work? – Coltech Mar 06 '12 at 22:17
  • I don't want to hardcode a width and height. The size of the page will change and adding extra code for changing the size of the page is overkill for what I'm doing. Dock.Fill is seems to be the proper approach for my situation. But thank you for your answer anyway. – ImGreg Mar 06 '12 at 22:18
  • @Coltech - exactly. It's pretty fussy to work with, but it can be done! – Jon B Mar 06 '12 at 22:19
  • @Coltech that took me the first couple hours to figure out as well, but then I encountered this problem randomly after. SO frustrating! – ImGreg Mar 06 '12 at 22:21
1

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

Nick
  • 11
  • 1
1

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.

Matt
  • 11
  • 1
1

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

Colin Talbert
  • 454
  • 6
  • 20
0

Setting Dockstyle to None allows the form to load, but I really need to use the Fill docking style for some of my charts. I solved this problem by setting myChart.Dock = DockStyle.None in the designer, and then setting myChart.Dock = DockStyle.None at the end of the Form.Load event in code. Now the program loads without error and the charts are sized properly.

Robert Cody
  • 179
  • 2
  • 10