Skip to content

Instantly share code, notes, and snippets.

@hughesjs
Created April 8, 2020 03:29
Show Gist options
  • Save hughesjs/f0f98dc4e95f7811eccbe144076e382c to your computer and use it in GitHub Desktop.
Save hughesjs/f0f98dc4e95f7811eccbe144076e382c to your computer and use it in GitHub Desktop.
private GroupBox GenerateGroupFromQuestion(JToken question)
{
GroupBox box = new GroupBox {Text = (string) question["text"], Dock = DockStyle.Fill, Tag = question};
questionGroups.Add(box);
List<Control> controls;
Enum.TryParse((string) question["questiontype"], out QuestionType qType);
switch (qType)
{
case QuestionType.MultipleChoiceExclusive:
{
controls = GenerateControlArray<RadioButton>((int) question["optionsnum"],
question["controltext"]
.Select(t => (string) t).ToArray());
break;
}
case QuestionType.MultipleChoiceNotExclusive:
{
controls = GenerateControlArray<CheckBox>((int) question["optionsnum"],
question["controltext"]
.Select(t => (string) t).ToArray());
break;
}
case QuestionType.YesNo:
{
controls = new List<Control>
{
new RadioButton
{
Text = "Yes",
Dock = DockStyle.Fill
},
new RadioButton
{
Text = "No",
Dock = DockStyle.Fill
}
};
break;
}
case QuestionType.FreeComment:
{
controls = new List<Control>
{
new RichTextBox
{
Dock = DockStyle.Fill
}
};
break;
}
default:
{
throw new ArgumentOutOfRangeException();
}
}
TableLayoutPanel layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = Controls.Count / 5,
ColumnCount = Controls.Count <= 5 ? Controls.Count : 5,
MinimumSize = new Size(MinWidth, Controls.Count / 5 * RowHeight),
AutoSizeMode = AutoSizeMode.GrowAndShrink,
AutoSize = true
};
box.Controls.Add(layout);
for (int i = 0; i < controls.Count; i++)
{
int column = i % 5;
int row = i / 5;
layout.Controls.Add(controls[i], column, row);
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
}
box.MinimumSize = new Size(layout.MinimumSize.Width + WidthOffset,
layout.MinimumSize.Height + GroupBoxOffset);
return box;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment