Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows app and resizing 1

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
0
0
US
I have this windows app built to how I want it to look. The problem I am having now is how do I make the objects resize when the form is resized. In html I would use percentages to make this work but I am not sure this is available in vs c# or if it is how to do it.

for example
Code:
starts like this
-----  
|---| 
|| ||  
|| ||  
|---|  
-----
But if I resize it it looks like this
----------   
|---     |   
|| |     |   
|| |     |   
|---     |   
----------    
When it should look like this
----------
|--------|
||      ||
||      ||
|--------|
----------

tia
 
You need to become familiar with the Dock property and the Anchor Property. Dock is probably the most useful but it takes a bit of getting used to.

You have to add your controls to the form in the right order.

Hope that gets you started.
 
It puts me one step further than I was a minute ago. ;)

Thanks
 
In my experience besides of Anchor and Dock look for object as Panel, GroupBox, SplitContainer.
One trick which I can not understand still. To construct form which properly reacts on resize - I mean DataGridView's follow resizing, Navigatorbinding and MenuStreep sit on their places and not overlap I should construct form in very strict consequence from top to the bottom
E.g.
1. MenuStreep for menu in form
2. SplitContainer
3. GroupBox
4. DataGridView
and so on especially if you have few DatagridView on form and some of them has Binding Navigator Object
Question How I can manipulate them in design mode and they do not get overlapped? so far I have to remove everything and start from clean form
Thanks
 
The other method a coworker was telling me about was to implemant athe "SizeChanged" method on the main form. The ancor worked for everything for me except 1 thing that was placed so the anchor wouldnt work. I guess I now have to calculate how far and what direction the form was changed and then add or subtract to the objects.
 
I use Dock over anchor. It tends to work better for me.

As for the overlapping DataGridViews that likhtin was talking about, i would tend to put those controls into individual panels that automatically layout, or put them all into one panel and create a layout manager class. When your form resizes, ask your layout manager to arrange the items in your panel for you based on percentages or make them evenly spaced.

For example:

LayoutManager.LayoutControls(pnlDataGridViews);

public void LayoutControls(Panel containerpanel)
{
int controlwidth = containerpanel.Width / containerpanel.Controls.Count;

int currentxposition = 0;

foreach (Control c in containerpanel.Controls)
{
c.Location = new Point(currentxposition, 0);
c.Size = new Size(controlwidth, containerpanel.Height);

currentxposition += controlwidth;
}
}

And all your controls will layout from left to right, evenly spaced in the order that you added them to the panel.
 
2 JurkMonkey:
I meant a little bit different issue
Make this experiment
1. Create form
2. Put on it GroupBox and dock it is as Fill
3. Create MenuStrip and try to put it in usual space on top of window.
You will see that Groupbox will not allow you do that. My question how I can get it over without remowing GroupBox then puttinh MenuStree and then create GroupBox again? There should be way to get it around
 
there is - but you're into dangerous territory. You have to screw with the InitializeComponent() section that is created automatically. You have to change the order in which yourform.Controls.Add(control) is called.

Please note that screwing with this section often has very negative results - including the erasing of code. The IDE gets upset that the form code and the resx file don't match up exactly so it fixes the problem by removing the "bad code".

the other option, is to create a different method that you call after InitializeComponent() that will remove the controls from the form and re-add them in the right order.

Isn't it a pain? :)
 
In other word there is no safe "normal" way to freely change design of the form. That's sad.
M$ noobs should understand that there is (almost) no such thing as unchangble forms
I have a lot work to do thus :(
 
In that respect, I rip my forms apart all the time. If you have designed your application properly, your form is only a view that makes your controller do something.

if your form is doing more work than that - you have a problem!

So yes, it is a pain to restructure already existing controls but it is not hard to destroy them and re-add them quickly. GUI Takes a lot of designing before you start.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top