Last active
August 29, 2015 14:11
-
-
Save sweenist/04231de9c9db1d6c2f05 to your computer and use it in GitHub Desktop.
Dynamic Windows Control example that makes a GroupBox and adds items as needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace RandomSelector | |
{ | |
public partial class frmRandomSelector : Form | |
{ | |
public List<string> prizes = new List<string>(); | |
public List<string> participants = new List<string>(); | |
public List<Label> prizeLabels = new List<Label>(); | |
public List<TextBox> winnerBoxes = new List<TextBox>(); | |
private string listboxInputFileName; | |
private string categoryInputFileName; | |
private string gbText; | |
private string labelText; | |
private string textBoxText; | |
private GroupBox gbDynamicGroupBox; | |
private Label lbl_TBDescriptions; | |
private Label lbl_LabelDescriptions; | |
public frmRandomSelector() | |
{ | |
InitializeComponent(); | |
//The following lines ensure the Form grows with the dynamic content | |
this.AutoSizeMode = AutoSizeMode.GrowAndShrink; | |
this.AutoSize = true; | |
this.Padding = new Padding(0, 0, 24 + bQuit.Width , 0); | |
//Load Content | |
ReadArgs(); | |
LoadListBox(); | |
LoadCategories(); | |
//Load Dynamic Controls | |
Dynamic_Components(); | |
} | |
#region Form Controls | |
private void frmRandomSelector_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.Escape) | |
{ | |
this.Dispose(); | |
} | |
} | |
private void btnCancel_press(object sender, EventArgs e) | |
{ | |
this.Dispose(); | |
} | |
private void bSelect_Click(object sender, EventArgs e) | |
{ | |
foreach (TextBox t in winnerBoxes) | |
{ | |
//Instantiate a random number and seed it randomly for max randomness | |
Random randIndex = new Random(Guid.NewGuid().GetHashCode()); | |
int r = randIndex.Next(0, participants.Count); | |
t.Text = participants[r]; | |
participants.RemoveAt(r); | |
} | |
} | |
#endregion | |
#region Load Data | |
private void LoadListBox(string inputFile = "inputParticipants.csv") | |
{ | |
try | |
{ | |
using (StreamReader sr = new StreamReader(listboxInputFileName)) | |
{ | |
while (!sr.EndOfStream) | |
{ | |
participants.Add(sr.ReadLine().Split(',')[0]); | |
} | |
lbItems.Items.AddRange(participants.ToArray()); | |
} | |
} | |
catch | |
{ | |
lbItems.Items.Add("<No input file detected>"); | |
bSelect.Enabled = false; | |
} | |
} | |
private void LoadCategories() | |
{ | |
try | |
{ | |
//Read Input file for Category labels | |
prizes.AddRange(new StreamReader(categoryInputFileName).ReadToEnd().Replace("\r\n", "\r").Split('\r')); | |
} | |
catch | |
{ | |
Label warningLabel = new Label(); | |
warningLabel.Location = new Point(lbItems.Left, lbItems.Bottom + 24); | |
warningLabel.AutoSize = true; | |
warningLabel.ForeColor = Color.Red; | |
warningLabel.Text = "No Category Input File Found"; | |
this.Controls.Add(warningLabel); | |
} | |
} | |
private void ReadArgs() | |
{ | |
//Predefine string values to avoid nulls | |
gbText = "<group box description>"; | |
labelText = "<categories>"; | |
textBoxText = "<random content>"; | |
listboxInputFileName = "inputParticipants.csv"; | |
categoryInputFileName = "input.txt"; | |
//Parse through Args | |
string[] args = Environment.GetCommandLineArgs(); | |
for (int i = 1; i < args.Length; i++ ) | |
{ | |
string arg = args[i]; | |
//split the argument | |
string key = arg.Split('=')[0].ToLower(); | |
string value = arg.Split('=')[1]; | |
if (key == "selectionfile") | |
{ | |
listboxInputFileName = value; | |
} | |
else if (key == "categoryfile") | |
{ | |
categoryInputFileName = value; | |
} | |
else if (key == "gbtext") | |
{ | |
gbText = value; | |
} | |
else if (key == "label") | |
{ | |
labelText = value; | |
} | |
else if (key == "text") | |
{ | |
textBoxText = value; | |
} | |
} | |
} | |
private void Dynamic_Components() | |
{ | |
//Instantiate Controls | |
gbDynamicGroupBox = new GroupBox(); | |
lbl_TBDescriptions = new Label(); | |
lbl_LabelDescriptions = new Label(); | |
//Add Static Labels inside Group Box | |
this.gbDynamicGroupBox.Controls.Add(this.lbl_TBDescriptions); | |
this.gbDynamicGroupBox.Controls.Add(this.lbl_LabelDescriptions); | |
lbl_TBDescriptions.Text = textBoxText; | |
lbl_TBDescriptions.Location = new Point(8, 20); | |
lbl_LabelDescriptions.Text = labelText; | |
lbl_LabelDescriptions.Location = new Point(200, 20); | |
for (int i = 0; i < prizes.Count; i++) | |
{ | |
Label dynLabel = new Label(); | |
TextBox dynTextBox = new TextBox(); | |
//Set up dynamic Textbox | |
dynTextBox.Location = new Point(8, (lbl_TBDescriptions.Bottom + (i * 24))); | |
dynTextBox.Size = new Size(185, 20); | |
//Set up Dynamic Labels | |
dynLabel.Text = prizes[i]; | |
dynLabel.Location = new Point((dynTextBox.Right + 12), (dynTextBox.Top + 6)); | |
dynLabel.AutoSize = true; | |
//Add to Lists | |
winnerBoxes.Add(dynTextBox); | |
prizeLabels.Add(dynLabel); | |
//Add to GroupBox | |
this.gbDynamicGroupBox.Controls.Add(dynTextBox); | |
this.gbDynamicGroupBox.Controls.Add(dynLabel); | |
} | |
this.gbDynamicGroupBox.Location = new Point(288, 14); | |
this.gbDynamicGroupBox.Size = new Size(264, 100); | |
this.gbDynamicGroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink; | |
this.gbDynamicGroupBox.AutoSize = true; | |
this.gbDynamicGroupBox.Padding = new Padding(0, 0, 0, 0); | |
this.gbDynamicGroupBox.Text = gbText; | |
this.Controls.Add(gbDynamicGroupBox); | |
int bsX = this.Width - (bSelect.Width + 24); | |
int bsY = this.Height - (bSelect.Height + 48 + bQuit.Height); | |
int bqX = this.Width - (bQuit.Width + 24); | |
int bqY = this.Height - (bQuit.Height + 48); | |
bSelect.Location = new Point(bsX, bsY); | |
bQuit.Location = new Point(bqX, bqY); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment