[.net] How to set controls to adapt form size?

Started by
2 comments, last by devronious 16 years, 2 months ago
I would like to know how to make control adapt the Form resize? I make an application consists of 2 groupboxes and 6 buttons. Inside each groupbox, there are 2 textBoxes, 1 large(Multiline support) and 1 standard and 1 button. The button is used to show file open dialog. The standard sized textBox is used to receive file path from openFileDialog FileName property, the large one ise used to receive the file content which is a text file. The first groupBox is located exactly at the left. I've set the anchor and there is no problem when I maximize the Form but the problem is I want it to be relative to the Form size. The second groupBox is a trouble I guess, it is located next to the first one. So I am confused where I should "anchor" it. It must also relative to the form size. The 6 buttons are arranged vertically along the right side of the form. How to make all the controls I've mentioned above to shrink, expand and keep their positions well-arranged following Form size? Is there any facility in this forum for file attach? I was having difficulties to explain this. If it is, please tell me how to attach files. Thank you very much.
Advertisement
If you want something to expand when the form is expanded, then you can set its anchor to Left + Right. Then as the form is increased horizontally the control will increase by the same amount.

The method of using anchors is troublesome if you want two controls to both increase width (or height), and not overlap as the form's width is increased. Then you need to use some container controls. In your case where you want both panels to expand, you should put them in a SplitContainer. Then when the container is expanded, the panels will both expand and not overlap (as long as FixedPanel property is set to None). You can set IsSplitterFixed if you don't want the user to adjust the position of th esplitter, and change its width real small so it's hardly visible.

You have buttons too; you have some options there. Since you don't need them to expand, you could just put them on the right side with anchor Right and put the SplitContainer to the left of them, and make the SplitContainer have Left + Right anchor property. I'd actually put the buttons in a panel and make the panel anchored at the right. You could put the SplitContainer with your panels in the left side of another SplitContainer and put the buttons on the right, and then make the FixedPanel equal to Panel2 on the outer SplitContainer.

Another option is to use a TableLayoutPanel with three columns. Put the buttons in a panel in the right column, and make that column fixed and the others expand. I don't exactly remember, it's been a while since I've used that a TableLayoutPanel.
If you don't mind, please tell me your e-mail address and I will send you the screenShot of my application.

I have tried to use the splitContainer but the size of splitContainer.Panel2 cannot be changed. I want it the same size as splitContainer1.

How to do that?

I understand your explanation but I was a bit confused.

Thank you.

[Edited by - chrisliando on January 28, 2008 2:17:50 AM]
If you find that none of the built-in controls are doing what you need you could try to make your own method to handle the placement and resize of the controls. You could hook an event to the main form's ResizeEnd event or if within the main form's derived class override the OnResizeEnd method.

For instance, here's one I did that sizes a statusstrip for me in a form named Editor. As you can see I hooked into the ResizeEnd event of the form:

        #region StatusStrip        private void Editor_ResizeEnd(object sender, EventArgs e)        {            this.LayoutStatusStrip();        }        /// <summary>        /// Fixes the status strip element widths if a resize or other layout change has occured.        /// </summary>        /// <param name="workArea"></param>        private void LayoutStatusStrip()        {            int len = this.statusStrip.DisplayRectangle.Width;            len -= this.toolStripStatusGrid.Width;            len -= this.toolStripStatusSnap.Width;            len -= this.toolStripStatusCursorXY.Width;            this.toolStripStatusMessage.Width = len;        }        #endregion

This topic is closed to new replies.

Advertisement