Windows Develop Bookmark and Share   
 index > Windows Forms Designer > currencyformat in textbox winforms
 

currencyformat in textbox winforms

Hi all

I have to display data in text box with currency format(comma seperated) with decimal round to 2

I am using Winforms 2005 . similar like 32,344,343,432.50
when the user types 3234434343250

using which control i can achieve this textbox or masked textbox

Any help


Thanks in advance

DAM
damu maddy  Saturday, February 21, 2009 12:21 AM

Hi damu maddy,

I think you need to create an inherited TextBox. There is no such control can fully meet your need.
Here is the code.

publicclassMyTextBox:TextBox
{
publicMyTextBox()
{
}
protectedoverridevoidOnValidating(CancelEventArgse)
{
base.OnValidating(e);
stringresult=this.Text;
if(this.Text.Contains('.'))
{
for(inti=this.Text.IndexOf('.');i>=0;i--)
{
if((this.Text.IndexOf('.')-i)%3==0&&(this.Text.IndexOf('.')-i)!=0&&i!=0)
{
result=result.Insert(i,",");
}
}
}
else
{
for(inti=this.Text.Length;i>=0;i--)
{
if((this.Text.Length-i)%3==0&&(this.Text.Length-i)!=0&&i!=0)
{
result=result.Insert(i,",");
}
}
}
this.Text=result;
}
protectedoverridevoidOnGotFocus(EventArgse)
{
base.OnGotFocus(e);
this.Text=this.Text.Replace(",","");
}
}


I think this can help you solve the problem.

If you have any question, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, February 24, 2009 4:10 AM
Go to properties DataBindings (Advanced) and select numeric or currency and the format you wish.
oiftimus  Saturday, February 21, 2009 8:18 AM


the user manualy enter values in textbox and it is not binded to DB

Regards,
DAM



damu maddy  Monday, February 23, 2009 3:28 PM

Hi damu maddy,

I think you need to create an inherited TextBox. There is no such control can fully meet your need.
Here is the code.

publicclassMyTextBox:TextBox
{
publicMyTextBox()
{
}
protectedoverridevoidOnValidating(CancelEventArgse)
{
base.OnValidating(e);
stringresult=this.Text;
if(this.Text.Contains('.'))
{
for(inti=this.Text.IndexOf('.');i>=0;i--)
{
if((this.Text.IndexOf('.')-i)%3==0&&(this.Text.IndexOf('.')-i)!=0&&i!=0)
{
result=result.Insert(i,",");
}
}
}
else
{
for(inti=this.Text.Length;i>=0;i--)
{
if((this.Text.Length-i)%3==0&&(this.Text.Length-i)!=0&&i!=0)
{
result=result.Insert(i,",");
}
}
}
this.Text=result;
}
protectedoverridevoidOnGotFocus(EventArgse)
{
base.OnGotFocus(e);
this.Text=this.Text.Replace(",","");
}
}


I think this can help you solve the problem.

If you have any question, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, February 24, 2009 4:10 AM

You can use google to search for other answers

Custom Search

More Threads

• Using commands/verbs in the PropertyGrid when hosting the Form Designer
• How To Refresh Property Grid while desigining,.
• Escape Button in a propertyGrid
• Launching a Window in the Designer from a Custom Server Control
• Using the designer in my own applications
• Saving a panel content as an image
• Visual inheritence problem
• Problem with my custom control crashing the Windows Forms Designer
• ScrollBars location for panel
• Problem with Usercontrol