string - Textbox for price/cash/currency on C# -
i have problem haunting me while. tried solutions didn't worked.
i have textbox cash input ($999,99 example). need automatically input "," , "." display value correctly.
i tried 2 solutions. 1 of them this:
private void tx_valorunidade_textchanged(object sender, eventargs e) { string value = tx_valorunidade.text.replace(",", "").replace("r$", ""); decimal ul; //check indeed handling number if (decimal.tryparse(value, out ul)) { //unsub event don't enter loop tx_valorunidade.textchanged -= tx_valorunidade_textchanged; //format text currency tx_valorunidade.text = string.format(system.globalization.cultureinfo.createspecificculture("pt-br"), "{0:c2}", ul); tx_valorunidade.textchanged += tx_valorunidade_textchanged; } }
the result, however, weird. words may fail on one, recorded video: http://www.screenr.com/2slh
the other 1 this:
private void tx_valorunidade_keyup(object sender, keyeventargs e) { if (!string.isnullorempty(tx_valorunidade.text)) { system.globalization.cultureinfo culture = new system.globalization.cultureinfo("en-us"); int valuebefore = int32.parse(tx_valorunidade.text, system.globalization.numberstyles.allowthousands); tx_valorunidade.text = string.format(culture, "{0:n0}", valuebefore); tx_valorunidade.select(tx_valorunidade.text.length, 0); * } }
this 1 kinda works, there issue: if user wants insert somethink $10,00 can't. crashes after 5 numbers. video that: http://www.screenr.com/1slh
for original reference, got 2 codes other questions here.
how can fix it? using examples wrong? thought welcome.
i think better off when formatting when user moves next control, e.g. below. otherwise confusing text change user typing:
private void textbox1_leave(object sender, eventargs e) { double value; if (double.tryparse(textbox1.text, out value)) textbox1.text = string.format(system.globalization.cultureinfo.currentculture, "{0:c2}", value); else textbox1.text = string.empty; }
Comments
Post a Comment