By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Formatting a bound textbox to show currency?

P: n/a
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.

Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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;
}

Nov 16 '06 #2

P: n/a
WOO HOO! That did it. Thanks a bunch.
Marc Gravell wrote:
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;
}
Nov 18 '06 #3

P: n/a
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

Nov 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.