Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# User Control Question

Posted on 2006-05-16
4
Medium Priority
?
334 Views
Last Modified: 2010-04-16
Hello Experts,

I am developing a user control in C#.

I have a regular expression in my user control which allows
1)whole number from one to five
2)one to five digits before the dot and the one or two digits after the dot.

Basically it allows

X
XX
XXX
XXXX
XXXXX

X.X
X.XX
XX.XX
XXX.XX
XXXX.XX
XXXXX.XX all are valid values.

I want to display the values retruned from user control in currecy format in my calling aspx page.

I need the output to be formatted to $XXX.00 if it is a whole number, if it is a decimal only $ sign in front of the value.


This is my user control code:
----------------------------------------------

namespace CSharpWebApplication.UserControls
{
      using System;
      using System.Data;
      using System.Drawing;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;

      /// <summary>
      ///            Summary description for CurrencyValidator.
      /// </summary>
      public class CurrencyValidator : System.Web.UI.UserControl
      {
            protected System.Web.UI.WebControls.TextBox txtCurrency;
            protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
            protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
            

            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            public string CurrencyValue
            {                                                      
                  get
                  {return Convert.ToInt32(txtCurrency.Text).ToString("c");}
            }

                        

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            ///            Required method for Designer support - do not modify
            ///            the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}

--------------------------------------------------------------
Thanks
0
Comment
Question by:mani_sai
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 16692289
You should be able to do:

Convert.ToInt32(txtCurrency.Text).ToString("$.00");

which will give:

$.50
$699.00
$4.40
$.37
etc...
$.00

The 0 in the mask indicates that you want to force a number to appear in that spot.
0
 
LVL 8

Author Comment

by:mani_sai
ID: 16692446
Thansk for your reply.

It works fine for any interger values (whole numbers) entered.

But when i enter decimal values, it throws up an error.

Error:Input string was not in a correct format.

So basically i need to check if it is interger or decimal and then call the correct syntax i beleive.

Sorry, I am new to C#. any help?

Thanks
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 2000 total points
ID: 16693336
You need to change this:
Convert.ToInt32(txtCurrency.Text).ToString("$.00");

to this:

Convert.ToDecimal(txtCurrency.Text).ToString("$.00");

It will then work for all.  Sorry I missed that the first time.
0
 
LVL 8

Author Comment

by:mani_sai
ID: 16693697
Thanks, it works fine.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question