textBox1.SelectionStart = textBox1.Text.Length; textBox1.SelectionLength = 0; textBox1.ScrollToCaret();
21/05/2009
autoscroll text
ArrayList to array
using System.Collections;
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");
string[] strArray = arrList.ToArray(Type.GetType("System.String")) as string[];
return strArray;
Fill combobox with enum values
public enum CardDataType
{
Unknown,
Raw,
RowChp
}
ctlDataType.DataSource = Enum.GetValues(typeof(CardDataType));
ctlDataType.SelectedItem = CardDataType.Unknown;
...
...
string strType = ctlDataType.SelectedItem.ToString();
or
CardDataType dataType;
string strDataType = "Raw";
dataType = (CardDataType)Enum.Parse(typeof(CardDataType), strDataType, true);
and eventually
ctlDataType.SelectedItem = dataType;
// a volte da' problemi in fase di inizializzazione della UI;
// in questi casi si puo' ricorrere a Enum.GetNames()
foreach ( string name in Enum.GetNames(typeof(CardDataType)) )
{
ctlDataType.Items.Add(name);
}
...
string strDataType = "Raw";
ctlDataType.SelectedItem = strDataType;
String to date/time conversion
public static DateTime atodt( string text ) { // Formati previsti : "AAAAMMGG" oppure "HHMM" DateTime rc = new DateTime(); rc = DateTime.MinValue; string strDateTime = text.Trim(); switch ( strDateTime.Length ) { case 8 : { int year = atoi(strDateTime.Substring(0,4)); int month = atoi(strDateTime.Substring(4,2)); int day = atoi(strDateTime.Substring(6,2)); rc = new DateTime(year,month,day); } break; case 4 : { int hours = atoi(strDateTime.Substring(0,2)); int minutes = atoi(strDateTime.Substring(2,2)); rc = DateTime.MinValue; rc = rc.AddHours(hours); rc = rc.AddMinutes(minutes); } break; } return rc; } public static TimeSpan atohhmmss( string text ) { // Formato previsto : "HHMMSS" TimeSpan rc = new TimeSpan(); rc = TimeSpan.MinValue; string strDateTime = text.Trim(); if ( strDateTime.Length == 6 ) { int hours = atoi(strDateTime.Substring(0,2)); int minutes = atoi(strDateTime.Substring(2,2)); int seconds = atoi(strDateTime.Substring(4,2)); rc = new TimeSpan(hours,minutes,seconds); } return rc; }
String to int/double conversion
// Double to string conversion; always uses '.' as decimal separator public static string dtoa( double value ) { string text = value.ToString("0.#######"); return text.Replace(",","."); } // String to int conversion // from http://www.c-sharpcenter.com/CSNET/string.asp public static int atoi( string str ) { int sign = 1; int TheNumber = 0; int tmp = 0; int x = 0; //int l = (str.Length )-1; int l = str.Length; if ( str.Length > 0 ) { char [] c = str.ToCharArray(); if ('-'==c[0]) { sign=-1; x=1; } for ( ; x<l; x++ ) { if ((c[x]>='0')&&(c[x]<='9')) { tmp =(c[x]-'0'); TheNumber = TheNumber * 10 + tmp; } } } return TheNumber * sign; } // String to double conversion; both '.' and ',' are treated as decimal separator public static double atod( string str ) { int sign = 1; double TheNumber = 0.0; int tmp = 0; int x = 0; //int l = (str.Length )-1; int l = str.Length; bool bDot = false; int normalizer = 1; if ( str.Length > 0 ) { char [] c = str.ToCharArray(); if ('-'==c[0]) { sign=-1; x=1; } for ( ; x<l; x++ ) { if ((c[x]>='0')&&(c[x]<='9')) { tmp =(c[x]-'0'); TheNumber = TheNumber * 10.0 + tmp; if ( bDot ) { normalizer *= 10; } } else if ( c[x]=='.' || c[x]==',' ) { bDot = true; } } } return (TheNumber * sign)/normalizer; }