Search code examples
c#windows-phone-7string-conversion

WP7: What is the easiest way to convert a group of textboxes from text to a number or decimal value?


So I have users inputting numbers into multiple text boxes and I need to check that they are not null and convert them to decimal. Is there a simpler way to do this than a separate IF for each textbox?

if (txtBoxAuto1.Text != null)
{

    String varStrTxtBox1 = txtBoxAuto1.Text;
    decimal varTxtBox1 = Decimal.Parse(varStrTxtBox1);


}

I tried putting an "and" after the first textbox and it didn't seem to like it.


Solution

  • When I did such stuff, I added the Textboxes into a list to loop thought them. Or if you have all Textboxes in a panel you can loop thought them just like

    foreach(Control c in this.panel1.Controls)
            {
                if (c.GetType() == typeof(TextBox) && c.Text != String.Empty)
                {
                    decimal myValue = Convert.ToDecimal(c.Text);
                }
            }
    

    Or did I miss the point?