textBox中输入的数字怎么转为float?

syp133 2003-10-18 02:14:07
简单的问题;
另外怎么判断textBox中输入的是合法的数字而不是其他;

菜鸟的说;
今晚结贴;
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
syp133 2003-10-18
  • 打赏
  • 举报
回复
谢谢各位了
JB-Zhang 2003-10-18
  • 打赏
  • 举报
回复
使用用类Convert的一个静态方法就可以了,Convert.ToFloat(),或 Convert.ToDouble()
rgbcn 2003-10-18
  • 打赏
  • 举报
回复
// Source Code starts

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

class NumberBox:TextBox
{
public NumberBox()
{
this.KeyPress+=new KeyPressEventHandler(NumberBox_KeyPress);
}

private void NumberBox_KeyPress(object sender,KeyPressEventArgs kpe)
{
int KeyCode=(int)kpe.KeyChar;

if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46)
{
kpe.Handled=true;
}
else
{
if(KeyCode==46)
{
kpe.Handled=(this.Text.IndexOf(".")>-1);
}
}
}

private bool IsNumberInRange(int Val,int Min,int Max)
{
return (Val>=Min && Val<=Max);
}
}

class NumberBoxDemo:Form
{
Label l1=new Label();
NumberBox n1=new NumberBox();
public NumberBoxDemo()
{
l1.Text="Number Box:";
n1.Location=new Point(l1.Left+l1.Width+10,l1.Top);
this.Controls.Add(l1);
this.Controls.Add(n1);
}

public static void Main()
{
Application.Run(new NumberBoxDemo());
}
}
// Source Code End
孟子E章 2003-10-18
  • 打赏
  • 举报
回复
System.Single.Parse(TextBoxId.Text);
System.Decimal.Parse(TextBoxId.Text);
rgbcn 2003-10-18
  • 打赏
  • 举报
回复
private void TextBox_Validation(object sender,CancelEventArgs ce)
{
try
{
int value=Int32.Parse(this.Text);
}
catch(Exception)
{
ce.Cancel=true;
MessageBox.Show("Please Enter Numeric Value");
}
}

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧