I put together a very simple control that will take any number of configuration key,value pairs as input. It will display them in a list, allow the user to change what they wish and submit the modified values back to the client.
The form is very simple, it contains a layout panel, two labels and two buttons.
To use it, you simply build up a Dictionary
So there you go! A very simple control to allow a user to modify any name/value pairs of data.
Here are the file contents.
SimpleConfigurationControl.Designer.cs
using System.Windows.Forms;namespace SimpleConfigurationControl
{
partial class SimpleConfiguration
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSave = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.lblConfigKey = new System.Windows.Forms.Label();
this.lblConfigVal = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(546, 400);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 5;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(13, 400);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// tableLayoutPanel
//
this.tableLayoutPanel.Anchor = System.Windows.Forms.AnchorStyles.None;
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45F));
this.tableLayoutPanel.Location = new System.Drawing.Point(13, 61);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.RowCount = 2;
this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel.Size = new System.Drawing.Size(608, 333);
this.tableLayoutPanel.TabIndex = 6;
//
// lblConfigKey
//
this.lblConfigKey.AutoSize = true;
this.lblConfigKey.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblConfigKey.Location = new System.Drawing.Point(12, 25);
this.lblConfigKey.Name = "lblConfigKey";
this.lblConfigKey.Size = new System.Drawing.Size(141, 20);
this.lblConfigKey.TabIndex = 7;
this.lblConfigKey.Text = "Configuration Key";
//
// lblConfigVal
//
this.lblConfigVal.AutoSize = true;
this.lblConfigVal.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblConfigVal.Location = new System.Drawing.Point(328, 24);
this.lblConfigVal.Name = "lblConfigVal";
this.lblConfigVal.Size = new System.Drawing.Size(155, 20);
this.lblConfigVal.TabIndex = 8;
this.lblConfigVal.Text = "Configuration Value";
//
// SimpleConfiguration
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(633, 446);
this.Controls.Add(this.lblConfigVal);
this.Controls.Add(this.lblConfigKey);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.tableLayoutPanel);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SimpleConfiguration";
this.Text = "SimpleConfiguration";
this.TopMost = true;
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;
private Label lblConfigKey;
private Label lblConfigVal;
}
}
SimpleConfigurationControl:
using System.Collections.Generic;using System.Net.Mime;
using System.Windows.Forms;
namespace SimpleConfigurationControl
{
public partial class SimpleConfiguration : Form
{
private Dictionary
public Dictionary
{
get { return _configurationValues; }
set
{
_configurationValues = value;
DisplayConfigurationValues();
}
}
public SimpleConfiguration()
{
InitializeComponent();
}
private void DisplayConfigurationValues()
{
//get the table width
var layoutPanelWidth = tableLayoutPanel.Width;
//subtract a few points for borders etc
var cellWidth = (layoutPanelWidth/2) - 10;
foreach (var configurationValue in _configurationValues)
{
var lbl = new Label()
{
Name = string.Format("{0}{1}", "lbl", configurationValue.Key),
Text = configurationValue.Key,
Width = cellWidth
};
var txtBox = new TextBox()
{
Name = string.Format("{0}{1}", "txt", configurationValue.Key),
Text = configurationValue.Value,
Width = cellWidth
};
tableLayoutPanel.RowCount = tableLayoutPanel.RowCount + 1;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 45F));
tableLayoutPanel.Controls.Add(lbl, 0, tableLayoutPanel.RowCount-1);
tableLayoutPanel.Controls.Add(txtBox, 1, tableLayoutPanel.RowCount - 1);
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
var returnDictionary = new Dictionary
foreach (var configurationValue in _configurationValues)
{
var genericTxtBox = Controls.Find("txt" + configurationValue.Key, true);
foreach (var control in genericTxtBox)
{
if (control is TextBox)
{
returnDictionary.Add(control.Name.Replace("txt", ""), control.Text);
}
}
}
_configurationValues = returnDictionary;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}
No comments:
Post a Comment