簡體   English   中英

將Xaml文本框轉換為double,但是如何驗證用戶輸入是否為double,而不是Windows 8應用程序的c ++中的字符串

[英]Converting Xaml textbox to double, but how to validate that the user input is double not a string in c++ for the windows 8 app

這就是我為xaml中的文本框准備的內容。

<TextBox x:Name="number1" HorizontalAlignment="Left" Height="85" Margin="115,239,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="328" FontSize="60" TextAlignment="Center"/>

這就是我要從文本框中獲取文本的內容:

void Simple_Calculator_4_Kids::MainPage::add_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
String^ str1 = txtNumberToMultiply->Text; 
wstring ws1( str1->Data());
wstringstream convertor; 
double ws1_d;
convertor << ws1; 
convertor >> ws1_d; 
ws1_d = ws1_d + 2.2; 
}

我不明白如何添加一種方式來驗證文本框中的內容實際上是雙精度而不是字母字符串或為空。 我將如何添加一種方法來驗證文本框中的內容是否為重復值呢? 這是使用Visual Studio 2012 for Windows 8應用程序的C ++。

使用C運行時庫中的strtod(將字符串轉換為雙精度值)。 根據文檔,對照功能測試您的文本輸入並檢查錯誤。 Windows Store應用程序通過C ++和CRT支持strtod(和朋友)。

http://msdn.microsoft.com/zh-CN/library/kxsfc1ab(v=vs.110).aspx

我花了一段時間才弄清楚wstring如何使用char16保持輸入,因此我編寫了這段代碼。 它只是檢查期間和數字。

bool isValidInput(String^ str){
    int decimalX = 0;
    wstring ws(str->Data());
    int strSize = ws.length();
    if(strSize<1)
        return false;
    else{
        for(int i=0;i<strSize;i++){
            if(ws[i]==46||ws[i]==48||ws[i]==49||ws[i]==50||ws[i]==51||ws[i]==52||ws[i]==53||ws[i]==54||ws[i]==55||ws[i]==56||ws[i]==57){
                if(ws[i]==46){
                    decimalX++;
                    if(decimalX>1)
                        return false;
                }
            }
            else
                return false;
        }
        return true;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM