using System;
using System.Windows.Forms;
namespace ExtensionMethods
{
///
/// Add extensions to
///
public static class ControlExtensions
{
///
/// Reset the value of all s in the given
/// Empty es, set es to N/A, uncheck es.
///
/// The whose children s shall be reset
public static void clearControls(this Control Control)
{
foreach (Control ctrl in Control.Controls)
{
ctrl.BackColor = System.Drawing.Color.White;
switch (ctrl.GetType().Name)
{
case "TextBox":
case "RichTextBox":
((TextBox)ctrl).Text = String.Empty;
((TextBox)ctrl).BackColor = System.Drawing.Color.White;
break;
case "ComboBox":
((ComboBox)ctrl).Text = String.Empty;
break;
case "CheckBox":
((CheckBox)ctrl).Checked = false;
break;
case "Control":
case "Panel":
case "SplitContainer":
case "SplitterPanel":
case "TabPage":
ctrl.clearControls();
break;
case "CheckedListBox":
foreach (object i in ((CheckedListBox)ctrl).CheckedIndices)
((CheckedListBox)ctrl).SetItemChecked((int)i, false);
break;
#if DEBUG
case "ProgressBar":
case "Button":
case "Label":
case "DataGridView":
case "DataGrid":
case "PictureBox":
break;
default:
MessageBox.Show(ctrl.Name + ": " + ctrl.GetType());
break;
#endif
}
}
}
///
/// Set .readOnly or .Enabled of all s in the given to
///
/// The which children s behaviour shall be set to
/// Read only
public static void setControlsReadOnly(this Control Control, bool readOnly)
{
foreach (Control ctrl in Control.Controls)
{
switch (ctrl.GetType().Name)
{
case "TextBox":
((TextBox)ctrl).ReadOnly = readOnly;
break;
case "RichTextBox":
((RichTextBox)ctrl).ReadOnly = readOnly;
break;
case "ComboBox":
((ComboBox)ctrl).ReadOnly(readOnly);
break;
case "CheckBox":
case "Button":
case "CheckedListBox":
ctrl.Enabled = !readOnly;
break;
case "Panel":
case "TabPage":
case "SplitContainer":
case "SplitterPanel":
ctrl.setControlsReadOnly(readOnly);
break;
#if DEBUG
case "ProgressBar":
case "Label":
case "DataGridView":
case "DataGrid":
case "PictureBox":
break;
default:
MessageBox.Show(ctrl.Name + ": " + ctrl.GetType().Name);
break;
#endif
}
}
}
///
/// Set .Enabled of all s in the given to
///
/// The which children s shall be reset
/// Enable
public static void setControlsEnabled(this Control Control, bool enabled)
{
foreach (Control ctrl in Control.Controls)
{
switch (ctrl.GetType().Name)
{
case "TextBox":
case "RichTextBox":
case "ComboBox":
case "CheckBox":
case "Button":
case "CheckedListBox":
ctrl.Enabled = enabled;
break;
case "Panel":
case "TabPage":
case "SplitContainer":
case "SplitterPanel":
ctrl.setControlsEnabled(enabled);
break;
#if DEBUG
case "ProgressBar":
case "Label":
case "DataGridView":
case "DataGrid":
case "PictureBox":
break;
default:
MessageBox.Show(ctrl.Name + ": " + ctrl.GetType().Name);
break;
#endif
}
}
}
}
}