您好研究員C#和Windows Phone開發者,小數點Windows phone的文本框
對於我的Windows Phone應用程序,我有要求用戶輸入他們的年齡一個文本框。在調試模式下,我輸入了數字.8。並點擊繼續,應用程序意外關閉。我需要添加什麼代碼才能發佈消息框,通知用戶具有多個小數點的數字是不可接受的。請幫助
您好研究員C#和Windows Phone開發者,小數點Windows phone的文本框
對於我的Windows Phone應用程序,我有要求用戶輸入他們的年齡一個文本框。在調試模式下,我輸入了數字.8。並點擊繼續,應用程序意外關閉。我需要添加什麼代碼才能發佈消息框,通知用戶具有多個小數點的數字是不可接受的。請幫助
假設輸入一個字符串,請嘗試:
if (input.IndexOf('.') == -1 || input.LastIndexOf('.') == input.IndexOf('.'))
{
//good
}
else
MessageBox.Show("More than one decimal point");
一個更好的辦法,雖然是使用的TryParse將檢查該號碼,格式化
float age;
if (float.TryParse(input, out age))
{
//good
}
else
MessageBox.Show("Invalid age.");
一個方法是當用戶輸入他們的輸入時,將小數點輸入的位數限制在小數點後一位。
這會更好,因爲它是實時的,而不是在最後檢查它。
private void tbx_KeyDown(object sender, KeyEventArgs e)
{
//mark the sneder as a textbox control so we can access its properties
TextBox textBoxControl = (TextBox)sender;
//if there is already a decimals, do not allow another
if (textBoxControl.Text.Contains(".") && e.PlatformKeyCode == 190)
{
e.Handled = true;
}
}
謝謝你的幫助,我把inputscope放到數字上 – KPath001 2012-07-07 00:42:28