Skip to content

Instantly share code, notes, and snippets.

@hughesjs
Created April 8, 2020 03:28
Show Gist options
  • Save hughesjs/9f188a5e34d4ffc4b7d06ab249b0cf83 to your computer and use it in GitHub Desktop.
Save hughesjs/9f188a5e34d4ffc4b7d06ab249b0cf83 to your computer and use it in GitHub Desktop.
private void PopulateControls()
{
JObject jsonQuestionnaire = JObject.Parse(File.ReadAllText(_metadata.FilePath));
JToken questions = jsonQuestionnaire["questions"];
List<GroupBox> groups = questions.Select(GenerateGroupFromQuestion).ToList();
TableLayoutPanel layout = new TableLayoutPanel
{
RowCount = groups.Count + 2, //Questions + header + footer
ColumnCount = 1,
Dock = DockStyle.Fill
};
//Header
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
PictureBox header = new PictureBox
{
Dock = DockStyle.Fill, Image = Resources.banner,
SizeMode = PictureBoxSizeMode.Zoom,
MinimumSize = new Size(MinWidth, _headerHeight)
};
layout.Controls.Add(header, 0, 0);
//Questions
for (int i = 0; i < groups.Count; i++)
{
layout.RowStyles.Add(GetQuestionHeight((JObject) groups[i].Tag));
layout.Controls.Add(groups[i], 0, i + 1); //(+1 because of header row)
}
//Footer
layout.RowStyles.Add(new RowStyle(SizeType.Absolute, FooterHeight));
Button saveButton = new Button
{
Text = "Save Answers",
Dock = DockStyle.Fill,
MinimumSize = new Size(MinWidth - 20, FooterHeight - 20)
};
saveButton.Click += (sender, args) => SaveResults();
layout.Controls.Add(saveButton, 0, groups.Count + 1);
Controls.Add(layout);
//Minimum Sizing
MinimumSize = new Size(groups.Max(g => g.MinimumSize.Width) + WidthOffset,
CalculateTableMinHeight(layout) + _headerHeight + FooterHeight);
Size = MinimumSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment