Format bound data in a textbox

M

Michael Kellogg

I have a textbox on my Winform that is data bound to a floating-point
element. It's displaying the number out to like 9 digits. Since I'm not
"handing" the textbox the value using ".ToString("f3")" I'm at a loss for
how to control the formatting. Is there a simple way to accomplish this?
 
G

Guest

Try,
Binding binding = new Binding("Text", dataTable, "Column1");
binding.Format += new ConvertEventHandler(DoubleToString);
binding.Parse += new ConvertEventHandler(StringToDouble);
textBox1.DataBindings.Add(binding);
..
..
..
private void DoubleToString(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
e.Value = System.Convert.ToDouble(e.Value).ToString("f3");
}

private void StringToDouble(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(double)) return;
e.Value = double.Parse(e.Value.ToString());
}

Regards,
Phil.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top