c# - Implementing negative number validation for a windows form -
this question has answer here:
i have little code snippet i've been working on. provides validation numbers more 2 decimal places.
private void calculatebutton_click(object sender, eventargs e) { int amount; if (int.tryparse(amounttextbox.text, out amount)) { wantedtextbox.text = currency_exchange.exchangecurrency((currencies)currencycombobox.selectedindex, (currencies)wantedcurrencycombobox.selectedindex, amount).tostring("0.00"); wantedcurrencylable.text = ((currencies)wantedcurrencycombobox.selectedindex).tostring(); groupbox.visible = true; } else { messagebox.show("invalid amount"); }
now i've realized late should've implemented validation negative numbers aswell. way i've set code makes difficult. suggested declare parse of textbox boolean caused more trouble. how go this?
if tryparse
succeeds, parsed value stored in amount
, use first make sure it's valid number , then, if is, amount greater or equal 0:
if (int.tryparse(amounttextbox.text, out amount) && amount >= 0)
Comments
Post a Comment