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.
BaseQuestionObject.FitAndPlaceObject()
method do to theBaseQuestionObject
that is passed into it? Does it set the Location and Size? – CodingWithSpike