Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 08:21 AM

I have a panel on my form with a tab control inside the panel. I've set the panel dock property to fill so that it would take up the entire form, but for some reason it stops once it gets larger than the tab control. Here is a screenshot of what is happening.

As you can see there is still a lot of space left that is not covered by the panel. I've been searching online for a solution to this and the answer that came up the most was to right click on the panel and click Bring to Front, however, that solution does not apply to my problem. Any ideas?

Spoiler

In case anyone's wondering why I'm doing this I'm trying to set the panel to fill the entire form then set the tab control's width to be the same as the panel. Then make the tab control's height to be the height of the panel minus 40. That way I have room for a button below the tab control. Because at any time when the user is using the program they might be done and needs to be able to export the data so they would press the button to export the data. It seems like a better way than putting a button on every tab.

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 08:33 AM

[/URL]

You were right. The panel is in fact full size, the tab control just isn't resizing like it's supposed to. Currently here is what I'm doing to make the tab control take up the space in the panel, it's just not doing it.

Re: Panel doesn't fill form when using dock fill

Unless you are running that code on the Panel.SizeChanged event then you should expect it is only running one time; I would guess on .Load. Picture the sequence:

Form changes size.

Panel stays docked so it resizes

But what mechinism is telling the TabControl to keep resizing with the parent?

Take a look at the .Anchor properties of the TabConrol. You can set them to anchor all 4 sides relative to the parent. That way it will always adjust itself to being... 4 pixels within each side for example.

If you're just starting out in Windows/C#/WinForms and it is all new to you anyway, you might consider shifting to WPF instead of WinForms. Stuff like this is much better supported and WPF is the direction MS is pushing everyone into.

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 08:46 AM

Putting the code in a size changed event worked perfectly! Thank you very much!

I've been programming with C# and WinForms for a couple years just never on a project this large. Usually it's small projects to help my friends and family. This is my first freelance project so I'm learning a lot. I'll have to remember to check into WPF.

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 03:44 PM

I'm currently working on a string builder that gets data from each of the tabs, but is there a way to know if the user doesn't interact with anything in a tab? Say they use the first four tabs then skip the fifth one is a there a way to know if they did anything? The only way I know is to make an if statement for each control but I'd rather do it another way if you have a better solution.

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 04:21 PM

Sounds like you are using the tabs as a sort of wizard. I think there are some ready made frameworks that do this for .NET. Unfortunately, even though the .NET TabControl uses the same Win32 TabControl which is used to implement the Wizard97 built into Windows, implementing a .NET wrapper around the wizard functionality was not considered a priority in .NET 1.1 and 2.0. And with no new work going into WinForms control, there probably won't be a solution coming from Microsoft unless an employee posts something in CodePlex.

Anyway, back to your issue. Your question is already hinting that you've broken encapsulation between your tabs. The outside form seems to know about the contents of tabs and is pulling the data out directly. The correct way to handle this would have been to maintain encapsulation. Something like this:

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 07:22 PM

You said that the visited is set to true when a text based event occurs (I'm guessing you mean like if a user enters something into a textbox). Would it also be set to true if a checkbox is checked by the user?

An untested alternative is to capture the Validating/Validated event on the TabPage itself. As I understand the WinForms event model, whenever the user changes focus from a field, the Validating/Validated events are fired. I know it fires for the textboxes and other controls, but I don't know if it also fires for the containing control like the TabPage. Assuming it does fire, the hard part is determining if the data changed, or that the Validating/Validated events just simply fired because of the focus change.

Re: Panel doesn't fill form when using dock fill

Posted 26 August 2012 - 08:01 PM

This may sound like a dumb question, because I'm just trying to read through your code and make sure I understand what you're doing. Is what you've just shown me 2 different methods of doing this or is it 2 parts of the same solution?

You're 2nd post makes a little more sense than the 1st post, though I don't understand lines 10 and 15 after the = sign.

Re: Panel doesn't fill form when using dock fill

Posted 27 August 2012 - 06:42 AM

RandomlyKnighted, on 26 August 2012 - 04:44 PM, said:

I'm currently working on a string builder that gets data from each of the tabs, but is there a way to know if the user doesn't interact with anything in a tab? Say they use the first four tabs then skip the fifth one is a there a way to know if they did anything? The only way I know is to make an if statement for each control but I'd rather do it another way if you have a better solution.

With single forms or UserControls I keep a bool for IsDirty. As soon as any value is changed then IsDirty becomes true. I usually do this through the INotifyProperyChanged event. Every property is coded to raise it. Its handler marks IsDirty as true. Saving the settings then sets it back to false.

If you want to do it on a tab by tab basis there are a couple ways you could go.
One is to make UserControls for the contents of each Tab. The nice thing about that is that you can simply check if a given UserControl is dirty. If you don't want to rework all your tabs, then you would take the IsDirty idea and multiply it by 10 tabs. IsGeneralDirty, IsNetworkDirty, IsPatientDirty...

If SkyDiver is right about you using this more like a Wizard, then make a Wizard. Give the user the experience they are used to.

Wizard Component for WinForms by ComponentSource. I've used this one and it was decent. For $87 how can you go wrong. You won't re-invent the wheel in the 2 hours pay this costs.