C# 文本输入限制类型,datagridview单元格输入验证
1.只能输入double类型
private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e)
{
{
//数字0~9所对应的keychar为48~57,小数点是46,Backspace是8
e.Handled = true;
//输入0-9和Backspace del 有效
if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
{
e.Handled = false;
}
if (e.KeyChar == 46) //小数点
{
if (textBoxX6.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
if (float.TryParse(textBoxX6.Text + e.KeyChar.ToString(), out f))
{
e.Handled = false;
}
}
}
}
2.只能输入数字
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
// 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22)
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 &&
e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22)
{
e.Handled = true;
}
}
3.
ESC 27 7 55
SPACE 32 8 56
! 33 9 57
" 34 : 58
# 35 ; 59
$ 36 < 60
% 37 = 61
& 38 > 62
' 39 ? 63
( 40 @ 64
) 41 A 65
* 42 B 66
+ 43 C 67
' 44 D 68
- 45 E 69
. 46 F 70
/ 47 G 71
0 48 H 72
1 49 I 73
2 50 J 74
3 51 K 75
4 52 L 76
5 53 M 77
6 54 N 78
O 79 g 103
P 80 h 104
Q 81 i 105
R 82 j 106
S 83 k 107
T 84 l 108
U 85 m 109
V 86 n 110
W 87 o 111
X 88 p 112
Y 89 q 113
Z 90 r 114
[ 91 s 115
\ 92 t 116
] 93 u 117
^ 94 v 118
_ 95 w 119
` 96 x 120
a 97 y 121
b 98 z 122
c 99 { 123
d 100 | 124
e 101 } 125
f 102 ~ 126
75=<e.char<=122 为字符
48=<e.char<=56 为字符
2.单元格输入验证
this.dGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dGV_EditingControlShowing);
}
void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex == 4)
{
e.Control.KeyPress -= new KeyPressEventHandler(TextBoxDec_KeyPress);
e.Control.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);
}
} private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex ==4)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
} }
要点:在 EditingControlShowing 的事件中,判断单元格,所在列,调取文本输入事件
C# 文本输入限制类型,datagridview单元格输入验证的更多相关文章
- winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...
- WinForm中DataGridView验证单元格输入的是数字
转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...
- Winform Datagridview 单元格html格式化支持富文本
Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...
- 设置DataGridView单元格的文本对齐方式
实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性 //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...
- DataGridView 单元格自动填充
在DataGridView单元格中,当输入指定字符时,自动完成填充. 通过 TextBox实现 AutoCompleteMode AutoCompleteMode.Suggest: AutoCompl ...
- 【Winform-自定义控件】DataGridView 单元格合并和二维表头
DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("); ...
- WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序
TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- DataGridView单元格合并
本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...
随机推荐
- Manager Test and DAO
1. 阅读ManagerTest代码 (1)代码 import java.util.* package test; /** * This program demonstrates inheritanc ...
- Java-Runoob:Java switch case
ylbtech-Java-Runoob:Java switch case 1.返回顶部 1. Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等 ...
- springboot成神之——拦截器
本文介绍spring boot拦截器 创建拦截器类LogInterceptor.java 创建拦截器类OldLoginInterceptor.java 拦截器配置类WebMvcConfig.java ...
- selenium 获取某元素的 某属性 的值
selenium 获取某元素的 某属性的值 1 先通过元素定位,获得此元素的 WebElement; WebElement yuansu = driver.findElement(By.clas ...
- 执行CRUD总结
- 一.volatile关键字
一.volatile关键字的原理 使用volatile关键字增加了实例变量在多个线程之间的可见性.但volatile的最致命的缺点是不支持原子性. synchronized代码块具有volatile同 ...
- linux cp -r chmod -R 递归拷贝 删除 改权限
在linux下拷贝的时候有时候会出现cp:omitting directory的错误 ,例如 cp:omitting directory "bbs" 说明bbs目录下面还有目录,不 ...
- Linux常用基本命令 1
useradd 创建用户. password 修改密码. date 查看时间 man date 帮助文档.f往后翻 b往前翻 q退出.软修改 man hwclock 修改硬件时钟, cal 查看日历 ...
- Unity shader saturate
当你想将颜色值规范到0~1之间时,你可能会想到使用saturate函数(saturate(x)的作用是如果x取值小于0,则返回值为0.如果x取值大于1,则返回值为1.若x在0到1之间,则直接返回x的值 ...
- Quick Find
--------------------siwuxie095 Quick Find 这里介绍并查集的一种实现思路:Qui ...