private List <Control> GetControls(GroupField field, Size canvasSize) { DragableGroupBox group = new DragableGroupBox(); SetControlProperties(group, field, canvasSize); group.ForeColor = Color.Black; group.BackColor = field.BackgroundColor; group.Text = field.PromptText; group.AutoSize = true; List <Control> controls = new List <Control>(); controls.Add(group); return(controls); }
private List <Control> GetControls(OptionField field, Size canvasSize) { DragableGroupBox groupBox = new DragableGroupBox(); groupBox.Text = field.PromptText; groupBox.Font = field.PromptFont; groupBox.BackColor = SystemColors.Window; int groupWidthEstimate; System.Text.StringBuilder pattern = new System.Text.StringBuilder(); System.Text.StringBuilder locations = new System.Text.StringBuilder(); if (field.ControlWidthPercentage == 0) { groupWidthEstimate = groupBox.Width - 40; } else { groupBox.Width = (int)(field.ControlWidthPercentage * canvasSize.Width); groupWidthEstimate = groupBox.Width - 30; } Size proposedSize = new Size(groupWidthEstimate, int.MaxValue); Bitmap tempImage = new Bitmap(1, 1); Graphics graphics = Graphics.FromImage(tempImage); SizeF groupPromptSize = graphics.MeasureString(groupBox.Text, groupBox.Font, proposedSize.Width); if (field.Options.Count < 1) { RadioButton rdb = new RadioButton(); rdb.Text = "Option 1"; rdb.Left = 5; rdb.Top = 20; rdb.Enabled = false; groupBox.Controls.Add(rdb); } else { int tallestControlHeight = 10; int widestOptionWidth = 16; int leftAlign = 12; foreach (string item in field.Options) { RadioButton radioButton = new RadioButton(); radioButton.Text = item; radioButton.Font = field.ControlFont; radioButton.MaximumSize = new Size(proposedSize.Width + 4, int.MaxValue); proposedSize = new Size(groupWidthEstimate, int.MaxValue); SizeF optionMeasuredSize = graphics.MeasureString(radioButton.Text, radioButton.Font, radioButton.MaximumSize.Width - 20); radioButton.Width = (int)optionMeasuredSize.Width + 20; radioButton.Height = (int)optionMeasuredSize.Height + 1; radioButton.AutoSize = false; radioButton.Enabled = true; radioButton.Visible = false; groupBox.Controls.Add(radioButton); if (radioButton.Width > widestOptionWidth) { widestOptionWidth = (int)radioButton.Width; } if (radioButton.Height > tallestControlHeight) { tallestControlHeight = (int)radioButton.Height; } } widestOptionWidth += 10; double div = (groupBox.Width - (1.2 * leftAlign)) / widestOptionWidth; div = div < 1 ? 1 : div; int columnCount = (int)Math.Floor(div); div = (double)field.Options.Count / (double)columnCount; int rowCount = (int)Math.Ceiling(div); bool isVertical = true; bool startOnLeft = true; if (((OptionField)field).Pattern.Contains(Enums.OptionLayout.Horizontal.ToString())) { isVertical = false; } if (((OptionField)field).Pattern.Contains(Enums.OptionLayout.Right.ToString())) { startOnLeft = false; } pattern.Append(string.Format("{0},", isVertical == true ? Enums.OptionLayout.Vertical.ToString() : Enums.OptionLayout.Horizontal.ToString())); pattern.Append(string.Format("{0},", startOnLeft == true ? Enums.OptionLayout.Left.ToString() : Enums.OptionLayout.Right.ToString())); int topMargin = (int)groupPromptSize.Height + 10; int column = 0; int row = 0; if (startOnLeft == false) { column = columnCount - 1; } int topOfLastControlDown = 0; int bottomOfLastControlDown = 0; foreach (Control control in groupBox.Controls) { if (columnCount == 1) { if (row == 0) { control.Top = bottomOfLastControlDown + topMargin; } else { control.Top = bottomOfLastControlDown + 6; } topOfLastControlDown = control.Top; bottomOfLastControlDown = control.Top + control.Height; control.Width = (int)control.MaximumSize.Width; } else { control.Top = row * (tallestControlHeight + 4) + topMargin; } if (row == (rowCount - 1) || isVertical == false) { topOfLastControlDown = control.Top; } if (field.ShowTextOnRight) { control.RightToLeft = RightToLeft.No; control.Left = column * (widestOptionWidth) + leftAlign; } else { control.RightToLeft = RightToLeft.Yes; control.Left = column * (widestOptionWidth) + leftAlign + widestOptionWidth - control.Width; } groupBox.AutoSizeMode = AutoSizeMode.GrowOnly; groupBox.AutoSize = false; control.Visible = true; string topPercent = ((float)control.Top / (float)canvasSize.Height).ToString("#.#####"); string leftPercent = ((float)control.Left / (float)canvasSize.Width).ToString("#.#####"); locations.Append(string.Format("{0}:{1},", topPercent, leftPercent)); if (isVertical) { row++; if (row >= rowCount) { row = 0; column = startOnLeft ? ++column : --column; } } else { if (startOnLeft) { column++; if (column >= columnCount) { column = 0; row += 1; } } else { column--; if (column < 0) { column = columnCount - 1; row += 1; } } } } groupBox.Height = topOfLastControlDown + (int)(tallestControlHeight * 1.9); field.ControlHeightPercentage = 1.0 * groupBox.Height / canvasSize.Height; } field.Pattern = pattern.ToString().TrimEnd(new char[] { ',' }); field.Locations = locations.ToString().TrimEnd(new char[] { ',' }); field.SaveToDb(); SetControlProperties(groupBox, field, canvasSize); List <Control> controls = new List <Control>(); controls.Add(groupBox); return(controls); }