Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Formatting a bound textbox to show currency?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mracuraintegra@gmail.com - 16 Nov 2006 02:25 GMT
I am seriously at my wits' end here with this. I've got a textbox bound
to a money column in a database, but it shows up as a decimal value,
meaning that $9.25 shows up as 9.250000. ARRRRG.

Here's what I've tried:
* String.Format("{0:C}", txtCalcValue.Text) (guess this only works on
console...)
* Masked text box with ###.## mask
* txtCalcValue.Text = txtCalcValue.Remove(txtCalcValue.Length, 4)

I've also tried to create my own currency textbox control with masking,
but it didn't work much better than the masking above.

I've now spent 2.25 hours just trying to get this damnable textbox to
stop showing millionths of a cent! Any help would be appreciated.
Marc Gravell - 16 Nov 2006 06:18 GMT
Like so?

Marc

using System;
using System.Windows.Forms;

class Program
{
   static void Main()
   {
       MyData md = new MyData();
       md.Value = 123.45M;
       using (Form f = new Form())
       using (TextBox tb = new TextBox())
       {
           f.Controls.Add(tb);
           tb.DataBindings.Add("Text", md, "Value", true).FormatString
= "C";
           f.ShowDialog();
       }
   }
}
class MyData
{
   private decimal value;
   public decimal Value
   {
       get { return this.value; }
       set
       {
           if (Value != value)
           {
               this.value = value;
               EventHandler handler = ValueChanged;
               if (handler != null) handler(this, EventArgs.Empty);
           }
       }
   }
   public event EventHandler ValueChanged;
}
mracuraintegra@gmail.com - 18 Nov 2006 01:21 GMT
WOO HOO! That did it. Thanks a bunch.

> Like so?
>
[quoted text clipped - 37 lines]
>     public event EventHandler ValueChanged;
> }
Marc Gravell - 18 Nov 2006 21:43 GMT
Glad it helped, and thanks for getting back.

(it does sometimes grate a little [not much] when you go to the trouble
of writing a detailed reply, and you never get to find out if a: it
worked / helped, b: it didn't work / fit for some reason, or c: they
never looked for replies).

Happy coding,

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.