|
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
/*
Author: Philip McGahan
Email: pmcgahan@hteinc.com, McGahanFL@Earthlink.net
The author maintains no rights or restriction on this code, its reuse or modification.
Date: 09/15/2006
*/
namespace PFM_Wrap_Ctrl_Text_Property
{
/// <summary>
/// N:PFM_Wrap_Ctrl_Text_Property: Used as a standard way to get/set values of a control.
/// </summary>
///
/// <summary>
/// Documentation tags based on http://www.codeproject.com/useritems/C__Documentation_Proess.asp
/// by Jaiprakash M Bankolli
/// N denotes the Namespace
///T denotes Type,and includes class, Interface, struct, enum or delegates
///F Denotes the fields of class
///P Denotes the Property indexer or indexed property
///M Denotes the Methods special methods constructor and operator(overloaded)
///E Denotes Events
///! Denotes error string that the C# was unable to resolve some references
/// </summary>
public class SetGetCtrlText
{
/// <summary>
/// T:SetGetCtrlText: Used as a standard way to get/set values to text and text-like controls.
/// Supports: Radio, checkbox and controls that use '.TEXT' property.
/// ToDo:Enhance this class to support additional control types.
/// </summary>
///
public string getText(Control ctrl)
{
/// <summary>
/// M:getText: Returns a value from a control.
/// Would need modification to support non Text and Text-like controls
/// </summary>
///
string sValueReturn = null;
Type ctrlType = ctrl.GetType();
if (ctrlType.Name == "CheckBox")
{
CheckBox tmp = (CheckBox)ctrl;
sValueReturn = "0";
if (tmp.Checked == true)
sValueReturn = "1";
}
else if (ctrlType.Name == "RadioButton")
{
RadioButton tmp = (RadioButton)ctrl;
sValueReturn = "0";
if (tmp.Checked == true)
sValueReturn = "1";
}
else if(ctrlType.Name == "ProgressBar")
{
//TODO-PROGRESS-BAR
//example of how to support more controls.
// 1 of 2. Save/Restore progress bar
// I do not want to set default data on a progress bar but if you did you would need code
// like this.
// ProgressBar tmp = (ProgressBar)ctrl;
// sValueReturn = tmp.Value.ToString();
}
else if (ctrlType.Name == "CheckedListBox")
{//this shows the limitation of this process. We can not save a controls state unless
//it can be converted to a single string.
StringBuilder sValues = new StringBuilder();
CheckedListBox tmp = (CheckedListBox)ctrl;
for (int iIndex = 0; iIndex < tmp.Items.Count; iIndex++)
{
if (tmp.GetItemChecked(iIndex) == false)
sValues.Append("0");
else
sValues.Append("1");
}
sValueReturn = sValues.ToString();
}
else
{//for control types that support the Text attribute
sValueReturn = ctrl.Text;
}
return sValueReturn;
}//getText
public void setText(Control ctrl, string strValue)
{
/// <summary>
/// M:setText: Sets a value to a control.
/// Would need modification to support non Text and Text-like controls
/// </summary>
///
Type ctrlType = ctrl.GetType();
if (ctrlType.Name == "CheckBox")
{
bool bState = false;
if (strValue == "1")
bState = true;
CheckBox tmp = (CheckBox)ctrl;
tmp.Checked = bState;
}
else if (ctrlType.Name == "RadioButton")
{
bool bState = false;
if (strValue == "1")
bState = true;
RadioButton tmp = (RadioButton)ctrl;
tmp.Checked = bState;
}
else if(ctrlType.Name == "ProgressBar")
{
//TODO-PROGRESS-BAR
//example of how to support more controls.
// 1 of 2. Save/Restore progress bar
// I do not want to set default data on a progress bar but if you did you would need code
// like this.
//ProgressBar tmp = (ProgressBar)ctrl;
//tmp.Value = Convert.ToInt16(strValue);
}
else if (ctrlType.Name == "CheckedListBox")
{//this shows the limitation of this process. We can not save a controls state unless
//it can be converted to a single string.
CheckedListBox tmp = (CheckedListBox)ctrl;
for (int iIndex = 0; iIndex < tmp.Items.Count; iIndex++)
{
if(strValue.Substring(iIndex, 1) == "0")
tmp.SetItemCheckState(iIndex, CheckState.Unchecked);
else
tmp.SetItemCheckState(iIndex, CheckState.Checked);
}
}
else
{
ctrl.Text = strValue;
}
}//setText
}//SetGetCtrlText
}//Wrap_ControlValue
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.