private void lstStyle_SelectedIndexChanged(object sender, System.EventArgs e) { //strInputValue = this.lstStyle.Text + ":" + this.cboFormat.Text ; this.bolModified = true; this.cboFormat.Items.Clear(); ValueFormatStyle style = (ValueFormatStyle)Enum.Parse(typeof(ValueFormatStyle), lstStyle.Text, true); string[] formats = myFormats[style] as string[]; if (formats != null) { cboFormat.Items.AddRange(formats); } }
/// <summary> /// 解释字符串设置对象数据 /// </summary> /// <param name="strText">字符串</param> public void Parse(string strText) { intStyle = ValueFormatStyle.None; strFormat = null; _NoneText = null; if (HasContent(strText)) { string[] items = strText.Split(','); if (items.Length > 0) { foreach (string item in items) { string txt = items[0].Trim(); int index = txt.IndexOf("="); if (index >= 0) { string name = txt.Substring(0, index).Trim(); txt = txt.Substring(index + 1).Trim(); if (string.Compare(name, "format", true) == 0) { strFormat = txt; } else if (string.Compare(name, "nonetext", true) == 0) { _NoneText = txt; } } else { bool find = false; foreach (string name in Enum.GetNames(typeof(ValueFormatStyle))) { if (string.Compare(name, txt, true) == 0) { intStyle = (ValueFormatStyle)Enum.Parse(typeof(ValueFormatStyle), name); find = true; break; } } if (find == false) { strFormat = txt; } } } }//if } }
/// <summary> /// 初始化对象 /// </summary> /// <param name="style">类型</param> /// <param name="format">格式</param> /// <param name="naNText">数据空时的文本</param> public ValueFormater(ValueFormatStyle style, string format, string naNText) { intStyle = style; strFormat = format; _NoneText = NoneText; }
/// <summary> /// 初始化对象 /// </summary> /// <param name="style">类型</param> /// <param name="format">格式</param> public ValueFormater(ValueFormatStyle style, string format) { intStyle = style; strFormat = format; }
public static string Execute( ValueFormatStyle style, string format, string noneText, object Value) { if (Value == null || DBNull.Value.Equals(Value)) { // 数据为空 return(noneText); } if (format == null) { format = ""; } switch (style) { case ValueFormatStyle.None: { // 无任何格式转换 return(Convert.ToString(Value)); } case ValueFormatStyle.Currency: { // 货币类型 decimal dec = 0; bool convertFlag = false; if (Value is string) { string txt = (string)Value; if (HasContent(txt)) { try { convertFlag = decimal.TryParse(txt, out dec); } catch { } } } else if (IsNumberTypeFast(Value) || Value is bool) { dec = Convert.ToDecimal(Value); convertFlag = true; } if (convertFlag) { if (HasContent(format)) { return(dec.ToString(format)); } else { return(dec.ToString()); } } else { return(noneText); } //break; } case ValueFormatStyle.DateTime: { DateTime dtm = DateTime.MinValue; bool convertFlag = false; if (Value is DateTime) { dtm = (DateTime)Value; convertFlag = true; } else if (Value is string) { string txt = (string)Value; if (HasContent(txt)) { convertFlag = DateTime.TryParse(txt, out dtm); } } else if (IsNumberTypeFast(Value)) { dtm = new DateTime(Convert.ToInt64(Value)); convertFlag = true; } else { dtm = Convert.ToDateTime(Value); convertFlag = true; } if (convertFlag) { if (HasContent(format)) { return(dtm.ToString(format)); } else { return(dtm.ToLongDateString()); } } else { return(noneText); } } case ValueFormatStyle.Numeric: { double dbl = 0; bool flag = false; if (Value is string) { string txt = (string)Value; if (HasContent(txt)) { flag = double.TryParse(txt, out dbl); } } else if (IsNumberTypeFast(Value)) { dbl = Convert.ToDouble(Value); flag = true; } else { try { dbl = Convert.ToDouble(Value); flag = true; } catch { } } if (flag && double.IsNaN(dbl) == false) { if (HasContent(format)) { return(dbl.ToString(format)); } else { return(dbl.ToString()); } } else { return(noneText); } //break; } case ValueFormatStyle.Percent: { double dbl = 0; int dig = 0; int rate = 100; if (Value is string) { string txt = (string)Value; if (HasContent(txt)) { if (txt.IndexOf("%") > 0) { rate = 1; txt = txt.Replace("%", ""); } if (double.TryParse(txt, out dbl) == false) { dbl = double.NaN; } } else { return(noneText); } } else if (IsNumberTypeFast(Value)) { dbl = Convert.ToDouble(Value); } else { try { dbl = Convert.ToDouble(Value); } catch { return(noneText); } } if (Int32.TryParse(format, out dig) == false) { dig = 0; } if (dig < 0) { dig = 0; } if (double.IsNaN(dbl) == false) { dbl = Math.Round(dbl * rate, dig); if (dig == 0) { return(dbl.ToString() + "%"); } else { return(dbl.ToString("0." + new string('0', dig)) + "%"); } } else { return(noneText); } } case ValueFormatStyle.SpecifyLength: { int specifyLength = 0; string txt = Convert.ToString(Value); if (Int32.TryParse(format, out specifyLength)) { if (specifyLength > 0) { int len = 0; foreach (char c in txt) { if (c > 255) { len += 2; } else { len++; } } if (len < specifyLength) { return(Value + new string(' ', specifyLength - len)); } } } return(txt); } case ValueFormatStyle.String: { string txt = Convert.ToString(Value); if (HasContent(txt) == false) { return(txt); } if (HasContent(format) == false) { return(txt); } format = format.Trim(); if (string.Compare(format, "trim", true) == 0) { return(txt.Trim()); } //else if (string.Compare(format, "normalizespace", true) == 0) //{ // return string.StringFormatHelper.NormalizeSpace(Value); //} //else if (string.Compare(format, "htmltext", true) == 0) //{ // HTMLDocument doc = new HTMLDocument(); // doc.LoadHTML(Value); // string txt = doc.InnerText; // if (txt != null) // txt = txt.Trim(); // return txt; //} else if (string.Compare(format, "HtmlEncode", true) == 0) { return(System.Web.HttpUtility.HtmlEncode(txt)); } else if (string.Compare(format, "HtmlDecode", true) == 0) { return(System.Web.HttpUtility.HtmlDecode(txt)); } else if (string.Compare(format, "UrlEncode", true) == 0) { return(System.Web.HttpUtility.UrlEncode(txt)); } else if (string.Compare(format, "UrlDecode", true) == 0) { return(System.Web.HttpUtility.UrlDecode(txt)); } else if (string.Compare(format, "HtmlAttributeEncode", true) == 0) { return(System.Web.HttpUtility.HtmlAttributeEncode(txt)); } else if (string.Compare(format, "lower", true) == 0) { return(txt.ToLower()); } else if (string.Compare(format, "upper", true) == 0) { return(txt.ToUpper()); } format = format.ToLower(); if (format.StartsWith("left")) { int index = format.IndexOf(","); if (index > 0) { string left = format.Substring(index + 1); int len = 0; if (Int32.TryParse(left, out len)) { if (len > 0 && txt.Length > len) { return(txt.Substring(0, len)); } } } return(txt); } if (format.StartsWith("right")) { int index = format.IndexOf(","); if (index > 0) { string right = format.Substring(index + 1); int len = 0; if (Int32.TryParse(right, out len)) { if (len > 0 && txt.Length > len) { return(txt.Substring(txt.Length - len - 1, len)); } } } return(txt); } // else if( string.Compare( Format , "groupsplit" , true ) == 0 ) // { // int GroupSize = Convert.ToInt32( // Value = Value.Trim(); // // } return(txt); } case ValueFormatStyle.Boolean: { if (format == null) { return(Convert.ToString(Value)); } int index = format.IndexOf(","); string trueStr = format; string falseStr = ""; if (index >= 0) { trueStr = format.Substring(0, index); falseStr = format.Substring(index + 1); } bool bol = false; bool flag = false; if (Value is bool) { bol = (bool)Value; flag = true; } else if (Value is string) { flag = Boolean.TryParse((string)Value, out bol); } else { try { bol = Convert.ToBoolean(Value); flag = true; } catch { return(noneText); } } if (bol) { return(trueStr); } else { return(falseStr); } //return Value ; } } return(Convert.ToString(Value)); }