[英]C# Double.TryParse equivalent on compact framework
我們想要在Win CE 5.0供電的設備上實現僅輸入資金的文本框(###。00)。 正在使用.NET Compact Framework 3.5(C#)開發該應用程序。
建議我采用以下解決方案:
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
double amount = 0.0d;
if (double.TryParse(txtbox1.Text, NumberStyles.Currency, null, out amount))
{
textbox.Text = amount.ToString("C");
}
}
(緊湊框架不支持Decimal.TryParse
)?
本文介紹了如何通過創建自己的try parse方法來做到這一點:
http://www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html
似乎是一個不錯的解決方案。
緊湊框架不支持TryParse。
您可以將其替換為
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
double amount = 0.0d;
try
{
amount = Convert.ToDouble(txtbox1.Text);
textbox.Text = amount.ToString("C");
}
catch
{
}
}
或參考此博客以獲取TryParse for CF的實現: http : //www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html
我無法發表評論,所以我必須回答,但據我所知3.5框架不支持TryParse。
就像其他人發布的一樣, http://www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html是一項很好的工作,不需要太多的工作。
您可以檢查每個方法頁面以找到特定的詳細信息。 http://msdn.microsoft.com/en-us/library/994c0zb1%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/zc2x2b1h%28v=vs.110 %29.aspx http://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/9hh1awhy%28v = vs.110%29.aspx
我對此發表評論,但我還需要3個因果關系,哈哈!
緊湊型框架的TryParse
/// <summary>
/// Contains methods to assist with parsing one value into another.
/// </summary>
public static class ParseAssistant
{
#region TryParse Overloads
/// <summary>
/// Attempts to parse the string provided into an integer value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out int result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToInt32(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = int.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a byte value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out byte result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToByte(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = byte.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into an Int16 value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out Int16 result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToInt16(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = Int16.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into an Int64 value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out Int64 result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToInt64(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = Int64.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a decimal value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out decimal result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToDecimal(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = decimal.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a float value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out float result)
{
bool retVal = false;
#if WindowsCE
try
{
result = (float)Convert.ToDecimal(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = float.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a double value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out double result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToDouble(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = double.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into an sbyte value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out sbyte result)
{
bool retVal = false;
#if WindowsCE
try
{
result = (sbyte)Convert.ToInt32(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = sbyte.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a uint value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out uint result)
{
bool retVal = false;
#if WindowsCE
try
{
result = (uint)Convert.ToUInt64(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = uint.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a ulong value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out ulong result)
{
bool retVal = false;
#if WindowsCE
try
{
result = (ulong)Convert.ToUInt64(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = ulong.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into a ushort value.
/// </summary>
/// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out ushort result)
{
bool retVal = false;
#if WindowsCE
try
{
result = (ushort)Convert.ToUInt64(s);
retVal = true;
}
catch (FormatException) { result = 0; }
catch (InvalidCastException) { result = 0; }
#else
retVal = ushort.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into an <see cref="System.DateTime"/> value.
/// </summary>
/// <remarks>Returns <see cref="System.DateTime.MinValue"/> in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or <see cref="System.DateTime.MinValue"/> if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out DateTime result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToDateTime(s);
retVal = true;
}
catch (FormatException) { result = DateTime.MinValue; }
catch (InvalidCastException) { result = DateTime.MinValue; }
#else
retVal = DateTime.TryParse(s, out result);
#endif
return retVal;
}
/// <summary>
/// Attempts to parse the string provided into an integer value.
/// </summary>
/// <remarks>Returns false in the result parameter if the parse fails.</remarks>
/// <param name="s">The string to attempt to parse.</param>
/// <param name="result">The result of the parsed string, or false if parsing failed.</param>
/// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParse(string s, out bool result)
{
bool retVal = false;
#if WindowsCE
try
{
result = Convert.ToBoolean(s);
retVal = true;
}
catch (FormatException) { result = false; }
catch (InvalidCastException) { result = false; }
#else
retVal = bool.TryParse(s, out result);
#endif
return retVal;
}
#endregion
}
http://www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.