Here is the code in C# to allow only one decimal point in a textbox:
以下是C#中的代码,只允许文本框中的一个小数点:
if textbox5.text.contains(".") && e.keychar="."
{
e.handled=true
}
I have to use it in VB.NET 2003 version, but I can't use the contains property. How can I do this?
我必须在VB.NET 2003版本中使用它,但我不能使用contains属性。我怎样才能做到这一点?
3 个解决方案
#1
How about using the Text.IndexOf(".")
method instead? If it returns >=0 then you already have a decimal point.
如何使用Text.IndexOf(“。”)方法呢?如果它返回> = 0,那么你已经有一个小数点。
If (textbox5.Text.IndexOf(".") >= 0 And e.KeyChar = ".") Then e.Handled = True
#2
Don't forget about the user's locale and number formats. I believe using a period ('.') as a decimal character is in the minority, world-wide. You can use CurrentUICulture
's NumberFormat
field to help you parse in the most correct way. Try using:
不要忘记用户的区域设置和数字格式。我认为使用句号('。')作为十进制字符在世界范围内是少数。您可以使用CurrentUICulture的NumberFormat字段来帮助您以最正确的方式进行解析。尝试使用:
decimal d;
bool isValidInput = decimal.TryParse(
textBox1.Text,
System.Globalization.NumberStyles.AllowDecimalPoint,
System.Globalization.CultureInfo.CurrentUICulture.NumberFormat,
out d);
This will validate whether a number was entered properly. Using other NumberStyles
flags, you can control what kinds of numbers you want to allow, for example things like thousands separators.
这将验证是否正确输入了数字。使用其他NumberStyles标志,您可以控制要允许的数字类型,例如千位分隔符。
*Sorry for C# here. VB.NET should be easy to adapt from this though; maybe some kind soul can edit it in.
*抱歉C#在这里。 VB.NET应该很容易适应这个;也许某种灵魂可以编辑它。
#3
package Utility
{
import flash.events.Event;
import flashx.textLayout.operations.PasteOperation;
import spark.components.TextInput;
import spark.events.TextOperationEvent;
/**
* This is a text input component that can only input numbers.
* A number means a real number.
* @author Cheng Liang
* @version 0.0.1
*/
public class NumberInput extends TextInput
{
[Bindable("numberChange")]
/**
* Sets number as input value.
*/
public function set number(value : Number) : void
{
var s : String = value.toString();
if(text != s) {
text = s;
dispatchEvent(new Event("numberChange"));
}
}
public function get number() : Number
{
return Number(text);
}
public function NumberInput()
{
super();
this.addEventListener(TextOperationEvent.CHANGE, onTextChange);
}
protected function onTextChange(event : TextOperationEvent) : void
{
if (event.operation is PasteOperation)
{
TextInput(event.currentTarget).text='';
}
if(text == "" || text.length == 1) {
return;
}
/* To allow single (.) */
var idx : int = text.indexOf(".", 0);
if(idx >= 0) {
text = text.substring(0, idx + 1) + text.substring(idx + 1).replace(".", "");
}
/* To allow single (-) only at the 1st position*/
if(text.lastIndexOf("-")==0)
text ='-'+ text.substring(1);
else if(text.lastIndexOf("-")>0)
{
if(text.charAt(0)=='-')
text ='-'+ text.substring(1).replace("-", "");
else
text =text.replace("-", "");
}
var arr:Array = TextInput(event.currentTarget).text.split(".");
try{
if(arr[1] && String(arr[1]).length > 2){
TextInput(event.currentTarget).text = arr[0]+"."+String(arr[1]).slice(0,2);
}
}catch(err:Error){}
this.selectRange((text.length), (text.length));
}
}
}
#1
How about using the Text.IndexOf(".")
method instead? If it returns >=0 then you already have a decimal point.
如何使用Text.IndexOf(“。”)方法呢?如果它返回> = 0,那么你已经有一个小数点。
If (textbox5.Text.IndexOf(".") >= 0 And e.KeyChar = ".") Then e.Handled = True
#2
Don't forget about the user's locale and number formats. I believe using a period ('.') as a decimal character is in the minority, world-wide. You can use CurrentUICulture
's NumberFormat
field to help you parse in the most correct way. Try using:
不要忘记用户的区域设置和数字格式。我认为使用句号('。')作为十进制字符在世界范围内是少数。您可以使用CurrentUICulture的NumberFormat字段来帮助您以最正确的方式进行解析。尝试使用:
decimal d;
bool isValidInput = decimal.TryParse(
textBox1.Text,
System.Globalization.NumberStyles.AllowDecimalPoint,
System.Globalization.CultureInfo.CurrentUICulture.NumberFormat,
out d);
This will validate whether a number was entered properly. Using other NumberStyles
flags, you can control what kinds of numbers you want to allow, for example things like thousands separators.
这将验证是否正确输入了数字。使用其他NumberStyles标志,您可以控制要允许的数字类型,例如千位分隔符。
*Sorry for C# here. VB.NET should be easy to adapt from this though; maybe some kind soul can edit it in.
*抱歉C#在这里。 VB.NET应该很容易适应这个;也许某种灵魂可以编辑它。
#3
package Utility
{
import flash.events.Event;
import flashx.textLayout.operations.PasteOperation;
import spark.components.TextInput;
import spark.events.TextOperationEvent;
/**
* This is a text input component that can only input numbers.
* A number means a real number.
* @author Cheng Liang
* @version 0.0.1
*/
public class NumberInput extends TextInput
{
[Bindable("numberChange")]
/**
* Sets number as input value.
*/
public function set number(value : Number) : void
{
var s : String = value.toString();
if(text != s) {
text = s;
dispatchEvent(new Event("numberChange"));
}
}
public function get number() : Number
{
return Number(text);
}
public function NumberInput()
{
super();
this.addEventListener(TextOperationEvent.CHANGE, onTextChange);
}
protected function onTextChange(event : TextOperationEvent) : void
{
if (event.operation is PasteOperation)
{
TextInput(event.currentTarget).text='';
}
if(text == "" || text.length == 1) {
return;
}
/* To allow single (.) */
var idx : int = text.indexOf(".", 0);
if(idx >= 0) {
text = text.substring(0, idx + 1) + text.substring(idx + 1).replace(".", "");
}
/* To allow single (-) only at the 1st position*/
if(text.lastIndexOf("-")==0)
text ='-'+ text.substring(1);
else if(text.lastIndexOf("-")>0)
{
if(text.charAt(0)=='-')
text ='-'+ text.substring(1).replace("-", "");
else
text =text.replace("-", "");
}
var arr:Array = TextInput(event.currentTarget).text.split(".");
try{
if(arr[1] && String(arr[1]).length > 2){
TextInput(event.currentTarget).text = arr[0]+"."+String(arr[1]).slice(0,2);
}
}catch(err:Error){}
this.selectRange((text.length), (text.length));
}
}
}