Windows Develop Bookmark and Share   
 index > Windows Forms General > Format Value on LostFocus
 

Format Value on LostFocus

I want to format the values which user is entering on the text box of windows form.

The requirement is user will enter the value and will click on the tab key. Please let me know how to do this using the Format function..??? Say if user enters 2, It shd get change to 2.0000 on the lost focus.

If I should use other event instead of LostFocus then also please suggest.

Anonymous261977  Thursday, November 08, 2007 11:33 AM

Hi Anonymous261977,

Based on your post, you want format the text when leaving the text box, don’t you? If so, I suggest you handle the Validating event instead, because you can check if input value is in right type in this handler. Try something like the following:

Code Block

TextBoxP

public partial class Form12 : Form

{

public Form12()

{

InitializeComponent();

this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);

this.textBox1.Enter += new EventHandler(textBox1_Enter);

}

void textBox1_Enter(object sender, EventArgs e)

{

if (this.textBox1.Tag != null)

this.textBox1.Text = this.textBox1.Tag.ToString();

}

void textBox1_Validating(object sender, CancelEventArgs e)

{

try

{

decimal d = Decimal.Parse(this.textBox1.Text);

this.textBox1.Tag = this.textBox1.Text;

this.textBox1.Text = d.ToString("0.0000");

}

catch

{

MessageBox.Show("Can not accept this input!");

e.Cancel = true;

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Monday, November 12, 2007 4:00 AM

Hi Anonymous261977,

Based on your post, you want format the text when leaving the text box, don’t you? If so, I suggest you handle the Validating event instead, because you can check if input value is in right type in this handler. Try something like the following:

Code Block

TextBoxP

public partial class Form12 : Form

{

public Form12()

{

InitializeComponent();

this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);

this.textBox1.Enter += new EventHandler(textBox1_Enter);

}

void textBox1_Enter(object sender, EventArgs e)

{

if (this.textBox1.Tag != null)

this.textBox1.Text = this.textBox1.Tag.ToString();

}

void textBox1_Validating(object sender, CancelEventArgs e)

{

try

{

decimal d = Decimal.Parse(this.textBox1.Text);

this.textBox1.Tag = this.textBox1.Text;

this.textBox1.Text = d.ToString("0.0000");

}

catch

{

MessageBox.Show("Can not accept this input!");

e.Cancel = true;

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Monday, November 12, 2007 4:00 AM

You can use google to search for other answers

Custom Search

More Threads

• [Help] Web Browser
• In C#.NET How to get Current Control Name
• Drawing outside of your Form
• Vertical scrollbars on multi-column listbox.
• AxWebBrowser/mshtml: how to calculate physical page breaks position
• Please help me fix my Find and Find Next controls. Im so close!!
• (vb) asp.net validation
• BindingList save to database
• RichTextBox keyboard shortcuts
• How to make scrollbars move accordingly to make the focus viewable?