簡體   English   中英

比較C#中的兩個文本框

[英]Compare two textbox in c#

如何使用大於或小於比較c#中的兩個文本框。 我在計費系統上工作。 我希望現金應該大於總數。 如果不。 它將彈出錯誤消息框。 我已經嘗試將其轉換為int。 加倍。 串起來。 這是我當前的代碼。

if (txtCash.TextLength < txtTotal.TextLength){
    MessageBox.Show("Cash must me bigger than the        total");
    txtCash.Focus();
    return;
  }

誰能幫助我? 我知道文字長度錯誤。 但我只是用它。

我正在開發計費系統。 我希望現金應該大於總數。 如果不。 它將彈出錯誤消息框。

您可能正在尋找這樣的東西:

if (double.Parse(txtCash.TextLength) <= double.Parse(txtTotal.TextLength))
{
    MessageBox.Show("Cash must me bigger than the total");
    txtCash.Focus();
    return; // i dont know why you put this here but i'll leave it there anyway.
}
else
{
    var amount = double.Parse(txtCash.TextLength);
    // DO something
}

如果需要,您可能希望進一步驗證用戶輸入。 例如,如果用戶未輸入數字...


更好的方法以盡可能強大的方式完成任務,您應該考慮以下鏈接:

如何制作僅接受數字的文本框?

double.TryParse(...)

更新:

似乎您不確定關鍵字var 我建議您深入閱讀var的MSDN參考頁。

暫無
暫無

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

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