I have a custom form that has 4 panels on it edges. I would like to anchor another panel to contain user controls to the visible edges of the form. Once that is done I would开发者_开发知识库 then like to anchor an undetermined number of controls to the already anchored panel. I am rather new to this and I do not know that this is the best way to achieve my goal of having a list of objects that resize as the form is resized. The reason I am working at it from this angle is I want the objects to be clickable, and moveable, not just text lines.
My thought is something like this.
----------------------------------------
| Header Panel |
----------------------------------------
|| <--left panel right panel->||
|| [ user control object ]||
|| [ user control object ]||
|| [ user control object ]||
|| [ user control object ]||
----------------------------------------
| Bottom Panel |
----------------------------------------
Currently I am able to draw the panel that holds the user control objects as desired, but it does not resize with the parent, and when I try to anchor it to the parent, it anchors to the wrong place. I have also attempted to anchor the user control objects to the user control panel but they are not resizing at all.
So here is the code for the container panel
//This is called after InitializeComponent(), I would assume the anchor would go
//in here somewhere, but I need the anchor to be offset by the bounds of the
//other panels as listed below.
private static void SetQuestionContainerBounds(SessionReviewForm instance)
{
instance.pnlQuestionContainer.Top = instance.HeaderPanel.Bottom;
instance.pnlQuestionContainer.Left = instance.LeftPanel.Right;
instance.pnlQuestionContainer.Width = instance.RightPanel.Left - instance.pnlQuestionContainer.Left;
instance.pnlQuestionContainer.Height = instance.StatusPanel.Top - instance.pnlQuestionContainer.Top;
}
After I make the form and position the pnlQuestionContainer, I then start making user controls called base question objects, anchor them, and added them to the pnlQuestionContainer
private void DisplayData()
{
// tracks the number of questions, used in placement of objects
int questionCount = 0;
// if the session question is marked for review
// generate a new question object and place it.
foreach (SessionQuestion sq in thisSessionPart.SessionQuestions)
{
if(sq.MarkForReview)
{
BaseQuestionObject bqo = new BaseQuestionObject(sq, parentSession);
BaseQuestionObject.FitAndPlaceObject(pnlQuestionContainer, bqo, questionCount);
bqo.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
pnlQuestionContainer.Controls.Add(bqo);
questionCount++;
}
}
}
A base question object is made up of three parts at the moment. A userControl, a group box, and a label. All of these items are set to autosize with anchor of left, right with the exception of the user control as I cant set that in the properties window, but I think i am setting it in the above method.
The current results are that the container panel is drawn perfectly at first, but it never resizes. The question objects are drawn at the same size that they were made, though this is not the max or the min size.
[EDIT] The issue I had was the anchor styles were not playing nice with autosize. After turning autosize off and manipulating the anchorStyles I was able to get the desired results.
First, I'm not 100% sure of what you are trying to do but anchoring is much simpler than the way you are trying to achieve it.
Each control has a property called Anchor
which is an AnchorStyles enumeration. These values can be Left, Right, Top, Bottom or None and can be bitwised ORed together to allow for multiple anchoring values.
The default 'Anchor' value is Top-Left. If you change the anchoring to Top-Right your controls will remain the same size, but will "float" with the right-side of your application window as it is resized. If you anchor to the Left, Top and Right, your controls will grow and shrink as you resize the width of your window.
I do not believe that you should need any of these panels, unless they are used to logically group controls together. You can experiment with the anchoring by placing a bunch of controls on a form and changing their anchor settings. Run your test form and resize it and see what happens.
Additionally, if you would like to re-arrange the contents of your form as the Window grows and shrinks, the standard MS toolbox has a few controls that you can play around with. Specifically, check out the FlowLayoutPanel and TableLayoutPanel. You can use these controls to fine tune how you want to reposition your child controls on a form or in a section of a form.
精彩评论