Displaying currency in a MaskedTextBox control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm using C# to create a printable form. The data is coming from a XML
source that I load when the application starts. I have no problems loading
the data, but I am having a hard time figuring out the best way to display
currency in a field. I can use a MaskedTextBox, but it doesn't work in every
situation. Are there any good code samples to show how to resolve this
display issue? Thanks in advance!
 
Hi,

Have you tried using the String.Format() method ?

as in :

Text1.Text = String.Format("{0:c}", price);

HTH,

Regards,

Cerebrus.
 
Thank you for the tip! I cleared my previous mask setting in the Mask
property and updated my code to include your code as well:

protected void FillForm(XmlDocument oXDoc, Form pForm)
{
// Fill in each control
int n = pForm.Controls.Count - 1;
for (int i = 0; i < n ; i++)
{
// set the text of the control
string strCtrl = pForm.Controls.Name;
string strType =
pForm.Controls.GetType().ToString().Substring(pForm.Controls.GetType().ToString().LastIndexOf(".") + 1);
XmlNodeList xmlNode = oXDoc.GetElementsByTagName(strCtrl);

// Make sure we have something to work with
if (xmlNode.Count > 0)
{
if (strType == "TextBox")
{
pForm.Controls.Text = xmlNode[0].InnerText;
}
else if (strType == "MaskedTextBox")
{
pForm.Controls.Text = String.Format("{0:c}",
xmlNode[0].InnerText);
}
}
}
}


Again, thanks for your help!
 
Back
Top