I am using below Property example to make some calculation on textbox
and if textbox is null I am assigning zero to it so calculation won't fail as you can see I am using Math.Round
and I want to make several checks on these textbox
input like
textbox that only accepts numbers I searched and found method 1
I want my textbox to be formated I searched and found Method 2
Now my question is ..
Is there any way to mareg all these method in the property method I am using so my code won't be like "spaghetti code" ?
is there any better ways to do these checks ?
Thank you in advance
Property example
public double ItemPriceResult
{
get
{
return Math.Round(ItemCost * RevenuePercentage / 100 + ItemCost, 0);
}
}
Method 1
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
{
MessageBox.Show("Please enter only numbers.");
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
Method 2
textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text));
UPDATE after some answers
MaskedTextBox
seems fit my needs I read and searched and below some question
if you kindly would like to help me
I need to use MaskedTextBox
because I can set it to accept
number and I can also force number formating so
also I need to make number textboxs easer to read for users
so 1000 will be come 1,000 and 10000 will be come 10,000
then according to Microsoft Docs formating MaskedTextBox
to fit my needs
Masked MaskedTextBox
with 999,999,999
,
second I do not want the PromptChar
to be visible I google it but none of search result did it