比如要实现两个textbox,在里面输入数字,按下button时在另一个textbox中显示两个数的和添加控件之后的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//在此加入实现相加的代码
}
}
}现在想要只让textbox只接受数字和小数点
google了一下,找到如下代码:
//KeyPress事件:当控件获得焦点,并且用户按下且释放键盘上的键后发生
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//文本框只接受数字的输入和小数点
{
//IsNumber:指定字符串中位于指定位置的字符是否属于数字类别
//IsPunctuation:指定字符串中位于指定位置的字符是否属于标点符号类别
//IsControl:指定字符串中位于指定位置的字符是否属于控制字符类别
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true; //获取或设置一个值,指示是否处理过System.Windows.Forms.Control.KeyPress事件
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar == '.')
{
if (((TextBox)sender).Text.LastIndexOf('.') != -1)
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
}请问:1、Handled是什么属性?
2、我找到的这段代码应该加入什么位置?还要加什么控件不?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//在此加入实现相加的代码
}
}
}现在想要只让textbox只接受数字和小数点
google了一下,找到如下代码:
//KeyPress事件:当控件获得焦点,并且用户按下且释放键盘上的键后发生
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//文本框只接受数字的输入和小数点
{
//IsNumber:指定字符串中位于指定位置的字符是否属于数字类别
//IsPunctuation:指定字符串中位于指定位置的字符是否属于标点符号类别
//IsControl:指定字符串中位于指定位置的字符是否属于控制字符类别
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true; //获取或设置一个值,指示是否处理过System.Windows.Forms.Control.KeyPress事件
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar == '.')
{
if (((TextBox)sender).Text.LastIndexOf('.') != -1)
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
}请问:1、Handled是什么属性?
2、我找到的这段代码应该加入什么位置?还要加什么控件不?
2、那个代码加到TextBox1_TextChanged 下!
private void button1_Click(object sender, EventArgs e)
{
//在此加入实现相加的代码
//自己加判断语句,限定输入的值是数字,这里就不写了
int sum = 0;
int number1 = int.Parse(textbox1.text);
int number2 = int.Parse(textbox2.text);
sum = number1+number2;
textbox3.text = sum.ToString();
}
老板给分啊,我现在穷的叮当响
ivanx 著于2007-8-14 16:26:56
本文的控件用于安全密码输入,它可以让你在输入密码的时候避免被键盘记录木马截获。当你用鼠标点击输入框时,这个控件会自动弹出。异曲同工
当你要限制使textbox只能输入数字和小数点时,要这样写.
private void button1_Click(object sender, EventArgs e)
{
///变量先初始化
int sum = 0;
int number1 = 0;
int number2 = 0;
///判断是否成功转换成数字
if (int.TryParse(textBox1.Text, out number1))//int number1 = int.Parse(textBox1.Text);
{
MessageBox.Show("success");
}
else
{
"Error,please try again.";
}
if (int.TryParse(textBox2.Text, out number2))//int number1 = int.Parse(textBox1.Text);
{
MessageBox.Show("success");
}
else
{
"Error,please try again.";
}//int number2 = int.Parse(textBox2.Text);
sum = number1 + number2;
textBox3.Text = sum.ToString(); }
快点给分啊,吼吼~~~~~~
改正:
else
{
MessageBox.Show("Error,please try again.");
}
代码里类型改成double就可以满足你的要求了
给分,给分~~~~~
pupstar:我的代码里没有TextBox1_TextChanged呀
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string pattern = @"^[0-9]|\.$";
Regex reg = new Regex(pattern);
if (!reg.Match(e.KeyChar.ToString()).Success)
{
e.Handled = true;
}
}
===============把代码里的类型改成double.===================================
后一个要求:
我是想要如果在textbox1和textbox2中输入数字,则接受,如果输入的是其他,则没反应,好像windows中的计算器一样。
可以把if改成while,当转换未成功,提示出错,重新输入.这个自己写吧
e.Handled = false;
else
e.Handled = true;
两句话搞定。
private void textBoxYMaxScale_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
//为数字时 }
else if (e.KeyChar == 8)
{
//为删除键时 }
else if (e.KeyChar == 46)
{
//为点时 }
else
{
//其它所有字符
e.KeyChar = Convert.ToChar(20);
}
}
另外guoxia的代码没有看明白
怎么吧处理时间放在了button按钮里面了
{
//为数字时 }
else if (e.KeyChar == 8)
{
//为删除键时 }
else if (e.KeyChar == 46)
{
//为点时 }
else
{
//其它所有字符
e.KeyChar = Convert.ToChar(20);
楼上的用阿斯科码判断
{ if ( e.KeyChar > (char)47 && e.KeyChar < (char)58 || e.KeyChar ==(char)8 ||e.KeyChar==(char)46)
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("只能输入数字0-9和“.”如:(192.168.1.1)");
}
{
string f = textBox1.Text.Substring(i, 1).ToString().Trim();
if (f != "1" && f != "2" && f != "3" && f != "4" && f != "5" && f != "6" && f != "7" && f != "8" && f != "9" && f != "0" && f != ".")
{
MessageBox.Show("请输入数字类型!");
textBox1.Text = "";
}
}
{
ShowMessageBox("请输入数字!");
return (char)0;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
Single s = Convert.ToSingle(textBox1.Text + e.KeyChar);
}
catch (Exception ex)
{
e.Handled = true;
}
}
{
try
{
Single s = Convert.ToSingle(textBox1.Text + e.KeyChar);
}
catch (Exception ex)
{
e.Handled = true;
}
}这种方式,好像可以用Ctrl+c/ Ctrl+v 输入非法的数据。
{
if(!(((window.event.keyCode>=48)&&(window.event.keyCode<=57))||(window.event.keyCode==13)||(window.event.keyCode==46)||(window.event.keyCode==45)))
{
window.event.keyCode=0;
}
}
WPF中的TextBox有Keypressed事件吗?