How to get AutoSizing for forms with dynamic controls? - c#

I have a Form with a TabControl on it.
Onto the TabControl TabPages are dynamically added (FYI: It's about configuration settings for different dynamical loaded modules).
Now I would like to have a way to get the Form - and/or the TabControl on the Form - to adjust its size according to the size of the added TabPage.
While writing this I realize that this could be someway more complicated than I thought: Since the different TabPages can be different in size the Form does have to change its size whenever another TabPage is selected or I have to set it to the size of the biggest TabPage once which seems to be the best approach.
Of course I could set the size programmatically by setting width and height to fitting values but first I need to know if there is no automated way to solve my problem and if not what is the best approach to realize this.
I already tried AutoSize=true which didn't solve my problem. ;)
I'm working with Visual Studio 2005 .Net 2.0 and C#.

First off it's generally considered bad UI practice to have a magically resizing form when all a user is doing is switching tabs.
With that said, I don't think you can do what you're asking to without writing code.
However it should be easy enough to determine the size of the largest control you'll be adding to the tab control, then sizing the form (before its displayed) accordingly. If you set the Anchor properties of the tab control to Ttop,left,right,bottom or set its dock to fill, you'll get the tab control to resize with the form... Of course you'll need to account for the visual padding when computing the form size from a house control.

Related

Developing multi tab windows

let's say I want to make program settings menu like:
Many tab options that change the layout of the rest of the window
My program is in C# and I'm making it in Visual Studio
I tried to do it 2 ways:
Make the window super large with all possible layouts in the Form designer and then just resizing it to fit one of them at the time but this method works for like 4 tabs when you can fit them all at 1 screen. If it's large you have to work with slide bars and that's really impractical, laggy and for many tabs you even have to search them
Not using Form designer at all and hand write all the declarations, positions, sizes, colors etc. But like this it takes pretty a while even just to set up 1 button and there is no way I can fast see how it looks like
So the question is: Is there a magic way I don't know about to do this? And how is this made professionally?
Simple solution for small number of views
You can use a TextBox and ListView docked in a Panel docked left.
And a ControlTab docked fill in a panel named for example PanelMain docked fill at right with visible at false.
You will create any tabpage as option. In each tabpage you will put a panel docked fill and dedicated content in. On the listview item click event or itemchange, you will set the tabpage panel parent to PanelMain.
The little problem can be about spacing and the code file can be large (regions can be used).
Advanced solution more clean for several views
You can use the standard multipage pattern with one form per option/view, and do the same thing as exposed previously.
You create one form per view and put a panel docked fill embedding controls.
When the user click on the menu, you set the form main panel parent to the option form or the main panel of the options form.
I hope I haven't written too badly in unverified English.
Feel free to open any new question centered on any atomic and code problem on this subject.

Issue Related to Overlapping .Net Controls in WindowsXP

I have developed a windows Application in .Net2010 in Windows 7, Windows Forms and Controls looks Perfact in Windows 7, but in WindowsXP Text containing in Textbox hides first character overlaps between Label and Textbox as shown in Following picture:
The Label controls that you're using to provide a caption for each of the TextBox and ComboBox controls are extending out over the top of the TextBox and ComboBox controls. Their background color is the same as your form (the brownish-gray color).
One possible solution is to change the Z order of your controls so that the Label controls are always in the background. The controls will still overlap, but the TextBox and ComboBox controls will overlap the Label controls instead of the other way around.
That would work fine as long as there was no text on the Label controls getting covered up. That's rather unlikely, and you certainly count on it. Instead, you need to redesign your form so that there is plenty of extra space between controls. You can't jam things up that close to one another, or there won't be any room for expansion and they'll have no choice but to overlap.
Move the controls around and give them some breathing room. The aptly-named Padding property is very useful for this.
You could use TableLayoutPanel to place your controls in a good manner
Go here to read about it.
With this you could use Docking and Anchoring properties of those controls to grow or shrink them as the space changes.

How Can I Intelligently Implement Auto-Resizing in a Visual C# Winforms App?

My application (which I am using Visual C# 2008 WinForms for) involves a lot of generated controls. Specifically: grids of buttons, arrays of labels, lists, headings, etc... all populated so that they fit their containers appreciably.
I want users to be able to resize the main form, which obviously would require me to either destroy my generated content, and remake it at the proper size OR I could index through every control, figure out what it is by name and type, and re-size each item individually. I would have to do this while/after the form resizes.
Are there any more intelligent ways of doing this? Dock and Anchor don't quite apply here because I am dealing with items that don't make up 100% of a dimension (for example, grids of buttons).
Hard do give a reasonnable answer without seing just how complex the layout in question is.
But in principle, you should use a layout container such as FlowLayoutPanel or TableLayoutPanel to do the job they were designed to do. If one does not do the job, just nest them.
Docking/anchoring is probably the answer here. You need to anchor your grid to top/bottom/left/right or dock it (same effect, but the grid will fill the parent control).
If this is done right your control(s) will re-size with the rest of the form just as if you created everything in the designer.
I believe something like this would work:
Control.Anchor = AnchorStyles.TopLeft | AnchorStyles.BottomRight;

C#: Anchored Panel won't resize properly

I've a strange problem regarding auto sizing of a panel in a user control. This panel is anchored to all 4 sides of the user control.
But the anchoring not always works as it should:
If the user control is resized by resizing the form, the right and bottom anchors don't work. But if the control is resized by using a splitter which is in the form, it works correctly.
All other controls which are also affected by resizing work correctly, just this panel won't do.
I can't see the difference between resizing through a splitter and through a form.
Setting the size manually in the OnResize of the UserControl only works if the resizing is done by moving the splitter. If the resizing occurs by resizing the form, the heigth and the width of the panel won't change to the new values.
Edit: To make it more clear: The user control is added to a TabPage and set to Dock=DockType.Fill.
Edit2: To make it more complicated: This is the hierarchy the control is used:
The Form contains a splitter with 2 controls (A and B). (B) contains a user control (C) with a splitContainer. The split container->panel1 in (C) contains a tabcontrol (D). This tabcontrol (D) contains a user control (E) (in a tabpage) . This user control (E) has a splitcontainer (F) in which another usercontrol with a tabControl (G) is placed. This tabcontrol (G) has tabpages, and in this pages is the described user control (H) placed.
If I place the control (H) directly to the panel in the splitcontainer (C) everything works fine. Since all controls which are added to a splitcontainer or a tabpage are set to docktype=fill, I cannot see why the last control has this problems.
Is there any limitation in the Windows Forms framework which limits the amount of child controls?
Edit: I have uploaded a TestApplication with which you can reproduce this issue.
http://rapidshare.de/files/49092516/TestApplication.zip.html
It occurs only under Vista. Win XP and 7 work properly...
If you resize the form, the textBox with the "..." button won't resize, but if you resize via the vertical splitContainer, it works.
There is a bug with deep nesting where the kernel runs out of stack space causing resizing, docking and anchoring to fail silently.
See https://web.archive.org/web/20140818095718/http://support.microsoft.com/kb/953934
You have talked about both "anchor" and "dock", which can serve similar purposes but act differently. Your problem may be that you are trying to do both, and this is interfering with the behavior you want. If not, perhaps try switching from the one to the other.
It sounds like you've double checked the ansectery of the control, but did you examine it with: View->Other Windows->Document Outline (a very handy tool I only recently discovered)?
Finally, while it doesn't directly solve your problem, it sounds like you have a very complicated UI, and your users may benefit if you were to spend some time to consider if you could simplify the UI. Of course, I don't know your users or the purpose of the app, and what you have may be the best, but it sounds like it might be time to ask this question.
Good luck.
The problem is not the depth of nesting; controls can be nested arbitrarily deep. Anchoring and Docking generally do not play nicely with each other. I usually favor docking over anchoring, but, whichever solution you choose, you should probably avoid mixing them.

Dynamic Form Controls

Using C# 2.0 what is the best way to implement dynamic form controls?
I need to provide a set of controls per data object, so should i just do it manually and lay them out while increment the top value or is there a better way?
You can use panels with automatic layout such as FlowLayoutPanel and TableLayoutPanel.
Unfortunately there are only 2 panels with automatic layout out of box but you can create custom layout panel.
I would recommend you to read following articles:
How to: Create a Resizable Windows Form for Data Entry
Walkthrough: Creating a Resizable Windows Form for Data Entry
Another option would be using of WPF (Windows Presentation Presentation).
WPF is a perfect match for your task.
WPF controls can be hosted in WinForms apps so you don't have to switch to it completely.
#Sam I know this question was about Windows Forms, but you should definitely start looking at WPF. This sort of scenario is really easy in WPF with DataTemplates and TemplateSelectors.
What do you mean by “dynamic”? A new, fixed set of controls for each data row in the data set? Then use a UserControl that contains your controls.
Or do you mean that, depending on your data layout, you want to provide the user with a customized set of controls, say, one TextBox for each column?
Yeah, I've found manually layout out controls (incrementing their Top property by the height of the control plus a margin as I go) to be reasonably effective.
Another approach is to place your controls in Panels with Dock set to Top, so that each successive panel docks up against the one above. Then you can toggle the visibility of individual panels and the controls underneath will snap up to fill the available space. Be aware that this can be a bit unpredictable: showing a hidden panel that's docked can sometimes change its position relative to other docked controls.
Well that's the way we are doing it right now on a project. but that's only useful for simple cases. I suggest you use some sort of template for more complex cases.
For instance I used Reflection to map a certain type of control to a certain property on my domain objects on an older project.
You could try generating the code from templates using t4 see T4 Templates in Visual Studio for Code Generation Screencast for a simple example. You can apply this to WinForms.
Also DevExperience has a nice ( expensive ) framework, see DevExpress eXpressApp Framework™ .

Categories