Pages

‏إظهار الرسائل ذات التسميات c # Format. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات c # Format. إظهار كافة الرسائل

C# Numbers Only in Textbox

I created a text box event handler to only allow my textbox to accept Numbers only. The Event handler works great and does not allow anything but numbers except when the form first loads and the character entered is non-numeric it then allows the first charcter to be entered can be non-numberic. If I enter a number then erase it it will not allow a non-numeric character.

I saw one example of this but the texbox had a underline in it but it did not have any code in it.
Here is my event handler right now
 
first way
C# Syntax
 
private void text1_TextChanged(object sender, EventArgs e)
{
      text1.KeyPress += new KeyPressEventHandler(rtbQuantity_KeyPress);
}
private void text1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0')  || (e.KeyChar > '9')) e.Handled = true;
}
 
 
sec way
 
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)

{

int isNumber = 0; e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);

}
 
 
3rt way
 
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)


{

int isNumber = 0; e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);

}

c # Format a String as Currency


When building a string for output to a web page, it’s useful to format any currency value in a human-friendly money format. This is extremely easy in C#.
The system format string works like this: {0:C}
For example, the following code example:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
Outputs the following:
Order Total: $1,921.39

It’s worth noting that you must pass in a numeric value to the String.Format statement. If you pass in a string value, it won’t format correctly. If your currency value is in a string, you need to convert it into a double first.

Link With in

Related Posts Plugin for WordPress, Blogger...